#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define pb push_back
#define fr_z(start,end) for(int z=start;z<end;z++)
#define fr_o(start,end) for(int o=start;o<end;o++)
#define all(m) m.begin(),m.end()
#define mp make_pair
#define fa_io std::ios::sync_with_stdio(false); cin.tie(NULL)
pair<int,int> totct[100000];
int len[100000],prv[100000];
int main()
{
fa_io;
int t,n;
string s;
cin>>t;
while(t--)
{
cin>>n;
cin>>s;
fr_z(0,n)
len[z]=0,prv[z]=-1;
totct[0]=(s[0]=='0')?mp(1,0):mp(0,1);
fr_z(1,n)
totct[z]=s[z]=='0'?mp(totct[z-1].first+1,totct[z-1].second):mp(totct[z-1].first,totct[z-1].second+1);
fr_z(0,n)
{
int tz=totct[z].first,to=totct[z].second;
//cout<<z<<'-'<<tz<<
fr_o(0,z+1)
{
if(to>tz && len[z]<tz+to)
len[z]=tz+to,prv[z]=o;
//cout<<tz<<'-'<<to<<'\n';
s[o]=='0'?tz--:to--;
}
}
int z=n-1,j,u;
/*cout<<'\n';
fr_z(0,n)
cout<<setw(4)<<len[z]<<' ';*/
int sm=0;
while(z>=0)
{
if(!len[z])
{
z--;
continue;
}
u=z-1;
bool i=true;
for(u=z-1;u>z-len[z];u--)
if(len[u]>=len[z])
{
i=false;
break;
}
if(i)
fr_o(z-len[z]+1,z)
len[o]=0;
else
len[z]=0;
z--;
}
/*cout<<'\n';
fr_z(0,n)
cout<<setw(4)<<len[z]<<' ';
cout<<'\n';
fr_z(0,n)
cout<<setw(4)<<prv[z]<<' ';*/
cout<<accumulate(len,len+n,0)<<'\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIHZpIHZlY3RvcjxpbnQ+CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgZnJfeihzdGFydCxlbmQpIGZvcihpbnQgej1zdGFydDt6PGVuZDt6KyspCiNkZWZpbmUgZnJfbyhzdGFydCxlbmQpIGZvcihpbnQgbz1zdGFydDtvPGVuZDtvKyspCiNkZWZpbmUgYWxsKG0pIG0uYmVnaW4oKSxtLmVuZCgpCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgZmFfaW8gc3RkOjppb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7IGNpbi50aWUoTlVMTCkKCnBhaXI8aW50LGludD4gdG90Y3RbMTAwMDAwXTsKaW50IGxlblsxMDAwMDBdLHBydlsxMDAwMDBdOwoKaW50IG1haW4oKQp7CiAgICBmYV9pbzsKICAgIAogICAgaW50IHQsbjsKICAgIHN0cmluZyBzOwogICAgY2luPj50OwogICAgd2hpbGUodC0tKQogICAgewogICAgICAgIGNpbj4+bjsKICAgICAgICBjaW4+PnM7CiAgICAgICAgZnJfeigwLG4pCiAgICAgICAgICAgIGxlblt6XT0wLHBydlt6XT0tMTsKICAgICAgICB0b3RjdFswXT0oc1swXT09JzAnKT9tcCgxLDApOm1wKDAsMSk7CiAgICAgICAgZnJfeigxLG4pCiAgICAgICAgICAgIHRvdGN0W3pdPXNbel09PScwJz9tcCh0b3RjdFt6LTFdLmZpcnN0KzEsdG90Y3Rbei0xXS5zZWNvbmQpOm1wKHRvdGN0W3otMV0uZmlyc3QsdG90Y3Rbei0xXS5zZWNvbmQrMSk7CiAgICAgICAgZnJfeigwLG4pCiAgICAgICAgewogICAgICAgICAgICBpbnQgdHo9dG90Y3Rbel0uZmlyc3QsdG89dG90Y3Rbel0uc2Vjb25kOwogICAgICAgICAgICAvL2NvdXQ8PHo8PCctJzw8dHo8PAogICAgICAgICAgICBmcl9vKDAseisxKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZih0bz50eiAmJiBsZW5bel08dHordG8pCiAgICAgICAgICAgICAgICAgICAgbGVuW3pdPXR6K3RvLHBydlt6XT1vOwogICAgICAgICAgICAgICAgLy9jb3V0PDx0ejw8Jy0nPDx0bzw8J1xuJzsKICAgICAgICAgICAgICAgIHNbb109PScwJz90ei0tOnRvLS07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaW50IHo9bi0xLGosdTsKICAgICAgICAvKmNvdXQ8PCdcbic7CiAgICAgICAgZnJfeigwLG4pCiAgICAgICAgICAgIGNvdXQ8PHNldHcoNCk8PGxlblt6XTw8JyAnOyovCiAgICAgICAgaW50IHNtPTA7CiAgICAgICAgd2hpbGUoej49MCkKICAgICAgICB7CiAgICAgICAgICAgIGlmKCFsZW5bel0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHotLTsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHU9ei0xOwogICAgICAgICAgICBib29sIGk9dHJ1ZTsKICAgICAgICAgICAgZm9yKHU9ei0xO3U+ei1sZW5bel07dS0tKQogICAgICAgICAgICAgICAgaWYobGVuW3VdPj1sZW5bel0pCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaT1mYWxzZTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoaSkKICAgICAgICAgICAgICAgIGZyX28oei1sZW5bel0rMSx6KQogICAgICAgICAgICAgICAgICAgIGxlbltvXT0wOwogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICBsZW5bel09MDsKICAgICAgICAgICAgei0tOwogICAgICAgIH0KICAgICAgICAvKmNvdXQ8PCdcbic7CiAgICAgICAgZnJfeigwLG4pCiAgICAgICAgICAgIGNvdXQ8PHNldHcoNCk8PGxlblt6XTw8JyAnOwogICAgICAgIGNvdXQ8PCdcbic7CiAgICAgICAgZnJfeigwLG4pCiAgICAgICAgICAgIGNvdXQ8PHNldHcoNCk8PHBydlt6XTw8JyAnOyovCiAgICAgICAgY291dDw8YWNjdW11bGF0ZShsZW4sbGVuK24sMCk8PCdcbic7CiAgICB9CiAgICAKICAgIHJldHVybiAwOwp9