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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

int t,n;

int size1, size2;
int zeros;

vector <int> results;

struct range{
	int size;
	int score;
};

bool desc (range i, range j) {
	size1 = i.size;
	size2 = j.size;

	if(i.score == 1){
		size1 *= 2;
	}
	if(j.score == 1){
		size2 *= 2;
	}
	return size1 > size2;
	// printf("%d\n", i.size);
	// printf("%d\n", i.score);
	// printf("%d\n", j.size);
	// printf("%d\n", j.score);
	// i.size - i.score > j.size - j.score;
	// if(i.score == 1 && j.score == 1){
	// 	i.size > j.size;
	// }else if(i.score == 1){
	// 	i.size * 2 > j.size;
	// }else if(j.score == 1){
	// 	i.size > j.size * 2;
	// }else{
	// 	i.size > j.size;
	// }
	// if(i.size == j.size){
	// 	return i.score < j.score;
	// }else if(i.size - i.score == j.size - j.score){
	// 	return i.score < j.score;
	// }else{
	// 	return i.size > j.size;
	// }
}

vector <range> ranges;


string input;

void count_result(){
	// printf("----------\n");
	// printf("CHECK\n");
	sort(ranges.begin(), ranges.end(), desc);
	// for(int i=0;i<ranges.size();++i){
	// 	printf("RANGE: [%d, %d]\n",ranges[i].size, ranges[i].score);
	// }
	// printf("---\n");
	// printf("CHECK\n");

	range curr;
	int moves = 0;
	int saved = 0;
	int curr_size, curr_score;
	for(int i=0;i<ranges.size();++i){
		curr = ranges[i];
		if(curr.size > curr.score * moves){
			curr_size = curr.size - curr.score * moves;
			if(curr_size == 1){
				saved += 1;
				moves += 1;
				// printf("-> ADDING %d [%d, %d, %d, %d], CURR: %d\n", 1, curr_size, 1, curr.size, curr.score, saved);
			}else if(curr.score == 1){
				saved += curr_size;
				moves += 1;
				// printf("-> ADDING %d [%d, %d, %d, %d], CURR: %d\n", curr_size, curr_size, 1, curr.size, curr.score, saved);
			}else{
				saved += curr_size - 1;
				moves += 2;
				// printf("-> ADDING %d [%d, %d, %d, %d], CURR: %d\n", curr_size - 1, curr_size, 2, curr.size, curr.score, saved);
			}
			// moves += curr.score;
		}else{
			break;
		}

		// printf("RANGE: [%d,%d]\n", curr.size, curr.score);
	}
	int res = n-saved;
	if(ranges.size() == 0){
		res = 0;
	}
	printf("%d\n",res);
}

void test_case(){
	scanf("%d",&n);
	zeros = 0;
	int begin_range = -1;
	int score = 0;
	char d;
	cin >> input;
	ranges.clear();
	for(int i=0;i<n;++i){
		d = input[i];
		if(d == '0'){
			zeros++;
			if(begin_range == -1){
				begin_range = i;
				if(i > 0){
					score++;
				}
			}
		}else{
			if(begin_range != -1){
				score++;
				// printf("NEW RANGE - [%d,%d], score: %d\n",begin_range,i,score);
				range new_range = {i-begin_range, score};
				ranges.push_back(new_range);
				begin_range = -1;
				score = 0;
			}
		}
	}

	if(begin_range != -1){
		// printf("NEW RANGE - [%d,%d], score: %d\n",begin_range,n-1,score);
		range new_range = {n-begin_range, score};
		if(begin_range > 0){
			ranges.push_back(new_range);
		}
		begin_range = -1;
		score = 0;
	}

	if(zeros == n){
		printf("%d\n",0);
	}else if(zeros == 0){
		printf("%d\n",n);
	}else{
		count_result();
	}
	


}

int main(){
	scanf("%d", &t);

	while(t--){
		test_case();
		// printf("---\n");
	}
	return 0;
}