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
#include<bits/stdc++.h>

#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<pii>
#define vb vector<bool>
#define vvb vector<vb>
#define vl vector<ll>
#define endl '\n'
#define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define ll long long
#define f first
#define s second

using namespace std;

int n;
string st;
bool senko = true;

int main()
{
    turbo
    int q; cin >> q;
    while(q--)
    {
        cin >> n >> st;
        priority_queue<pair<int, bool>> se;
        int pom = 0;
        vi v;
        for(auto it : st)
        {
            if(it == '0')
                pom++;
            else 
                if(pom != 0)
                {
                    v.pb(pom);
                    pom = 0;
                }
        }
        if(pom)
            v.pb(pom);
        if(v.empty())
        {
            cout << n << endl;
            continue;
        }
        if(v.size() == 1)
        {
            if(st.back() == '0' or st[0] == '0')
                se.push({v[0]*2, senko});
            else se.push({v[0], !senko});
        }
        if(v.size() > 1)
        {
            if(st[0] == '0') se.push({v[0]*2, senko});
            else se.push({v[0], !senko});
            if(st.back() == '0') se.push({v.back()*2, senko});
            else se.push({v.back(), !senko});           
        }
        for(int i = 1; i < (int)v.size()-1; i++)
            se.push({v[i], !senko});
        int res = 0, covid = 0;
        while(!se.empty())
        {
            auto it = se.top();
            se.pop();
            if(it.s == senko)
            {
                if(it.f/2-covid > 0)
                {
                    res+=it.f/2-covid;
                    covid++;
                }
                else break;
            }
            else
            {
                if(it.f-covid*2 > 0)
                {
                    se.push({(it.f-1-covid)*2, senko});
                    res++;
                    covid++;
                }
                else break;
            }
        }
        cout << n-res << endl;
    }
    return 0;
}