#P1617D2. Too Many Impostors (hard version)

    ID: 1515 Type: RemoteJudge 3000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 9 Uploaded By: Tags>constructive algorithmsimplementationinteractivemath*2400

Too Many Impostors (hard version)

Description

This is an interactive problem. The only difference between the easy and hard version is the limit on number of questions.

There are nn players labelled from 11 to nn. It is guaranteed that nn is a multiple of 33.

Among them, there are kk impostors and nkn-k crewmates. The number of impostors, kk, is not given to you. It is guaranteed that n3<k<2n3\frac{n}{3} < k < \frac{2n}{3}.

In each question, you can choose three distinct integers aa, bb, cc (1a,b,cn1 \le a, b, c \le n) and ask: "Among the players labelled aa, bb and cc, are there more impostors or more crewmates?" You will be given the integer 00 if there are more impostors than crewmates, and 11 otherwise.

Find the number of impostors kk and the indices of players that are impostors after asking at most n+6n+6 questions.

The jury is adaptive, which means the indices of impostors may not be fixed beforehand and can depend on your questions. It is guaranteed that there is at least one set of impostors which fulfills the constraints and the answers to your questions at any time.

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1001 \le t \le 100). Description of the test cases follows.

The first and only line of each test case contains a single integer nn (6n<1046 \le n < 10^4, nn is a multiple of 33) — the number of players.

It is guaranteed that the sum of nn over all test cases does not exceed 21042 \cdot 10^4.

Interaction

For each test case, the interaction starts with reading nn.

Then you are allowed to make at most n+6n+6 questions in the following way:

"? a b c" (1a,b,cn1 \le a, b, c \le n, aa, bb and cc are pairwise distinct).

After each one, you should read an integer rr, which is equal to 00 if there are more impostors than crewmates among players labelled aa, bb and cc, and equal to 11 otherwise.

Answer 1-1 instead of 00 or 11 means that you made an invalid query. Exit immediately after receiving 1-1 and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

When you have found the indices of all impostors, print a single line "! " (without quotes), followed by the number of impostors kk, followed by kk integers representing the indices of the impostors. Please note that you must print all this information on the same line.

After printing the answer, your program must then continue to solve the remaining test cases, or exit if all test cases have been solved.

After printing the queries and answers do not forget to output end of line and flush the output buffer. Otherwise, you will get the Idleness limit exceeded verdict. To do flush use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • Read documentation for other languages.

Hacks

You cannot make hacks in this problem.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1001 \le t \le 100). Description of the test cases follows.

The first and only line of each test case contains a single integer nn (6n<1046 \le n < 10^4, nn is a multiple of 33) — the number of players.

It is guaranteed that the sum of nn over all test cases does not exceed 21042 \cdot 10^4.

Sample Input 1

2
6

0

1

9

1

Sample Output 1

? 1 2 3

? 3 4 5

! 3 4 1 2

? 7 1 9

! 4 2 3 6 8

Note

Explanation for example interaction (note that this example only exists to demonstrate the interaction procedure and does not provide any hint for the solution):

For the first test case:

Question "? 1 2 3" returns 00, so there are more impostors than crewmates among players 11, 22 and 33.

Question "? 3 4 5" returns 11, so there are more crewmates than impostors among players 33, 44 and 55.

Outputting "! 3 4 1 2" means that one has found all the impostors, by some miracle. There are k=3k = 3 impostors. The players who are impostors are players 44, 11 and 22.

For the second test case:

Question "? 7 1 9" returns 11, so there are more crewmates than impostors among players 77, 11 and 99.

Outputting "! 4 2 3 6 8" means that one has found all the impostors, by some miracle. There are k=4k = 4 impostors. The players who are impostors are players 22, 33, 66 and 88.