Telephone Network

Time limit2sMemory limit128 MB

Summary
Route m disjoint input-output requests through a recursive Clos-like network of binary switches, choosing at each layer the lexicographically smallest routing bit string.
Level

Hard8 of 10

Topics
Graph, Greedy, Recursion, Bit manipulation
Solved
No attempts yet

Problem

A telephone company wants to build a new telephone network in a city. Every person in the city should be able to call every other person. Building a direct connection between every pair of persons is impossible, so the company uses a network made of several layers.

The network switch in layer jj is written S(j)S(j). A switch S(0)S(0) consists of one input, one output and a cable that connects the input to the output. A switch S(j)S(j) with j>0j > 0 consists of 2j2^j inputs, 2j2^j outputs and two switches S(j−1)S(j-1). Input ii of S(j)S(j) (0≤i<2j0 \le i < 2^j) is connected by a cable to input i mod 2j−1i \bmod 2^{j-1} of each of the two switches S(j−1)S(j-1). Output ii of S(j)S(j) is connected the same way to output i mod 2j−1i \bmod 2^{j-1} of each of the two switches S(j−1)S(j-1).

Consider a network whose outermost layer is a single switch S(n)S(n). From any input and any output of S(n)S(n) there is exactly one path to each of the S(0)S(0) switches, so any input of S(n)S(n) can be connected to any of its outputs, and naming the S(0)S(0) switch that carries the connection fixes the whole path.

The S(0)S(0) switches inside S(n)S(n) are numbered 00 to 2n−12^n - 1. Switch number ii is defined as follows. Write ii in binary as bn−1bn−2…b0b_{n-1}b_{n-2}\dots b_0. These bits describe a path from an input of S(n)S(n) down to switch number ii: for each jj, bj=0b_j = 0 means the path leaves S(j+1)S(j+1) into the first of the two switches S(j)S(j) it consists of, and bj=1b_j = 1 means it leaves into the second one. The path ends at the same S(0)S(0) switch no matter which input of S(n)S(n) it starts from, and that switch is the one numbered ii.

Several connections are sometimes needed at the same time. To avoid interference, every input and every output of every switch S(j)S(j) (0≤j≤n0 \le j \le n) may be used by at most one connection. Given a set of connection requests, route every request so that no two connection paths share an input or an output of any switch.

Input

The first line contains a positive integer, the number of test cases, at most 100100. Each test case follows in this form.

  • One line with two integers nn (1≤n≤161 \le n \le 16) and mm (1≤m≤2n1 \le m \le 2^n): the layer of the outermost switch and the number of connection requests.
  • mm lines, the ii-th with two integers aia_i and bib_i (0≤ai,bi<2n0 \le a_i, b_i < 2^n), a request to connect input aia_i of S(n)S(n) to output bib_i. The values aia_i are pairwise distinct, and the values bib_i are pairwise distinct as well.

Output

For each test case, print one line with mm integers s1,…,sms_1, \dots, s_m, where sis_i is the number of the S(0)S(0) switch that carries the connection from input aia_i to output bib_i. The mm connection paths must be pairwise disjoint, and at least one such routing always exists.

Several routings are usually valid, so print the canonical one. It is defined layer by layer, from the outermost layer inward. In layer jj every request enters one of the two switches S(j−1)S(j-1) that the switch S(j)S(j) holding it consists of. Write the choices of that layer as the bit string c1c2…cmc_1c_2\dots c_m, where cic_i is 00 for the first of the two switches and 11 for the second, so cic_i is bit j−1j-1 of sis_i. Start from all valid routings and keep the ones whose bit string for layer nn is smallest in lexicographic order. Among those keep the ones whose bit string for layer n−1n-1 is smallest, and continue down to layer 11. Exactly one routing survives.

Hint

The number of an S(0)S(0) switch spells out its path. For n=3n = 3, switch number 55 has bits b2b1b0=101b_2b_1b_0 = 101, so the path takes the second S(2)S(2) inside S(3)S(3), then the first S(1)S(1) inside that S(2)S(2), then the second S(0)S(0) inside that S(1)S(1).

Examples1

  1. Example 1

    Input
    2
    1 1
    0 1
    3 5
    0 3
    1 4
    2 5
    3 6
    4 7
    
    Expected output
    0
    0 1 2 3 4