CF BUDDY
← Problems·

1980G · Yasya and the Mysterious Tree

2300 · bitmasks, data structures, dfs and similar

Problem: Yasya was walking in the forest and accidentally found a tree with nn vertices. A tree is a connected undirected graph with no cycles.

Next to the tree, the girl found an ancient manuscript with mm queries written on it. The queries can be of two types.

The first type of query is described by the integer yy. The weight of each edge in the tree is replaced by the bitwise exclusive OR of the weight of that edge and the integer yy.

The second type is described by the vertex vv and the integer xx. Yasya chooses a vertex uu (1un1 \le u \le n, uvu \neq v) and mentally draws a bidirectional edge of weight xx from vv to uu in the tree.

Then Yasya finds a simple cycle in the resulting graph and calculates the bitwise exclusive OR of all the edges in it. She wants to choose a vertex uu such that the calculated value is maximum. This calculated value will be the answer to the query. It can be shown that such a cycle exists and is unique under the given constraints (independent of the choice of uu). If an edge between vv and uu already existed, a simple cycle is the path vuvv \to u \to v.

Note that the second type of query is performed mentally, meaning the tree does not change in any way after it.

Help Yasya answer all the queries.

Input Format: The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains two integers nn, mm (2n21052 \le n \le 2 \cdot 10^5, 1m21051 \le m \le 2 \cdot 10^5) — the number of vertices in the tree and the number of queries.

The next n1n - 1 lines of each test case contain three integers vv, uu, ww (1v,un1 \le v, u \le n, 1w1091 \le w \le 10^9) — the ends of some edge in the tree and its weight.

It is guaranteed that the given set of edges forms a tree.

The next mm lines of each test case describe the queries:

  • ^ yy (1y1091 \le y \le 10^9) — parameter of the first type query;
  • ? vv xx (1vn1 \le v \le n, 1x1091 \le x \le 10^9) — parameters of the second type query.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5. The same is guaranteed for mm.

Output Format: For each test case, output the answers to the queries of the second type.

Sample Cases

Case 1

Input

2
3 7
1 2 1
3 1 8
^ 5
? 2 9
^ 1
? 1 10
^ 6
? 3 1
? 2 9
5 6
1 2 777
3 2 2812
4 1 16
5 3 1000000000
^ 4
? 3 123
? 5 1000000000
^ 1000000000
? 1 908070
? 2 1

Output

13 15 11 10 
1000000127 2812 999756331 999999756

Case 2

Input

3
8 4
8 6 3
6 3 4
2 5 4
7 6 2
7 1 10
4 1 4
5 1 2
^ 4
^ 7
? 7 8
? 4 10
5 6
3 1 4
2 3 9
4 3 6
5 2 10
? 5 7
^ 1
^ 8
? 4 10
? 1 9
? 3 6
4 2
2 1 4
4 3 5
2 3 4
^ 13
? 1 10

Output

14 13 
13 8 11 11 
10

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback