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

void test()
{
    long long n; cin >> n;
    char z; long long akt = 0;
    vector<long long> v;
    long long lewa = 0, prawa = 0;
    for(long long i = 1; i <= n; i++)
    {
        cin >> z;
        if(z == '0')
        {
            if(i == 1) lewa = 1;
            if(i == n) prawa = 1;
            akt++;
        }
        else if(akt != 0)
        {
            v.push_back(akt);
            akt = 0;
        }
    }
    if(akt != 0) v.push_back(akt);
    if(lewa)
    {
        lewa = v[0];
        v[0] = 0;
    }
    if(prawa and v.back())
    {
        prawa = v.back();
        v.back() = 0;
    }
    sort(v.rbegin(), v.rend());

    long long day = 0;

    for(long long i = 0; i < v.size(); i++)
    {

        if(lewa - day >= 3)
        {
            n -= lewa - day;
            lewa = 0;
            day++;
            i--;
            continue;
        }
        if(prawa - day >= 3)
        {
            n -= prawa - day;
            prawa = 0;
            day++;
            i--;
            continue;
        }

        if(lewa - day >= v[i] - (2 * day + 1))
            if(lewa - day > 0)
            {
                n -= lewa - day;
                day++;
                lewa = 0;
                i--;
                continue;
            }
        if(prawa - day >= v[i] - (2 * day + 1))
            if(prawa - day > 0)
            {
                n -= prawa - day;
                prawa = 0;
                day++;
                i--;
                continue;
            }
        if(v[i] - (2 * day) > 2)
        {
            n -= v[i] - (2 * day + 1);
            day += 2;
            continue;
        }
        if(v[i] - (2 * day) == 1 or v[i] - (2 * day) == 2)
        {
            day++;
            n--;
            continue;
        }
    }
    cout << n << "\n";
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long long t; cin >> t;
    while(t--)
    {
        test();
    }
}