#include <iostream>
#include <string>
using namespace std;
int maxConsecutiveOnes(string s) {
int count = 0;
int maxCount = 0;
for (char c : s) {
if (c == '1') {
count++;
} else {
maxCount = max(maxCount, count);
count = 0;
}
}
maxCount = max(maxCount, count);
return maxCount;
}
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
string S;
cin >> S;
int totalOnes = 0;
for (char c : S) {
if (c == '1') totalOnes++;
}
if (totalOnes == 0) {
cout << 0 << endl;
continue;
}
int totalFlips = N - totalOnes;
if (totalFlips >= 1) {
cout << totalOnes + min(totalFlips, totalOnes) << endl;
} else {
cout << totalOnes << endl;
}
}
return 0;
}