Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Chef and Chhotu are best friends. Chef knows that Chhotu is very intelligent. So, Chef gives Chhotu a simple problem and wants him to solve this problem by today. The problem is as follows.

There are N students in a classroom. The students are sitting in a single row. Chef wants that all the girls should sit in left side of the row and all the boys should sit in right side of the row. You are provided the initial row of the students by N binary integers, where if i-th integer is zero, then it means that at the i-th position, there is a boy, where 1 will mean girl. For example, if the initial arrangement be [0 1 0 1 1], then final arrangement should be [1 1 1 0 0].

In a single second, if a boy finds that a girl is sitting to his immediate right i.e. if a boy is sitting at i^{th} position and a girl is sitting at (i+1)^{th} position then they swap their positions. For example, let the initial arrangement be [0 1 0 1 1]. Boys are sitting at positions 1, 3 (1-based indexing) and girls are sitting at positions 2, 4, 5. In a single second, boy at position 1 swaps his seat with girl at position 2, and similarly boy at position 3 swaps his seat with girl at position 4. So, after one second, the arrangement becomes [1 0 1 0 1]. For detailed explanation, please see the explanation of the sample provided at the end of this statement.Now Chef wants to determine the total time after which all the girls move to the left side of the row and all the boys towards the right side of the row.
Chhotu can solve this problem very easily but today he is very busy. Please help Chhotu to solve this problem.

------ Input ------ 

The first line of the input contains T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains an integer N denoting the number of students in the row.
The second line contains N space-separated integers - A_{1}, A_{2}, ..., A_{N }denoting the initial arrangement of the students.

------ Output ------ 

For each test case, output a single integer on a new line corresponding to the number of seconds required such that all girls are to the left of boys.

------ Constraints ------ 

$1 ≤ T ≤ 10	$
$1 ≤ N ≤ 10^{5}$
$0 ≤ A_{i } ≤ 1$

------ Subtasks ------ 

Subtask #1 (30 points):

$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{3}$

Subtask #2 (70 points):

$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$

------ Example ------ 

Input:

2
10
0 1 1 1 0 1 1 0 1 1
5
0 1 0 1 1

Output:

7
3

------ Explanation ------ 

Test case 1:
Initially, student's arrangement is [0 1 1 1 0 1 1 0 1 1]
After1^{st} second [1 0 1 1 1 0 1 1 0 1]
After 2^{nd} second [1 1 0 1 1 1 0 1 1 0]
After 3^{rd} second [1 1 1 0 1 1 1 0 1 0]
After 4^{th} second [1 1 1 1 0 1 1 1 0 0]
After 5^{th} second [1 1 1 1 1 0 1 1 0 0]
After 6^{th} second [1 1 1 1 1 1 0 1 0 0]
After 7^{th} second [1 1 1 1 1 1 1 0 0 0]
Total time taken = 7 seconds

Test case 2:
Initially, student's arrangement is [0 1 0 1 1]
After 1^{st} second [1 0 1 0 1]
After 2^{nd} second [1 1 0 1 0]
After 3^{rd} second [1 1 1 0 0]
Total time taken = 3 seconds

Source: codechef | Difficulty: VERY_HARD
Tags: Constructive algorithms

Data structures

Ad-hoc


Solutions:
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = c = c1 = c2 = c3 = 0
	for i in range(n):
		if a[i] == 1:
			c += 1
			if i + 1 == c:
				c1 = 0
			else:
				c2 = c2 - c3 + 1 if c2 - c3 + 1 > 0 else 0
				c1 = i + 1 - c + c2
			ans = max(ans, c1)
			c3 = 0
		else:
			c3 += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	total_delay = 0
	delay = 0
	delay_compensation = 0
	c = 1
	ones = 0
	last_one = 0
	first_one = -1
	first_zero = -1
	for i in range(n):
		if a[i] == 1:
			ones += 1
			last_one = i
		elif a[i] == 0 and first_zero == -1:
			first_zero = i
	first_zero = max(0, first_zero)
	for i in range(first_zero, last_one + 1):
		if a[i] == 1:
			first_one = i
			break
	first_one = max(first_one, 0)
	for i in range(first_one, last_one + 1):
		if a[i] == 1:
			total_delay += 1
		elif total_delay > 0:
			total_delay -= 1
	if ones == 0:
		print('0')
	elif ones - 1 == last_one:
		print('0')
	else:
		print(last_one - (ones - 1) + total_delay - 1)


for _ in range(int(input())):
	N = int(input())
	A = input().split()
	counter = 0
	sum = 0
	t = 0
	i = 0
	while i < N:
		if A[i] == '0':
			counter = counter + 1
			t = 1
		elif A[i] == '1' and t == 1:
			if sum < counter:
				sum = counter
			else:
				sum = sum + 1
		i = i + 1
	print(sum)


for _ in range(int(input())):
	el = int(input())
	k = input().split()
	mm = 0
	gp = []
	for i in range(el):
		if k[i] == '1':
			gp.append(i)
	ng = len(gp)
	if ng == len(k):
		print('0')
		continue
	for i in range(len(gp)):
		ng = ng - 1
		m = gp[i] - i
		if m > 0:
			m += ng
		if mm < m:
			mm = m
	print(mm)


cases = int(input())
for i in range(cases):
	lngth = int(input())
	lstinput = input().split(' ')
	(onecount, resistance) = (0, 0)
	(last_resistance, last_position) = (0, 0)
	IsSorted = bool(1)
	for j in range(lngth):
		jth_element = int(lstinput[j])
		if jth_element == 1:
			last_position = j
			last_resistance = resistance
			onecount += 1
			if j > onecount - 1:
				IsSorted = 0
				resistance += 1
		elif resistance != 0:
			resistance -= 1
	if IsSorted:
		print(0)
	else:
		print(last_position - (onecount - 1) + last_resistance)


GIRL = '1'
BOY = '0'

def switches(positions):
	final_girl_positions = [i for (i, p) in enumerate(positions) if p == GIRL]
	num_girls = len(final_girl_positions)
	max_moves = 0
	if num_girls == len(positions):
		return max_moves
	for (i, final_position) in enumerate(final_girl_positions):
		num_girls -= 1
		moves = final_position - i
		if moves:
			moves += num_girls
		if moves > max_moves:
			max_moves = moves
	return max_moves

def start():
	num_test_cases = input()
	for i in range(int(num_test_cases)):
		_ = input()
		positions = input().split()
		print(switches(positions))
start()


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	ar = list(map(int, input().split()))
	(cnt, las, tot) = (0, -1, 0)
	ans = -1
	en = n - 1
	while en >= 0 and ar[en] == 0:
		tot += 1
		en -= 1
	st = 0
	while st < n and ar[st] == 1:
		st += 1
	for i in range(en, st - 1, -1):
		if ar[i] == 0:
			las = i
			tot += 1
			ans = cnt + n - tot - las
			cnt += 1
		else:
			cnt = max(0, cnt - 1)
	if ans != -1:
		print(ans)
	else:
		print(0)