#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--){
ll n;
queue<int> q;
string s;
cin >> n >> s;
ll time = n/2 - 1;
for(int i = 0; i < n; i++){
if(s[i] == '1'){
if(i > 2 * q.size()){
q.push(i+1);
}
else{
q.pop();
q.push(i+1);
}
}
}
ll sum = n * (n+1) / 2;
while(!q.empty()){
sum -= q.front();
q.pop();
}
cout << sum << endl;
}
return 0;
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwoKaW50IG1haW4oKXsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUobnVsbHB0cik7CiAgICBpbnQgdDsgCiAgICBjaW4gPj4gdDsKICAgIHdoaWxlKHQtLSl7CiAgICAgICAgbGwgbjsKICAgICAgICBxdWV1ZTxpbnQ+IHE7CiAgICAgICAgc3RyaW5nIHM7CiAgICAgICAgY2luID4+IG4gPj4gczsKICAgICAgICBsbCB0aW1lID0gbi8yIC0gMTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKXsKICAgICAgICAgICAgaWYoc1tpXSA9PSAnMScpewogICAgICAgICAgICAgICAgaWYoaSA+IDIgKiBxLnNpemUoKSl7CiAgICAgICAgICAgICAgICAgICAgcS5wdXNoKGkrMSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgICAgIHEucG9wKCk7CiAgICAgICAgICAgICAgICAgICAgcS5wdXNoKGkrMSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgbGwgc3VtID0gbiAqIChuKzEpIC8gMjsKICAgICAgICB3aGlsZSghcS5lbXB0eSgpKXsKICAgICAgICAgICAgc3VtIC09IHEuZnJvbnQoKTsKICAgICAgICAgICAgcS5wb3AoKTsKICAgICAgICB9CiAgICAgICAgY291dCA8PCBzdW0gPDwgZW5kbDsKICAgIH0KICAgIHJldHVybiAwOwp9