#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int solve(string& s, int n) {
int onesBeforeZero = 0; // Misplaced '1's for non-decreasing order
int zeroesBeforeOne = 0; // Misplaced '0's for non-increasing order
int onesCount = 0;
int zeroCount = 0;
// Calculate misplaced '1's (for non-decreasing order)
// Count how many '1's are before a '0' for non-decreasing order
for (int i = 0; i < n; ++i) {
if (s[i] == '1') {
onesCount++;
} else {
// Each '0' after a '1' means we need to swap
onesBeforeZero += onesCount;
}
}
// Calculate misplaced '0's (for non-increasing order)
// Count how many '0's are before a '1' for non-increasing order
for (int i = 0; i < n; ++i) {
if (s[i] == '0') {
zeroCount++;
} else {
// Each '1' after a '0' means we need to swap
zeroesBeforeOne += zeroCount;
}
}
// Return the minimum of the two cases
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;
}