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
#include <bits/stdc++.h>
#define dbg(x) " [" << #x << ": " << (x) << "] "
using namespace std;
template<typename A,typename B>
ostream& operator<<(ostream& out,const pair<A,B>& p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template<typename T>
ostream& operator<<(ostream& out,const vector<T>& c) {
    out << "{";
    for(auto it = c.begin(); it != c.end(); it++) {
        if(it != c.begin()) out << ", ";
        out << *it;
    }
    return out << "}";
}
template<typename T>
ostream& operator<<(ostream& out,const multiset<T>& c) {
    out << "{";
    for(auto it = c.begin(); it != c.end(); it++) {
        if(it != c.begin()) out << ", ";
        out << *it;
    }
    return out << "}";
}
void solve() {
    int n;
    cin >> n;
    string ss;
    cin >> ss;
    vector<multiset<int>> s(3);
    int l = 0;
    bool f = true;
    for(int i = 0; i < n; i++) {
        if(ss[i] == '1') {
            if(l) {
                s[f ? 1 : 2].insert(l);
                l = 0;
            }
            f = false;
        } else if(ss[i] == '0') {
            l++;
        }
    }
    if(l) {
        s[1].insert(l);
    }
    int t = 0;
    int ans = 0;
    while(s[1].size() || s[2].size()) {
        for(int id = 1; id <= 2; id++) {
            while(s[id].size()) {
                auto it = s[id].begin();
                if(*it - id * t > 0) {
                    break;
                } else {
                    s[id].erase(it);
                }
            }
        }
        int id = 0;
        if(s[1].size() && s[2].size()) {
            int p1 = *prev(s[1].end());
            int p2 = *prev(s[2].end());
            p1 -= t;
            p2 -= 2 * t;
            if(p2 > p1 + 2) {
                id = 2;
            } else {
                id = 1;
            }
        } else if(s[1].size()) {
            id = 1;
        } else if(s[2].size()) {
            id = 2;
        }
        if(!id) {
            break;
        } else {
            int p = *prev(s[id].end());
            s[id].erase(prev(s[id].end()));
            p -= id * t;
            if(id == 1) {
                ans += p;
            } else {
                ans++;
                s[1].insert(p + t - 1);
            }
        }
        t++;
    }
    cout << n - ans << endl;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int testNum = 1;
    cin >> testNum;
    for(int i = 1; i <= testNum; i++) {
        solve();
    }
    return 0;
}