#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;
}