International Event

No attempts yetTime limit5sMemory limit128 MB

Problem

A large international event is held every year. A long, wide street runs in front of the event hall. When the event opens, the organizers put up flagpoles along the street. They raise national flags on the flagpoles and rearrange them every year. This has become the symbol of the event.

The flagpoles stand on a line, and flagpole fif_i is at location i\ell_i. The locations i\ell_i are integers and all distinct. Every flag raised on a flagpole belongs to a nation in the set \aleph. Last year the flag of nation aia_i was raised on flagpole fif_i. On the first day of the new year, the nation bib_i whose flag is newly raised on flagpole fif_i is decided.

The flag on flagpole fif_i has to change from aia_i to bib_i. A robot \Re does this work by lowering and raising flags. \Re can carry an almost unlimited number of flags. It may lower the flag of nation aia_i on flagpole fif_i and carry it. It may then move to the location j\ell_j of a flagpole fjf_j with bj=aib_j = a_i and raise the flag there. This work is always possible because the following condition holds.

For each nation cc \in \aleph, the number of flagpoles fif_i with ai=ca_i = c equals the number of flagpoles fjf_j with bj=cb_j = c.

There is a special location AA, different from every i\ell_i, where the robot \Re must always start and finish. No flagpole stands at AA. So \Re starts at AA, delivers every flag of nation aia_i to a flagpole fjf_j with bj=aib_j = a_i, and ends at AA.

Given the location AA, the locations of the flagpoles, and the nations aia_i and bib_i of each flagpole fif_i, write a program that computes the minimum travel distance of the robot \Re to deliver all the flags.

Figure 1 shows six points representing the flagpoles and the special point AA where the robot \Re starts and finishes. The nations of the flags correspond to the integers in the set {1,2,3}\{1, 2, 3\}. Each point carries a pair of integers (a,b)(a, b), where aa is the nation of last year's flag and bb is the nation of the new year's flag. The arrows show a movement of \Re that makes the travel distance as short as possible. \Re moves right from AA to 5 and loads the flag of nation 2 from the point at 5. Moving left from 5 to 1, it delivers the flags of the points at 3 and 5 to the points at 1 and 2. Then it moves right from 1 to 7 and delivers the flags of the points at 1, 2, and 6 to the points at 3, 5, and 7. Finally it moves left from 7 to AA and delivers the flag of the point at 7 to the point at 6 on the way. The travel distance of \Re is 14.


Figure 1.

Input

Your program reads from standard input. The input consists of TT test cases. The first line of the input contains the number of test cases TT. Each test case starts with a line containing an integer NN (2N100,0002 \le N \le 100{,}000), the number of points representing the flagpoles, which does not count AA. The second line contains an integer α\alpha (1α1,000,0001 \le \alpha \le 1{,}000{,}000), the coordinate of AA. The third line contains an integer MM (1M1,0001 \le M \le 1{,}000), which stands for the set of nations {1,2,,M}\{1, 2, \dots, M\}. For each integer i=1,,Mi = 1, \dots, M, at least one flag of nation ii is raised on a flagpole. The ii-th of the following NN lines contains three integers i\ell_i, aia_i, and bib_i: the coordinate of flagpole fif_i, the nation of its flag last year, and the nation of its flag in the new year. Here 1i1,000,0001 \le \ell_i \le 1{,}000{,}000 with iα\ell_i \ne \alpha, and 1ai,biM1 \le a_i, b_i \le M with aibia_i \ne b_i. All i\ell_i are distinct and are given in increasing order.

Output

Your program writes to standard output. Print exactly one line for each test case. The line contains the minimum travel distance of the robot \Re.