Roads in E City
Contest: Codeforces Round 866 (Div. 1)
Difficulty rating: 3200
Tags: interactive, math, probabilities, trees

This is an interactive problem.

As is well known, the city "E" has never had its roads repaired in its a thousand and a half years old history. And only recently the city administration repaired some of them.

It is known that in total in the city "E" there are $$$n$$$ intersections and $$$m$$$ roads, which can be used in both directions, numbered with integers from $$$1$$$ to $$$m$$$. The $$$i$$$-th road connects intersections with numbers $$$a_i$$$ and $$$b_i$$$.

Among all $$$m$$$ roads, some subset of the roads has been repaired, but you do not know which one. The only information you could get from the city's road services is that you can get from any intersection to any other intersection by driving only on the roads that have been repaired.

You are a young entrepreneur, and decided to organize a delivery service of fresh raw meat in the city "E" (in this city such meat is called "steaks", it is very popular among the locals). You have already recruited a staff of couriers, but the couriers are willing to travel only on repaired roads. Now you have to find out which roads have already been repaired.

The city administration has given you the city for a period of time, so you can make different queries of one of three types:

1. Block the road with the number $$$x$$$. In this case, movement on the road for couriers will be forbidden. Initially all roads are unblocked.
2. Unblock the road with the number $$$x$$$. In this case, couriers will be able to move on the road $$$x$$$ if it is repaired.
3. Try to deliver the order to the intersection with the number $$$y$$$. In this case, one of your couriers will start moving from intersection with number $$$s$$$ you don't know and deliver the order to intersection with number $$$y$$$ if there is a path on unblocked repaired roads from intersection $$$s$$$ to intersection $$$y$$$. It is guaranteed that intersection $$$s$$$ will be chosen beforehand.

Unfortunately, the city is placed at your complete disposal for a short period of time, so you can make no more than $$$100 \cdot m$$$ requests.

Input format:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1\,000$$$) — the number of test cases. The description of test cases follows.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2\,000$$$, $$$n - 1 \le m \le 2\,000$$$) —the number of intersections and roads in the city "E".

Each of the following $$$m$$$ lines describes one road. The $$$i$$$-th of these lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le n$$$) — the ends of the $$$i$$$-th road. It is guaranteed that no road connects the city to itself, while it is possible that there are several roads between a pair of different intersections.

It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2\,000$$$.

Interaction format:
Once you have read the description of the test case, you can make queries. Queries can be of three types:

1. "- $$$x$$$" ($$$1 \le x \le m$$$). In this case the road with the number $$$x$$$ is blocked if it has not already been blocked.
2. "+ $$$x$$$" ($$$1 \le x \le m$$$). In this case the road with the number $$$x$$$ is unblocked. Note that road $$$x$$$ must be blocked beforehand. All roads are initially unblocked.
3. "? $$$y$$$" ($$$1 \le y \le n$$$). In this case the jury program chooses some city $$$s$$$. If you can get from town $$$s$$$ to town $$$y$$$ by unblocked repaired roads, the jury program will output $$$1$$$, otherwise the jury program will output $$$0$$$. Note that city $$$s$$$ will be selected before getting information about city $$$y$$$, but your previous requests may be taken into account when selecting city $$$s$$$.

In total, you can make no more than $$$100 \cdot m$$$ queries for each set of input data.

After you have found all repaired roads, output "! $$$c_1,\ c_2,\ c_3,\ \ldots,\ c_m$$$", where $$$c_i$$$ is $$$1$$$ if road $$$i$$$ is repaired, and $$$0$$$ if road is not repaired. This output will not count in the total number of queries. The jury program will output $$$1$$$ if your answer is correct, and $$$0$$$ if the answer is not correct. If you received $$$0$$$, your program must terminate immediately to receive a Wrong Answer verdict. Otherwise you can get any verdict, because the program will continue reading from the closed stream. If you read $$$1$$$, move on to the next test case, or terminate the program if there is none.

Note that you do not have to unblock all roads before outputting the answer. It is guaranteed that all repaired roads are fixed initially and will not be changed by the jury program depending on queries.

After outputting a query or the answer do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

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

You can't do hacks on this problem.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first test case, road $$$1$$$ was repaired, while road $$$2$$$ was not. For the first delivery request, intersection $$$1$$$ was selected as $$$s$$$, and the path from intersection $$$1$$$ to $$$1$$$ exists. For the second delivery request, intersection $$$1$$$ was selected as $$$s$$$. Since the only repaired road was blocked, there was no path between intersections $$$1$$$ and $$$2$$$. For the third delivery request, intersection $$$2$$$ was selected as $$$s$$$, the path between intersections $$$2$$$ and $$$1$$$ exists along road $$$1$$$, which is repaired and unblocked.

In the second test case, intersections $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$2$$$, $$$3$$$, $$$1$$$ were selected as starting intersections for delivery requests.

Example input:
2
2 2
1 2
2 1


1

0



1

1
3 3
1 2
2 3
3 1


1

1


1

0


1

1

1

1

Example output:
- 1
? 1

? 2

- 2
+ 1
? 1

! 1 0





- 1
? 2

? 1

- 2
? 3

? 3

+ 1
? 3

? 2

? 1

! 1 1 1

Editorial:
Let's consider city as a graph. We will call an edge good if the road is repaired, and bad if the road is not repaired.
Since we know that we can reach any vertex from any other vertex using good edges, we will find the minimum spanning tree of the graph built on good edges. To do this, we will iterate through the edges one by one and check if the graph remains connected by good edges after removing that edge. If the graph remains connected, we will remove the edge. After this actions, only the good edges that form the minimum spanning tree of the graph will remain.
Let's find out how to check if the graph remains connected after removing an edge. Suppose we are trying to remove an edge between vertices $$$A$$$ and $$$B$$$. If the graph remains connected after removing this edge, then the answer to any query will be $$$1$$$. Otherwise, the graph will be divided into 2 connected components. Let the starting vertex for the next query be $$$S$$$. This vertex will be in one of the two connected components. If we choose a random vertex from $$$A$$$ and $$$B$$$ as the finishing vertex, there will be no path between the starting and finishing vertices using good edges with probability $$$1/2$$$. Therefore, if we make $$$45$$$ such queries, then with probability $$$1/2^{45}$$$, start and finish will be in same components for all queries. In the remaining cases, there will be no path at least in one query, which will indicate that the graph will no longer be connected by good edges after removing the edge.
This way, we can obtain the minimum spanning tree in $$$m \cdot 47$$$ queries.
After obtaining the minimum spanning tree, we will check if all other edges are good. Let's check the edge between vertices $$$C$$$ and $$$D$$$. We can find a path between vertices $$$C$$$ and $$$D$$$ in the minimum spanning tree, remove any edge from it, and then vertices $$$C$$$ and $$$D$$$ will be in different connected components. After that, we will add the edge $$$CD$$$, and using an algorithm similar to the previous one, we will check if the graph is connected by good edges. To do this, we will ask about a random vertex $$$C$$$ or $$$D$$$ 45 times.
This way, we can find all other good edges in $$$m \cdot 49$$$ queries.