This page is still under construction.

Parts of this page are still being built. What you see may change.

International Event

Time limit5sMemory limit128 MB

Summary
Find the minimum distance for a robot starting and ending at A to move every flag along a line from its old pole to a pole requesting that nation.
Level

Medium7 of 10

Topics
Greedy, Prefix sum, Intervals
Solved
No attempts yet

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 c∈ℵc \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 (2≤N≤100,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 (1≤M≤1,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 1≤ℓi≤1,000,0001 \le \ell_i \le 1{,}000{,}000 with ℓi≠α\ell_i \ne \alpha, and 1≤ai,bi≤M1 \le a_i, b_i \le M with ai≠bia_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.

Examples1

  1. Example 1

    Input
    2
    6
    4
    3
    1 1 2
    2 1 2
    3 2 1
    5 2 1
    6 2 3
    7 3 2
    12
    12
    3
    1 2 3
    4 1 2
    5 2 3
    7 1 2
    8 2 3
    10 1 2
    15 2 1
    16 2 1
    19 3 2
    20 2 1
    24 3 2
    26 3 2
    
    Expected output
    14
    62