Strahler order

No attempts yetTime limit1sMemory limit128 MB

Problem

In geology a river system can be drawn as a directed graph. A river is an edge, and the direction the water flows is the direction of the edge. A node is a place where a river begins, such as a spring or a lake, a place where rivers merge or split, or the place where a river meets the sea. Water only runs downstream, so no path returns to a node it has already passed.

The order of the river drawn by edge (u,v)(u, v) is the order of node uu that the river flows out of. The Strahler order of each node is found as follows.

  • A node with no incoming river is the source of a river, and its order is 11.
  • For any other node, let ii be the largest order among its incoming rivers. If exactly one incoming river has order ii, the node has order ii. If two or more do, the node has order i+1i + 1.

The order of the river system equals the order of the node that meets the sea. There is always exactly one such node, and it is node MM.

Given a river system, write a program that computes its Strahler order.

Among real rivers the Amazon has the largest Strahler order at 1212, and the largest in the United States is the Mississippi at 1010.

Input

The first line contains the number of test cases TT (1T1000)(1 \le T \le 1000).

The first line of each test case contains KK, MM, and PP. KK is the test case number, MM is the number of nodes, and PP is the number of edges. (2M1000)(2 \le M \le 1000)

Each of the next PP lines contains one edge as AA and BB, meaning water flows from AA to BB. (1A,BM)(1 \le A, B \le M)

Node MM is always the node that meets the sea, and no edge leads out of node MM.

Output

For each test case, print the test case number KK and the Strahler order of that river system on one line, separated by a single space.