# Move the Coins – Creating Tests SOLUTIONS MVCN2TST

## Move the Coins – Creating Tests SOLUTION

Gourmet expert is planning tests for the issue “Move the Coins”!
He needs to make a tree G with N hubs (numbered 1 through N), which is established at hub 1, and a rundown of Q unmistakable reparentings. A reparenting is a couple of hubs (u,v) (u≠1) of G and the consequence of applying it to G is another chart, framed as follows:
Take a duplicate of the first tree G.
In this duplicate, eliminate the edge interfacing the vertex u to its parent.
At that point, include another edge between the hubs u and v.
For instance, think about the accompanying tree G:
We wish to apply the reparenting r=(6,3) to it. The subsequent chart would be
A reparenting r is legitimate if the subsequent chart is as yet a tree (that is, an associated diagram without cycles). Until further notice, Chef needs to produce just substantial reparentings.
Gourmet expert has just settled on the tree G and now he simply needs to pick Q legitimate reparentings. He makes an underlying rundown L of all substantial reparentings for this tree and sorts them in an exceptional request.
Let r=(u,v) and r′=(u′,v′) be substantial reparentings. Their request is chosen as follows:
on the off chance that u<u′, at that point r<r′
on the off chance that u>u′, at that point r>r′
on the off chance that u=u′, discover the separations of hubs v and v′ from the root and indicate them by h and h′ individually
on the off chance that u=u′ and h<h′, at that point r<r′
on the off chance that u=u′ and h>h′, at that point r>r′
on the off chance that u=u′, h=h′ and v<v′, at that point r<r′
on the off chance that u=u′, h=h′ and v>v′, at that point r>r′
Gourmet expert picks Q whole numbers c1,c2,… ,cQ, eliminates the c1-th component of L, at that point eliminates the c2-th component of the subsequent (littler) list L, etc, until he eliminates the cQ-th component. The arrangement c1,c2,… ,cQ is picked so that during this cycle, every one of them is a legitimate list of a component in the current rundown L. Thusly, he eliminates Q particular legitimate reparentings and utilizations them in one test.
Since Chef doesn’t trust in making life simple for himself, the succession c1,c2,… ,cQ is encoded and every one of its components can’t be unscrambled until all past reparentings are found. All the more officially, consider a decoding key d; at first, d=0. You are given a scrambled arrangement e1,e2,… ,eQ. For every whole number I from 1 to Q, Chef unscrambles ci=ei⊕d (⊕ indicates the bitwise XOR administrator), finds the ci-th reparenting in the current rundown L, meant by (ui,vi), eliminates it from L and updates the decoding key d to (d+2i⋅ui+3i⋅vi)%(109+7).
Would you be able to assist Chef with finding the Q legitimate reparentings relating to his encoded decisions? Figuring the last estimation of d will get the job done to show that you can do it!
Info
The principal line of the information contains a solitary number T signifying the quantity of experiments. The portrayal of T experiments follows.
The main line of each experiment contains a solitary number N.
Each of the following N−1 lines contains two space-isolated whole numbers an and b signifying that hubs an and b are associated by an edge.
The following line contains a solitary number Q.
Q lines follow. For each substantial I, the I-th of these lines contains a solitary whole number ei.
Yield
For each experiment, print a solitary line containing one number ― the last estimation of d in the wake of preparing all Q reparentings.
Imperatives
1≤T≤1,000
1≤N,Q≤200,000
1≤a,b≤N
0≤ei≤236 for each substantial I
1≤ci≤R+1−i for each substantial I, where R is the quantity of legitimate reparentings
the aggregate of N over all experiments doesn’t surpass 200,000
the aggregate of Q over all experiments doesn’t surpass 200,000
T≤10
N,Q≤100
T≤100
N,Q≤1,000
Subtask #3 (85 focuses): unique limitations
Model Input
2
5
1 3
1 2
2 4
2 5
3
5
23
72
7
1 7
2 1
6 1
5 4
3 5
1 5
5
9
6
35
313
602
Model Output
126
910
Clarification
Model case 1: The tree G is
There are 25 reparentings (u,v) ― beyond any reasonable amount to draw every one of them! Nonetheless, here is an agent testing. Substantial reparentings are set apart with a ‘✓’; invalid ones with a ‘✗’:
We can likewise shape a table of legitimacy of all reparentings (u,v):
v
1 2 3 4 5
─────────
1 │✗ ✗
2 │✓ ✗ ✓ ✗
u 3 │✓ ✓ ✗ ✓
4 │✓ ✓ ✗ ✓
5 │✓ ✓ ✗
Our underlying rundown L, at that point, is shaped by posting all sets (u,v) with a ‘✓’ and arranging in the request portrayed previously. We get the accompanying rundown:
# u v h
1. 2 1 0
2. 2 3 1
3. 3 1 0
4. 3 2 1
5. 3 4 2
6. 3 5 2
7. 4 1 0
8. 4 2 1
9. 4 3 1
10. 4 5 2
11. 5 1 0
12. 5 2 1
13. 5 3 1
14. 5 4 2
For i=1,2,3, we have to decode ei to get ci, at that point locate the comparing reparenting (ui,vi) in the current rundown L, eliminate it from L and use it to refresh our unscrambling key d. The underlying estimation of d is 0. The primary scrambled decision is e1=5; we decode it to get c1=d⊕e1=0⊕5=5, so the reparenting relating to the best option is the 5-th section in the current rundown L, which is (u1,v1)=(3,4). We update d to (0+21⋅u1+31⋅v1)%(109+7)=18 and eliminate the 5-th component from L, which becomes
# u v h
1. 2 1 0
2. 2 3 1
3. 3 1 0
4. 3 2 1
5. 3 5 2
6. 4 1 0
7. 4 2 1
8. 4 3 1
9. 4 5 2
10. 5 1 0
11. 5 2 1
12. 5 3 1
13. 5 4 2
On to the subsequent option! e2=23, so c2=18⊕23=5 and the reparenting (u2,v2) is (3,5). We update d again to (18+22⋅u2+32⋅v2)%(109+7)=75 and eliminate (3,5) from our rundown L, which becomes
# u v h
1. 2 1 0
2. 2 3 1
3. 3 1 0
4. 3 2 1
5. 4 1 0
6. 4 2 1
7. 4 3 1
8. 4 5 2
9. 5 1 0
10. 5 2 1
11. 5 3 1
12. 5 4 2
On to the third and last decision! With e3=72, we unscramble c3=75⊕72=3, so the reparenting (u3,v3)=(3,1). We utilize this to refresh d for the last an ideal opportunity to (75+23⋅u3+33⋅v3)%(109+7)=126.
We have now handled all Q decisions, and the last unscrambling key is d=126.
Model case 2: The tree G is
There are 49 potential reparentings; once more, we just show a delegate test of them and their legitimacy:
The entire table of legitimacy of reparentings is
v
1 2 3 4 5 6 7
─────────────
1 │✗ ✗
2 │✓ ✗ ✓
3 │✓ ✓ ✗ ✓
u 4 │✓ ✓ ✗ ✓
5 │✓ ✓ ✗ ✓
6 │✓ ✓ ✗ ✓
7 │✓ ✓ ✗
By posting the legitimate ones and arranging them in the depicted request, we get our underlying rundown L:
# u v h
1. 2 1 0
2. 2 5 1
3. 2 6 1
4. 2 7 1
5. 2 3 2
6. 2 4 2
7. 3 1 0
8. 3 2 1
9. 3 5 1
10. 3 6 1
11. 3 7 1
12. 3 4 2
13. 4 1 0
14. 4 2 1
15. 4 5 1
16. 4 6 1
17. 4 7 1
18. 4 3 2
19. 5 1 0
20. 5 2 1
21. 5 6 1
22. 5 7 1
23. 6 1 0
24. 6 2 1
25. 6 5 1
26. 6 7 1
27. 6 3 2
28. 6 4 2
29. 7 1 0
30. 7 2 1
31. 7 5 1
32. 7 6 1
33. 7 3 2
34. 7 4 2
The underlying estimation of d is 0. The first scrambled worth is e1=9 and we decode it to get c1=d⊕e1=0⊕9=9. The reparenting (u1,v1) is the 9-th component of L, for example (u1,v1)=(3,5). We update d to (0+21⋅u1+31⋅v1)%(109+7)=21 and eliminate (3,5) from the rundown L. (To spare space, we don’t show L after every expulsion.)
On to the following encoded esteem e2=6: we decode it to get c2=21⊕6=19, so the reparenting (u2,v2) is the 19-th component of L now (the 20-th component in the underlying rundown), for example (5,2). We update d to (21+22⋅u2+32⋅v2)%(109+7)=59 and eliminate (5,2) from L.
Next, e3=35, so c3=59⊕35=24, (u3,v3)=(6,7), we update d to (59+23⋅u3+33⋅v3)%(109+7)=296 and eliminate (6,7) from L.
Next, e4=313, so c4=296⊕313=17, (u4,v4)=(4,3), we update d to (296+24⋅u4+34⋅v4)%(109+7)=603 and eliminate (4,3) from L.
The remainder of Chef’s encoded decisions is e5=602, so c5=603⊕602=1, (u5,v5)=(2,1). We update d to (603+25⋅u5+35⋅v5)%(109+7)=910.
We have now prepared all Q of Chef’s decisions, and the last unscrambling key is d=910.