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

#define FOR(i, n) for (int i = 0; i < int(n); ++i)
#define FO(i, a, b) for (int i = (a); i < int(b); ++i)
#define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((b) < (a) ? (a) : (b))

#define REMIN(a, b) ((a) = min(a, b))
#define REMAX(a, b) ((a) = max(a, b))

#define ALL(c) (c).begin(), (c).end()

#define SQR(x) ((x) * (x))

//

void tc() {
  int n;
  cin >> n;

  string s;
  cin >> s;

  int last = -1;

  vector<int> lens;
  vector<int> lens2;

  FOR(i, (int)s.size()) {
    if (s[i] == '1') {
      if (last == -1 && i > 0) lens2.push_back(i);

      if (last < i - 1 && last >= 0) {
        lens.push_back(i - last - 1);
      }

      last = i;
    }
  }

  if (s[(int)s.size() - 1] == '0') OF(i, 0, (int)s.size()) {
      if (s[i] == '1') {
        lens2.push_back(n - i - 1);
        break;
      }
    }

  if (lens.empty() && lens2.empty()) {
    if (s[0] == '0')
      cout << 0 << endl;
    else
      cout << n << endl;
    return;
  }

  sort(ALL(lens));
  sort(ALL(lens2));

  // cerr << "LENS ";
  // for (auto& len : lens) cerr << len << " ";
  // cerr << endl;

  // cerr << "LENS2 ";
  // for (auto& len : lens2) cerr << len << " ";
  // cerr << endl;

  int time = 0;
  int saved = 0;

  for (;;) {
    if ((lens.size() && lens2.size() && lens.back() > lens2.back() * 2) ||
        (lens.size() && !lens2.size())) {
      int len = lens.back();
      lens.pop_back();
      len -= time * 2;
      if (len <= 0) break;

      // cerr << "save " << len - 1 << endl;
      if (len >= 3) {
        saved += len - 1;
        time += 2;
      } else {
        saved += 1;
        time += 1;
      }
    } else if (lens2.size()) {
      int len = lens2.back();
      lens2.pop_back();
      len -= time;
      if (len <= 0) break;

      // cerr << "save2 " << len << endl;
      saved += len;
      time += 1;
    } else
      break;
  }

  cout << n - saved << endl;
}

int main() {
  ios_base::sync_with_stdio(0);

  int cases;

  cin >> cases;

  FOR(i, cases) tc();

  return 0;
}