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

int n, l1, l2, l[100001], res;
bool t[100001];

void in()
{
    for(int i = 1; i <= n; i++)
        l[i] = 0;
    res = 0;

    scanf("%d\n", &n);

    for(int i = 0; i < n; i++)
        t[i] = getchar() - '0';

    l1 = 0;
    while(l1 < n && t[l1] == 0)
        l1++;
    l2 = 0;
    while(l2 < n && t[n - 1 - l2] == 0)
        l2++;
}

int calcf(int ct)
{
    int ret = 0;

    for(int i = n; i > 0; i--)
    {
        for(int j = 0; j < l[i]; j++)
        {
            int val = i - ct;

            if(val <= 0)
                return ret;

            if(val <= 2)
            {
                ret += 1;
                ct += 2;
            }
            else
            {
                ret += val - 1;
                ct += 4;
            }
        }
    }

    return ret;
}

void calc()
{
    if(l1 == n)
    {
        res = l1;
        return;
    }

    for(int i = l1 + 1, ct = 0; i < n; i++)
    {
        if(t[i] == 0)
            ct++;
        else
        {
            l[ct]++;
            ct = 0;
        }
    }

    if(l1 < l2)
        swap(l1, l2);

    res = calcf(0);
    res = max(res, calcf(2) + l1);
    res = max(res, calcf(4) + l1 + l2 - 1);
}

void out()
{
    printf("%d\n", n - res);
}

int main()
{
    int q;
    scanf("%d", &q);

    while(q--)
    {
        in();
        calc();
        out();
    }
}