#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
unordered_map<char, int> charCount;
for (char c : s) {
charCount[c]++;
}
int totalSwaps = 0;
for (auto& entry : charCount) {
int count = entry.second;
totalSwaps += (count * (count - 1));
}
bool allSame = true;
for (int i = 1; i < n; ++i) {
if (s[i] != s[0]) {
allSame = false;
break;
}
}
if (allSame) {
totalSwaps = (n * (n - 1)) / 2;
}
cout << totalSwaps << endl;
}
return 0;
}