CF1829C.Mr. Perfectly Fine

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

Mr. Perfectly Fine

Victor wants to become "Mr. Perfectly Fine". For that, he needs to acquire a certain set of skills. More precisely, he has 22 skills he needs to acquire.

Victor has nn books. Reading book ii takes him mim_i minutes and will give him some (possibly none) of the required two skills, represented by a binary string of length 22.

What is the minimum amount of time required so that Victor acquires all of the two skills?

Input

The input consists of multiple test cases. The first line contains an integer tt (1t10001 \leq t \leq 1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (1n21051 \leq n \leq 2 \cdot 10^5) — the number of books available.

Then nn lines follow. Line ii contains a positive integer mim_i (1mi21051 \leq m_i \leq 2 \cdot 10^5) and a binary string of length 22, where si1=1s_{i1} = 1 if reading book ii acquires Victor skill 11, and si1=0s_{i1} = 0 otherwise, and si2=1s_{i2} = 1 if reading book ii acquires Victor skill 22, and si2=0s_{i2} = 0 otherwise.

It is guaranteed that the sum of nn over all test cases doesn't exceed 21052 \cdot 10^5.

Output

For each test case, output a single integer denoting the minimum amount of minutes required for Victor to obtain both needed skills and 1-1 in case it's impossible to obtain the two skills after reading any amount of books.

Note

In the first test case, we can use books 22 and 33, with a total amount of minutes spent equal to 3+4=73 + 4 = 7.

In the second test case, we can use the books 11 and 44, with a total amount of minutes spent equal to 3+2=53 + 2 = 5.

In the third test case, we have only one option and that is reading book 11 for a total amount of minutes spent equal to 55.

Samples

6
4
2 00
3 10
4 01
4 00
5
3 01
3 01
5 01
2 10
9 10
1
5 11
3
9 11
8 01
7 10
6
4 01
6 01
7 01
8 00
9 01
1 00
4
8 00
9 10
9 11
8 11
7
5
5
9
-1
8

在线编程 IDE

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