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

#define DEBUG if(0)
#define COUT cout << "\e[36m"
#define ENDL "\e[39m" << endl
#define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] "

using namespace std;

typedef long long LL;


int infected(int time, vector<int>& segments)
{
    int ret = 0;
    for(auto segment : segments)
    {
        int org = segment;
        segment -= 2*time;
        segment = max(0, segment);
        int just_infected = org-segment;
        if (segment > 0)
        {
            segment--; //vaccine
            time++;
        }
        if(segment > 0)
        {
            segment--;
            just_infected++;
        }
        if(segment > 0)
        {
            time++; //vaccine
        }
        ret += just_infected;

    }
    return ret;
}

int solve()
{
    int n;
    cin >> n;
    int left_healthy = 0, right_healthy = 0;
    int initial_infected = 0;
    vector<int> segments = {0};
    {
        int i = 0;
        for (i = 0; i < n; ++i)
        {
            char ch;
            cin >> ch;
            if(ch == '0')
            {
                left_healthy++;
            }
            else
            {
                initial_infected++;
                i++;
                break;
            }
        }

        for (; i < n; ++i)
        {
            char ch;
            cin >> ch;
            if(ch == '0')
            {
                segments.back()++;
            }
            else
            {
                if(segments.back() > 0)
                {
                    segments.push_back(0);
                }
                initial_infected++;
            }
        }

        right_healthy = segments.back();
        segments.pop_back();

        sort(segments.rbegin(), segments.rend());
    }

    DEBUG
    {
        COUT << VAR(left_healthy);
        for(auto el : segments)
        {
            COUT << el << " ";
        }
        COUT << VAR(right_healthy) << ENDL;
    }

    int ans = 0;
    ans = initial_infected+ min(min(
            left_healthy+right_healthy+infected(0, segments),
            min(left_healthy, right_healthy) + infected(1, segments)),
            1+infected(2, segments));

    return ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int z;
    cin >> z;
    while(z--)
    {
        cout << solve() << "\n";
    }
}