#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int solve(string& s, int n) {
// Misplaced 1's in non-decreasing order (0's first, then 1's)
int onesBeforeZero = 0;
int onesCount = 0;
// Traverse the string to count misplaced 1's for non-decreasing order
for (int i = 0; i < n; ++i) {
if (s[i] == '1') {
onesCount++;
} else {
onesBeforeZero += onesCount;
}
}
// Misplaced 0's in non-increasing order (1's first, then 0's)
int zeroesBeforeOne = 0;
int zeroCount = 0;
// Traverse the string to count misplaced 0's for non-increasing order
for (int i = 0; i < n; ++i) {
if (s[i] == '0') {
zeroCount++;
} else {
zeroesBeforeOne += zeroCount;
}
}
// Return the minimum of the two counts
return min(onesBeforeZero, zeroesBeforeOne);
}
int main() {
int T;
cin >> T;
while (T--) {
int N;
string S;
cin >> N >> S;
cout << solve(S, N) << endl;
}
return 0;
}