Expert2700

Asesino (Hard Version)

Codeforces
View on Codeforces
Time: 2.5 secondsMemory: 256 megabytes

This is the hard version of the problem. In this version, you must use the minimum number of queries possible. You can make hacks only if both versions of the problem are solved.

This is an interactive problem .

It is a tradition in Mexico's national IOI trainings to play the game " Asesino ", which is similar to " Among Us " or " Mafia ".

Today, n players, numbered from 1 to n, will play " Asesino " with the following three roles:

Each player will be assigned a role in the game. There will be exactly one Impostor but there can be any (possible zero) number of Knights and Knaves.

As the game moderator, you have accidentally forgotten the roles of everyone, but you need to determine the player who is the Impostor.

To determine the Impostor, you will ask some questions. In each question, you will pick two players i and j (1 ≤ i, j ≤ n; i ≠ j) and ask if player i thinks that player j is a Knight. The results of the question is shown in the table below.

Find the Impostor in the minimum number of queries possible. That is, let f(n) be the minimum integer such that for n players, there exists a strategy that can determine the Impostor using at most f(n) questions. Then, you should use at most f(n) questions to determine the Impostor.

Note: the grader is adaptive: the roles of the players are not fixed in the beginning and may change depending on your questions. However, it is guaranteed that there exists an assignment of roles that is consistent with all previously asked questions under the constraints of this problem.

Input

The first line of input contains a single integer t (1 ≤ t ≤ 10^3) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (3 ≤ n ≤ 10^5) — the number of people playing the game.It is guaranteed that the sum of n over all test cases does not exceed 10^5.

Examples

Input
2 7 1 0 0 1 1 0 0 4 0 1 1 1
Output
? 1 3 ? 7 6 ? 2 5 ? 6 2 ? 4 5 ? 4 6 ? 1 4 ! 4 ? 1 2 ? 2 3 ? 3 4 ? 4 1 ! 3

Note

Note that the example test cases do not represent an optimal strategy for asking questions and are only shown for the sake of demonstrating the interaction format. Specifically, we cannot determine which player is the Impostor from the questions asked in the examples.In the first test case of the example, players at indices 2 and 6 are Knights, players at indices 1, 3, 5, and 7 are Knaves, and the Impostor is at index 4. The following is an explanation of the questions asked: In the first query, player i is a Knave and player j is a Knave. The answer is "yes" since Knaves always lie. In the second query, player i is a Knave and player j is a Knight. The answer is "no" since Knaves always lie. In the third query, player i is a Knight and player j is a Knave. The answer is "no" since Knights always tell the truth. In the fourth query, player i is a Knight and player j is a Knight. The answer is "yes" since Knights always tell the truth. In the fifth query, player i is a Impostor and player j is a Knave. The answer is "yes" since the Impostor always lies. In the sixth query, player i is a Impostor and player j is a Knight. The answer is "no" since the Impostor always lies. In the seventh query, player i is a Knave and player j is a Impostor. The answer is "no" since Knaves always lie and Knaves thinks that the Impostor is a Knight.

constructive algorithmsdpinteractive

Discussion

(0)
Sign in to join the discussion.
Loading editor...
0 online