Good News and Bad News (Large)

Assign a nonzero integer to each directed edge so every vertex's outgoing sum equals its incoming sum, exactly as built by a prescribed DFS cycle-circulation procedure.

Hard8GraphDFSImplementationSimulationNo attempts yetTime limit5sMemory limit512 MB

Problem

You want to get your FF friends to share some news. You know your friends well, so you know which of them can talk to which. There are PP one way relationships, and the ii-th of them is an ordered pair (Ai,Bi)(A_i, B_i) meaning that friend AiA_i can talk to friend BiB_i. It does not mean that friend BiB_i can talk to friend AiA_i, although another ordered pair may state that separately.

For every given ordered pair (Ai,Bi)(A_i, B_i), friend AiA_i has to deliver one piece of news to friend BiB_i. A piece of news is one integer. Its absolute value is the magnitude of the news, and its sign gives the type of the news. The integer cannot be 0 (there would be no news at all), and its absolute value cannot exceed F2F^2 (the news would be too exciting). Different ordered pairs may get different integers.

Because you care about your friends' feelings, for each friend the sum of the values of all news that friend delivers has to equal the sum of the values of all news that friend receives. If a friend delivers no news, that sum is 0. If a friend receives no news, that sum is 0.

Find a set of news values that obeys every rule, or determine that none exists.

Input

The first line contains the number of test cases TT. TT test cases follow. The first line of each test case contains two integers FF and PP, the number of friends and the number of ordered pairs of friends. Each of the next PP lines contains two different integers AiA_i and BiB_i, meaning that friend AiA_i can talk to friend BiB_i. Friends are numbered from 1 to FF.

Limits

  • 1T1001 \le T \le 100
  • 2F10002 \le F \le 1000
  • 1P20001 \le P \le 2000
  • 1AiF1 \le A_i \le F, 1BiF1 \le B_i \le F, AiBiA_i \ne B_i for all ii (a friend does not talk to themselves)
  • (Ai,Bi)(Aj,Bj)(A_i, B_i) \ne (A_j, B_j) for all iji \ne j (no ordered pair repeats inside one test case)

Output

For each test case, print one line containing Case #x: y, where xx is the test case number starting from 1 and yy is one of the following.

If no assignment obeys the rules, yy is IMPOSSIBLE.

Otherwise yy is PP integers separated by single spaces, and the ii-th of them is the news value that friend AiA_i delivers to friend BiB_i. Many assignments obey the rules, so print exactly the one that this procedure builds.

  1. Build an undirected multigraph whose vertices are friends 1 to FF and whose ii-th edge joins AiA_i and BiB_i. Edge numbers follow the input order.
  2. Run a depth first search over that multigraph. Start at the smallest friend that has not been visited yet, and repeat until every friend is visited. At a friend, examine the incident edges in increasing order of edge number, and skip the single edge that was used to enter that friend. An edge that leads to a friend not visited yet becomes a tree edge of the search forest, and every other edge joins some friend to one of its ancestors and is a back edge.
  3. Let back edge ii join friend uu and its ancestor ww. Send one unit of news around the cycle formed by edge ii and the forest path that goes down from ww to uu. The unit travels from uu to ww along edge ii, and from ww toward uu along the forest path.
  4. Once every back edge is handled, the value of edge ii is the total amount that travelled along it, counted as positive when it travelled from AiA_i to BiB_i and as negative when it travelled from BiB_i to AiA_i.

Whenever an assignment that obeys the rules exists, the values this procedure builds are nonzero and lie inside [F2,F2][-F^2, F^2].

Notes

A friend who neither delivers nor receives any news still obeys the rules.

If some friend only receives news and can talk to nobody, the test case is IMPOSSIBLE. A news value cannot be 0, so the sum received is nonzero while the sum delivered is 0.

A value whose absolute value exceeds F2F^2 is not allowed even when every other rule holds.

Some test cases cannot be solved without at least one negative value.