CF2176B.Optimal Shifts

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Optimal Shifts

You are given a binary string s1s2sns_1s_2 \ldots s_n, containing at least one 1. You want to obtain a binary string of the same length, consisting only of 1s. To do this, you can perform the following operation any number of times:

Choose a number dd (1dn1 \le d \le n) and consider the string tt as a cyclic right shift of the string ss by dd, or, more formally, $t = s_{n - d + 1} \ldots s_{n}s_{1} \ldots s_{n - d}$. After that, for all jj for which tj=1t_j = 1, perform sj:=1s_j := 1. The described operation costs dd coins, where dd is the chosen shift amount.

Note that the positions jj in the string ss, where initially sj=1s_j=1, remain equal to 11 even if tj=0t_j=0.

You need to determine the minimum number of coins that can be spent so that the string ss consists only of 1s after all operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains the number nn (1n21051 \le n \le 2 \cdot 10^5) — the size of the given binary string.

The second line of each test case contains a binary string of length nn, each element of which is either 0 or 1.

It is guaranteed that at least one character in each string is equal to 1.

It is guaranteed that the sum of nn across all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output the answer to it — the minimum possible number of coins that you can spend to make all characters in the string equal to 1.

Note

Consider the third example, where s=s = "0110". In this case, it is optimal to choose d=2d = 2, then t=t = "1001". After that, at positions j=1j = 1 and j=4j = 4, sjs_j will be replaced with 1, resulting in the string ss consisting of all ones. Note that the cost of this operation is d=2d = 2.

Samples

5
1
1
3
101
4
0110
11
10101010100
2
11
0
1
2
2
0

在线编程 IDE

建议全屏模式获得最佳体验