This page is still under construction.

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

Kleptocrat

Time limit7sMemory limit512 MB

Summary
Given a connected weighted graph where path length is the XOR of edge weights, answer queries for the shortest distance between two nodes.
Level

Medium7 of 10

Topics
Graph, Shortest path, Bit manipulation, DFS
Solved
No attempts yet

Problem

Your company has a policy that refunds every employee an amount proportional to the shortest distance between their home and their office. This creates the loophole that many employees deliberately move very far away to claim the largest possible reimbursement.

One employee has abused this policy far too much and is about to bankrupt you. You have to stop this before you cancel the policy next year. The rules are strict, though: as long as the employee keeps track of the distances they have travelled, you are forced to reimburse them.

Then you have a flash of inspiration. Nowhere does it say you have to use Euclidean distances! You start working on subtler distance functions, and now you have a first prototype: XOR distance. The length of a path is the XOR of the lengths of the edges on the path, not the sum. The distance between two locations is the length of the shortest path between them.

You want to test this principle on the transport network, for the location of each of your employees in turn.

Input

  • The first line contains three integers nn (2≤n≤1042 \leq n \leq 10^4), mm (n−1≤m≤105n-1 \leq m \leq 10^5), and qq (1≤q≤105{1 \leq q \leq 10^5}): the number of nodes, edges, and questions.
  • The next mm lines describe the edges. Each line contains three integers xx, yy, ww (1≤x,y≤n1 \leq x,y \leq n, x≠yx\neq y, 0≤w≤10180 \leq w \leq 10^{18}), meaning there is an undirected edge of length ww between nodes xx and yy.
  • The next qq lines describe the questions. Each line contains two integers aa, bb (1≤a,b≤n1 \leq a,b \leq n), asking for the shortest distance between nodes aa and bb.

Between every pair of distinct nodes there is at most one edge, and every node is reachable from every other node.

Output

For each question, output the shortest distance between nodes aa and bb on its own line.

Examples2

  1. Example 1

    Input
    3 3 3
    1 2 2
    1 3 2
    2 3 3
    1 2
    1 3
    2 3
    
    Expected output
    1
    1
    0
    
  2. Example 2

    Input
    7 10 5
    1 2 45
    2 3 11
    2 4 46
    3 4 28
    3 5 59
    3 6 12
    3 7 3
    4 5 11
    5 6 23
    6 7 20
    1 4
    2 6
    3 5
    1 7
    5 5
    
    Expected output
    1
    5
    0
    5
    0