#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
map<char, int> last, cnt;
LL ans = 0;
FOR(i, 0, n)
{
last[s[i]] = i;
ans -= cnt[s[i]];
cnt[s[i]]++;
}
FOR(i, 0, n)
ans += last[s[i]] - i;
cout << 5 * ans << "\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}