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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream> 
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;

#define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b))
using ll = long long;
const int INF = 1e9;

using Segment = vector<int>;
using Segments = vector<Segment>;

void print_segments(const Segments& ss) {
    for (auto s : ss) {
        cout << "\t";
        for (auto v : s) {
            cout << v << ", ";
        }
        cout << endl;
    }
}
int solve(Segments ss) {
    int res = 0;
    while (true) {
        //print_segments(ss);
        int best_len = -1;
        int best_seg_i = -1;
        int best_i = -1;
        int best_is_pref_suf = 0;
        for (int i = 0; i < ss.size(); ++i) {
            for (int j = 0; j < ss[i].size(); ++j) {
                if (ss[i][j] == -1) continue;
                int len = ss[i][j];
                int is_pref_suf = j == 0 || j+1 == ss[i].size();
                if (len + 2*is_pref_suf > best_len + 2*best_is_pref_suf) {
                    best_len = len;
                    best_seg_i = i;
                    best_i = j;
                    best_is_pref_suf = is_pref_suf;
                }
            }
        }
        if (best_len == -1) break;
        // cout << best_len << " " << best_seg_i << " " << best_i << endl;
        Segments new_ss;
        for (int i = 0; i < ss.size(); ++i) {
            Segment tmp;
            for (int j = 0; j < ss[i].size(); ++j) {
                if (ss[i][j] == -1) {
                    if (tmp.empty() || tmp.back() != -1) tmp.push_back(-1);
                } else {
                    int len = ss[i][j];
                    int is_pref_suf = j == 0 || j+1 == ss[i].size();
                    if (best_seg_i == i && best_i == j) {
                        if (is_pref_suf) {
                            res += best_len;
                        } else {
                            res += 1;
                            if (tmp.size() > 0) new_ss.push_back(tmp);
                            tmp.clear();
                            int new_len = max(0, len-2);
                            if (new_len > 0) {
                                tmp.push_back(new_len);
                            }
                        }
                    } else {
                        int new_len = max(0, len-(is_pref_suf ? 1 : 2));
                        if (new_len > 0) {
                            tmp.push_back(new_len);
                        }
                    }
                }
            }
            if (tmp.size() > 0) new_ss.push_back(tmp);
        }
        //cout << "! " << res << endl;
        ss = new_ss;
    }
    return res;
}

const int MAX_N = 1e5+10;
char s[MAX_N];
int main() {
    // ios_base::sync_with_stdio(0);
    int t;
    // cin >> t;
    scanf("%d", &t);
    for (int tid = 1; tid <= t; ++tid) {
        int n;
        scanf("%d", &n);
        // cin >> n;
        // string s;
        // cin >> s;
        scanf("%s", s);
        Segment seg;
        /*
        if (tid == 662) {
            cout << n << endl;
            cout << s << endl;
            return 0;
        }
        */
        int run = s[0] == '0';
        if (s[0] == '1') {
            seg.push_back(-1);
        }
        for (int i = 1; i < n; ++i) {
            if (s[i] == '1' && run) {
                seg.push_back(run);
                run = 0;
                seg.push_back(-1);
            } else if (s[i] == '0') {
                run++;
            }
        }
        if (run > 0) {
            seg.push_back(run);
        }
        auto res = solve({seg});
        // cout << n-res << endl;
        printf("%d\n", n-res);
    }
    return 0;
}