1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main(){
    int t;
    cin >> t;
    
    while(t--){
        int n;
        cin >> n;
        vector<pair<int, int> > p;
        
        bool f = false;
        bool lj = false;
        int s = 0;
        int j=0;
    
        for(int i=0; i<n; i++){
            char x;
            cin >> x;
            if(x=='0'){
                if(f==false){
                    f=true;
                    s=1;
                }else{
                    s++;
                }
            }
            if(x=='1'){
                j++;
                if(f==true){
                    if(lj==false){
                        p.push_back(make_pair(s, 0));
                    }else if(s%2==0){
                        p.push_back(make_pair(s/2, s/2));
                    }else{
                        p.push_back(make_pair(s/2, (s+1)/2));
                    }
                    f=false;
                }
                lj=true;
            }
        }
        
        if(f==true && lj==true){
            p.push_back(make_pair(s, 0));
        }
        
        sort(p.begin(), p.end(), greater<pair<int, int> >());
        
        int cur = 0;
        
        for(int i=0; i<p.size(); i++){
            //cout << "[" << p[i].first << " " << p[i].second << "] ";
            if(p[i].second==0){
                if(p[i].first==cur){
                    j+=cur;
                }else if(p[i].first<cur){
                    j+=p[i].first;
                }else if(p[i].first>cur){
                    j+=cur;
                    cur++;
                }
            }else if(p[i].second==p[i].first){
                if(p[i].first==cur){
                    j+=cur;
                }else if(p[i].first<cur){
                    j+=p[i].first;
                }else if (p[i].first>cur){
                    j+=cur;
                    cur++;
                }
                
                if(p[i].second==cur){
                    j+=cur;
                }else if(p[i].second<cur){
                    j+=p[i].first;
                }else if(p[i].second>cur){
                    j+=cur;
                    cur++;
                }
            }else if(p[i].second>p[i].first){
                if(p[i].second==cur){
                    j+=cur;
                }else if(p[i].second<cur){
                    j+=p[i].second;
                }else if(p[i].second>cur){
                    j+=cur;
                    cur ++;
                }
                
                if(p[i].first==cur){
                    j+=cur;
                    cur++;
                }else if(p[i].first<cur){
                    j+=p[i].first;
                }else if(p[i].first>cur){
                    j+=cur;
                    cur++;
                }
            }
        }
        
        cout << j << "\n";
    }
    
    return 0;
}