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
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

char s[100010];

void test() {
  int n;
  scanf("%d", &n);
  scanf("%s", s);
  vector<pair<int, int>> x;
  bool start = true;
  int counter = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') counter++;
    if (s[i] == '1') {
      if (counter > 0) {
        if (start) {
          x.push_back(pair(counter * 2, 1));
        } else if (counter <= 2) {
          x.push_back(pair(2, 1));
        } else {
          x.push_back(pair(counter - 1, 2));
        }
      }
      start = false;
      counter = 0;
    }
  }
  if (start) {
    printf("0\n");
    return;
  }
  if (counter > 0) {
    x.push_back(pair(counter*2, 1));
  }
  int xs = x.size();
  sort(x.begin(), x.end());
  for (int i = 0; i < xs/2; ++i) {
    swap(x[i], x[xs-i-1]);
  }

  int result = 0;
  int minus = 0;
  for (int i = 0; i < xs; ++i) {
    int z;
    int m;
    if (x[i].second == 1) {
      z = (x[i].first - minus) / 2;
      m = 1;
    } else {
      z = x[i].first - minus;
      if (z == 0) {
        z = 1;
        m = 1;
      } else {
        m = 2;
      }
    }
    if (z <= 0) break;
    result += z;
    minus += m*2;
  }
  printf("%d\n", n - result);
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    test();
  }
}