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
121
#include<bits/stdc++.h>
using namespace std;
vector<int> v;
vector<int> spec;
void solve()
{
    int a;
    cin>>a;
    string d;
    cin>>d;
    bool czy_1 = false;
    for(auto x:d)
        if(x == '1')
            czy_1 = true;
    if(!czy_1)
    {
        cout<<0<<'\n';
        return ;
    }
    int licz = 0;
    while(!d.empty() && d.back() == '0')
    {
        d.pop_back();
        licz++;
    }
    if(licz != 0)
        spec.push_back(licz);
    reverse(d.begin(),d.end());
    licz = 0;
    while(!d.empty() && d.back() == '0')
    {
        d.pop_back();
        licz++;
    }
    if(licz != 0)
        spec.push_back(licz);
    while(!d.empty())
    {
        while(!d.empty() && d.back() == '1')
            d.pop_back();
        int licz2 = 0;
        if(!d.empty())
        {
            while(!d.empty() && d.back() == '0')
            {
                d.pop_back();
                licz2++;
            }
            v.push_back(licz2);
        }

    }
    int ans = 0;
    /*for(auto x:v)
        cout<<x<<" ";
    cout<<'\n';
    for(auto x:spec)
        cout<<x<<" ";
    cout<<'\n';*/
    sort(spec.begin(),spec.end());
    sort(v.begin(),v.end());
    int act = 0;
    while(!v.empty())
    {
        int pom = v.back();
        if(pom <= 3)
            break;
        v.pop_back();
        pom -= act * 2;

        if(pom <= 2)
        {
            act++;
            ans++;
        }
        else
        {
            act += 2;
            ans += pom-1;
        }
    }
    while(!spec.empty())
    {
        int pom = spec.back();
        spec.pop_back();
        pom -= act;
        if(pom <= 0)
            continue;
        act++;
        ans+=pom;
    }
    while(!v.empty())
    {
        int pom = v.back();
        v.pop_back();
        pom -= act * 2;
        if(pom <= 0)
            continue;
        if(pom <= 2)
        {
            act++;
            ans++;
        }
        else
        {
            act += 2;
            ans += pom-1;
        }
    }
    cout<<a-ans<<'\n';
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--)
        solve();
	return 0;
}