#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug(a) cout << #a << " : " << a << endl;
const ll N = 1e6, mod = 1e9 + 7;
ll power(long long n, long long k) {
ll ans = 1 % mod; n %= mod; if (n < 0) n += mod;
while (k) {
if (k & 1) ans = (long long) ans * n % mod;
n = (long long) n * n % mod;
k >>= 1;
}
return ans;
}
ll f[N], invf[N];
ll nCr(ll n, ll r) {
if (n < r or n < 0) return 0;
return 1LL * f[n] * invf[r] % mod * invf[n - r] % mod;
}
ll nPr(ll n, ll r) {
if (n < r or n < 0) return 0;
return 1LL * f[n] * invf[n - r] % mod;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
f[0] = 1;
for (ll i = 1; i < N; i++) {
f[i] = 1LL * i * f[i - 1] % mod;
}
invf[N - 1] = power(f[N - 1], mod - 2);
for (ll i = N - 2; i >= 0; i--) {
invf[i] = 1LL * invf[i + 1] * (i + 1) % mod;
}
ll t = 1; cin >> t;
while (t--) {
ll n; cin >> n;
string s;cin>>s;
map<char,ll>mp;
for(auto i:s)mp[i]++;
ll ans=0;
for(auto i:mp){
ans+=nCr(i.second,2);
}
cout<<ans*2<<endl;
}
return 0;
}