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

using namespace std;

int t, n;


int main() {

   cin >> t;
   while(t--) {
      vector<int> oneb, twob;
      
      cin >> n;

      int curb = 0;
      char c;
      for(int i = 0; i < n; i++) {
         cin >> c;
         if(c == '0') {
            curb++;
         } else {
            if(oneb.empty()) {
               oneb.push_back(curb);
            } else {
               if(curb == 0) {
                  continue;
               }
               twob.push_back(curb);
            }
            curb = 0;
         }
      }

      oneb.push_back(curb);

      if(curb == n) {
         cout << 0 << endl;
         continue;
      }

      sort(oneb.begin(), oneb.end());
      sort(twob.begin(), twob.end());

      int steps = 0;
      int respr = 0;
/*
      for(int i = 0; i < oneb.size(); i++) {
         cout << oneb[i] << ' ';
      }
      cout << endl;
      for(int i = 0; i < twob.size(); i++) {
         cout << twob[i] << ' ';
      }
      cout << endl;
*/
      for(;; steps++) {
         int v1 = oneb.empty() ? -9 : oneb.back() - steps;
         int v2 = twob.empty() ? -9 : twob.back() - 2 * steps;
         if(v1 < 1 && v2 < 1) {
            break;
         }
         if(v1 > 0 && v1 + 3 > v2) {
            respr += v1;
            oneb.pop_back();        
         } else if(v2 > 0) {
            respr++;
            if(v2 > 1) {
               oneb.push_back(v2 - 1 + steps);
            } /*
            respr += v2 - 1;
            steps++;
            twob.pop_back();
            */
            twob.pop_back();
         } else {
            break;
         }
      }
      cout << n - respr << endl;
      /*
      for(;; steps++) {
         int k = twob.back() - 2 * steps;
         if(k == 1) {
            respr++;
            twob.pop_back();
            continue;
         }
         if(k < 2) {
            break;
         }
         respr += k - 1;
         steps++;
         twob.pop_back();
      }

      for(;; steps++) {
         int k = oneb.back() - steps;
         if(k < 1) {
            break;
         }
         respr += k;
         oneb.pop_back();
      }      cout << n - respr << endl;
      continue;
      for(int i = 0; i < oneb.size(); i++) {
         cout << oneb[i] << ' ';
      }
      cout << endl;
      for(int i = 0; i < twob.size(); i++) {
         cout << twob[i] << ' ';
      }
      cout << endl;

      */
   }


}