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
#include <stdio.h>
#include <bits/stdc++.h>
#define D(x) //x

int Spaces[2000000];
int SpaceCount;
int LeftTail = 0;
int RightTail = 0;

void CountSpaces(int& saved, int& loss) {
	for(int i=0; i < SpaceCount; i++) {
		int candidate = Spaces[i] - (2 * loss);
		if(candidate >= 2)
			candidate--;
		if(candidate <= 0)
		 	break;
		saved += candidate;
		loss += 2;
	}
}

void CountTail(int& saved, int& loss, int tail) {
	int candidate = tail - loss;
	if(candidate <= 0)
	 	return;
	saved += candidate;
	loss++;
}

int Solve1() {
	int saved = 0;
	int loss = 0;
	CountTail(saved, loss, LeftTail);
	CountTail(saved, loss, RightTail);
	CountSpaces(saved, loss);
	return saved;
}

int Solve2() {
	int saved = 0;
	int loss = 0;
	CountSpaces(saved, loss);
	CountTail(saved, loss, LeftTail);
	CountTail(saved, loss, RightTail);
	return saved;
}

int Solve3() {
	int saved = 0;
	int loss = 0;
	CountTail(saved, loss, LeftTail);
	CountSpaces(saved, loss);
	CountTail(saved, loss, RightTail);
	return saved;
}

int Solve4() {
	int saved = 0;
	int loss = 0;
	CountTail(saved, loss, RightTail);
	CountSpaces(saved, loss);
	CountTail(saved, loss, LeftTail);
	return saved;
}

int max(int x, int y ) {
	return x > y ? x : y;
}

int main() {
	int tc;
	scanf("%d ", &tc);
	for(int i=0; i<tc; i++) {
		int n;
		scanf("%d ", &n);
		int sIdx = 0;
		Spaces[0] = 0;
		bool prevOne = true;
		char first;
		char last;
		for(int i=0; i<n; i++) {
			char c;
			scanf("%c ", &c);
			if( i == 0 )
				first = c;
			else if ( i == n - 1)
				last = c;

			if(c == '1') {
				if( prevOne == false ) {
					sIdx++;
					Spaces[sIdx] = 0;
				}
				prevOne = true;
			}
			else {
				prevOne = false;
				Spaces[sIdx]++;
			}
		}
		if( prevOne == false )
			sIdx++;

		if(sIdx == 1) {
			if(first == '1' && last == '1') {
				if(Spaces[0] > 1)
					Spaces[0]--;
				printf("%d\n", n - Spaces[0]);
			}
			else
				printf("%d\n", n - Spaces[0]);
			continue;
		}

		LeftTail = 0;
		RightTail = 0;
		if(last == '0') {
			RightTail = Spaces[sIdx - 1];
			sIdx--;
		}
		if(first == '0') {
			LeftTail = Spaces[0];
			Spaces[0] = Spaces[sIdx - 1];
			sIdx--;
		}

		std::sort(&Spaces[0], &Spaces[sIdx], std::greater<int>());

		D(printf("L %d  R %d\n", LeftTail, RightTail);
		for(int i=0; i < sIdx; i++) {
			printf("%d ", Spaces[i]);
		}
		printf("\n");)

		SpaceCount = sIdx;
		int r1 = Solve1();
		int r2 = Solve2();
		int r3 = Solve3();
		int r4 = Solve4();
		D(printf("results: %d %d %d %d\n", r1, r2, r3, r4);)
		int r = max(max(r1, r2), max(r3, r4));
		printf("%d\n", n - r);
	}
	return 0;
}