PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000096. Pishty và cây phép thuật (Pishty and Tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Pishty và cây phép thuật (Pishty and Tree)

Pishty and Tree

Source: CodeChef

Version: Phuoc Hung OJ Extended

Problem Statement

Pishty explores a castle of nn rooms connected by n−1n-1 bidirectional corridors forming a tree. Each corridor has a positive integer magic value cc.

For every query (u,v,k)(u,v,k), consider only corridors on the unique path from uu to vv whose magic values are at most kk. Find the bitwise XOR of these values, or 00 if none qualify. Queries do not modify the tree.

Input

The first line contains nn, followed by n−1n-1 lines u,v,cu,v,c. The next line contains the number of queries mm, followed by mm query lines u,v,ku,v,k.

Output

Print one XOR result for each of the mm queries, in order.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤1051\le n,m\le 10^5; 1≤u,v≤n1\le u,v\le n; 1≤c,k≤1091\le c,k\le 10^9. Đồ thị là cây vô hướng. Additionally, n≤10n\le10, m≤10m\le10.
  • Subtask 2 (30 points): 1≤n,m≤1051\le n,m\le 10^5; 1≤u,v≤n1\le u,v\le n; 1≤c,k≤1091\le c,k\le 10^9. Đồ thị là cây vô hướng. Additionally, n≤2000n\le2000, m≤2000m\le2000.
  • Subtask 3 (50 points): 1≤n,m≤1051\le n,m\le 10^5; 1≤u,v≤n1\le u,v\le n; 1≤c,k≤1091\le c,k\le 10^9. Đồ thị là cây vô hướng.

Examples

Input

5
1 2 1
2 3 2
2 4 5
3 5 10
6
5 4 5
5 4 10
5 4 1
1 2 1
4 1 10
1 5 8

Output

7
13
0
1
4
3

Explanation

The path 5→45\to4 has weights 10,2,510,2,5. The answers are 2⊕5=72\oplus5=7 for k=5k=5, 10⊕2⊕5=1310\oplus2\oplus5=13 for k=10k=10, and 00 for k=1k=1. Path 4→14\to1 yields 5⊕1=45\oplus1=4. Path 1→51\to5 with k=8k=8 yields 1⊕2=31\oplus2=3.