Under construction

Still being built. Things may change

Cactus

Time limit2sMemory limit512 MB

Summary
Given a cactus graph with XOR edge costs, answer each query with the k-th smallest distinct XOR value over all simple paths between two vertices.
Level

Hard9 of 10

Topics
Graph, DFS, Bit manipulation, Tree
Solved
No attempts yet

Problem

A cactus graph is a connected undirected graph with no self-loops or multiple edges in which each edge belongs to at most one simple cycle.

You are given a cactus graph GG with NN vertices, numbered from 11 to NN, and MM edges. The ii-th edge connects vertices aia_i and bib_i, and its cost is cic_i.

The cost of a simple path in GG is the bitwise XOR of the costs of all edges on that path.

Answer QQ queries of the form "xix_i yiy_i kik_i". Consider the costs of all simple paths connecting vertices xix_i and yiy_i, remove duplicate values, sort them in ascending order, and output the kik_i-th element. If there are fewer than kik_i distinct values, output −1-1.

Input

The first line contains two integers NN and MM, the number of vertices and edges (2≤N≤1052 \le N \le 10^5, N−1≤M≤2⋅105N-1 \le M \le 2 \cdot 10^5).

Each of the next MM lines contains three integers aia_i, bib_i, and cic_i (1≤ai,bi≤N1 \le a_i, b_i \le N, ai≠bia_i \ne b_i, 0≤ci<2300 \le c_i < 2^{30}).

The next line contains an integer QQ, the number of queries (1≤Q≤2⋅1051 \le Q \le 2 \cdot 10^5).

Each of the next QQ lines contains three integers xix_i, yiy_i, and kik_i (1≤xi,yi≤N1 \le x_i, y_i \le N, xi≠yix_i \ne y_i, 1≤ki≤2301 \le k_i \le 2^{30}).

Output

For each query, print one integer on its own line: the answer to that query.

Examples1

  1. Example 1

    Input
    4 4
    1 2 2
    1 3 8
    2 3 0
    1 4 6
    4
    2 1 1
    1 2 2
    4 1 1
    4 3 2020
    
    Expected output
    2
    8
    6
    -1