Under construction

Still being built. Things may change

XOR

Time limit2sMemory limit512 MB

Summary
In a connected weighted graph where a ride's cost is the XOR of its edge lengths and edges may repeat, answer queries for the minimum cost between two vertices.
Level

Hard8 of 10

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

Problem

A new city called Xor was recently built in Croatia. The city has NN intersections and MM two-way roads. Each road connects a pair of intersections and has a length, a non-negative integer. Mirko moved to Xor and plans to take taxis often. Taxi fares in Xor are computed in an unusual way. The price of a ride is the bitwise XOR of the lengths of the roads the taxi drives along. For example, a ride along roads of lengths 3, 4 and 6 costs 3 XOR 4 XOR 6 = 1. During one ride the taxi may drive along the same road several times, and every pass counts toward the price.

Mirko wants to spend as little as possible on his rides. Each ride is given by a starting intersection AA and a destination intersection BB, and you have to find the smallest possible price of that ride. The taxi may drive through intersection BB and keep going, but the ride has to end at BB.

Input

The first line contains the number of intersections NN and the number of roads MM. (1≤N,M≤200 0001 \le N, M \le 200\,000)

Each of the next MM lines describes one road with three integers AA, BB and CC (1≤A<B≤N1 \le A < B \le N, 0≤C≤1 000 000 0000 \le C \le 1\,000\,000\,000), meaning that a road of length CC connects intersections AA and BB.

No two roads connect the same pair of intersections.

Every intersection can be reached from every other intersection.

The next line contains the number of Mirko's rides QQ. (1≤Q≤200 0001 \le Q \le 200\,000)

Each of the next QQ lines describes one ride with the starting intersection AA and the destination intersection BB. (1≤A,B≤N1 \le A, B \le N)

Output

For each of the QQ rides print the smallest possible price on its own line, in the order the rides are given in the input.

Note that bit ii of AA XOR BB is 1 exactly when bit ii of AA and bit ii of BB differ. (for example, 0011 XOR 0101 = 0110)

Hint

In the first example the taxi visits intersections 1, 2, 3, 4, 5, 3, 2 in that order, and the price is 3 XOR 9 XOR 2 XOR 6 XOR 7 XOR 9 = 0.

Examples2

  1. Example 1

    Input
    5 5
    1 2 3
    2 3 9
    3 4 2
    4 5 6
    3 5 7
    1
    1 2
    
    Expected output
    0
    
  2. Example 2

    Input
    4 6
    1 2 7
    1 3 2
    1 4 0
    2 3 13
    2 4 19
    3 4 31
    3
    4 1
    4 2
    4 3
    
    Expected output
    0
    6
    2