Under construction

Still being built. Things may change

Royal Problem

Time limit3sMemory limit1024 MB

Summary
Given a directed graph with XOR-weighted edges, find the XOR of the XORs of all walks from a to b, or report -1 if infinitely many walks have nonzero XOR.
Level

Hard8 of 10

Topics
Graph, Bit manipulation, Math, Union-find
Solved
No attempts yet

Problem

Not long ago, a new road network was built in Berland. Some pairs of cities are connected by one-way roads. The ii-th road goes from city uiu_i to city viv_i and has length wiw_i. The two main cities of Berland are numbered aa and bb.

The king of Berland loves his country and likes to compute all kinds of its characteristics. He calls the beauty of a path the bitwise XOR of the lengths of all roads on that path. He calls the beauty of his country the bitwise XOR of the beauties of all paths from city aa to city bb. Note that there may be infinitely many such paths, and they may pass through the same city several times.

The king wants to know the beauty of his country. Compute it, or report that it cannot be computed.

The bitwise XOR of a set of numbers is the bitwise XOR of all nonzero numbers in the set. If the set contains infinitely many nonzero numbers, its bitwise XOR cannot be computed.

The bitwise XOR (bitwise addition modulo two) is a binary operation that applies logical XOR to each pair of bits at the same position. If the corresponding bits differ, the result bit is 1. If they match, the result bit is 0. For example, for x=10910=11011012x = 109_{10} = 1101101_2 and y=4110=1010012y = 41_{10} = 101001_2, we get x⊕y=10001002=6810x \oplus y = 1000100_2 = 68_{10}.

A path in a graph is a sequence of vertices in which every two consecutive vertices are connected by an edge.

Input

The first line contains one integer tt (1≤t≤40 0001 \le t \le 40\,000), the number of test cases.

The first line of each test case contains two integers nn and mm (1≤n,m≤200 0001 \le n, m \le 200\,000), the number of cities and the number of roads.

Each of the next mm lines contains three integers uiu_i, viv_i, and wiw_i (1≤ui,vi≤n1 \le u_i, v_i \le n, 0≤wi≤230−10 \le w_i \le 2^{30}-1), the start city, end city, and length of the ii-th road.

The last line of each test case contains two integers aa and bb (1≤a,b≤n1 \le a, b \le n), the start and end cities of the paths the king is interested in.

The sum of nn over all test cases does not exceed 200 000200\,000, and the sum of mm over all test cases does not exceed 200 000200\,000.

Output

For each test case, print one integer, the beauty of Berland. If the answer does not exist, print −1-1.

Hints

In the first test case, the country has only one road, of length 00, so the beauty of any path is 00. Therefore the bitwise XOR of all path beauties is 00.

In the second test case, there are 6 paths from city 1 to city 3, with beauties 0⊕5=50 \oplus 5 = 5, 0⊕2=20 \oplus 2 = 2, 1⊕5=41 \oplus 5 = 4, 1⊕2=31 \oplus 2 = 3, 3⊕5=63 \oplus 5 = 6, and 3⊕2=13 \oplus 2 = 1. The beauty of the country is 5⊕2⊕4⊕3⊕6⊕1=75 \oplus 2 \oplus 4 \oplus 3 \oplus 6 \oplus 1 = 7.

In the third test case, paths from city 1 to city 2 have beauties 11, 1⊕2⊕1=21 \oplus 2 \oplus 1 = 2, 1⊕2⊕1⊕2⊕1=11 \oplus 2 \oplus 1 \oplus 2 \oplus 1 = 1, 22, and so on. There are infinitely many paths with nonzero beauty, so the answer cannot be computed.

In the fourth test case, there are infinitely many paths from city 2 to city 3 with beauty 00, and no path with nonzero beauty. So the beauty of the country is 00.

Examples1

  1. Example 1

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