#P11022. [2016杭电多校]Boss Bo

[2016杭电多校]Boss Bo

Boss Bo

Problem Description

There is a tree with NN nodes, whose root is 1. There are QQ queries for you,for each query,we will give KK numbers,which are A1,A2,,AKA_1,A_2,\dots,A_K. For every node x[1,N]x\in[1,N] in the tree,we assume it's good if there is not a node yAy\in A,such that yy is the ancient of xx or y=xy=x And we will give two numbers T,PT , P to show the property of each query. 1.when T=1T=1,you should output the sum of the distance between every good node and PP. 2.when T=2T=2,you should output the minimum distance between every good node and PP. 3.when T=3T=3,you should output the maximum distance between every good node and PP. Let the distance between nodes in the tree be the shortest path between these two nodes.And we assume the length of each edge is 1. Specially,the distance between two same nodes is 0. For each query,if there is no nodes that is good,just output -1.

Input

There are several test cases. For each test case,the first line contains two numbers NN and QQ. For the following N1N-1 lines,each line contains two numbers uu and vv,indicating there is a edge between uu and vv in tree. For the following QQ lines,each line contains some numbers,which are K,P,T,A1,A2,,AKK,P',T,A_1,A_2,\dots,A_K in order. Let the answer of last query be lastanslastans,then P=(P+lastAns) modN+1P=(P' + lastAns)~\mod{N} + 1. If the answer of last query is -1 or it's the first query,then lastans=0lastans=0. Let the number of test cases be TT,we guarantee T=30T=30. 80%80\% test cases satisfy N,K,Q10000N,K,Q \le 10000 ,K20000\sum{K} \le 20000. 100%100\% test cases satisfy ~P,N,K50000P, N,K \le 50000,Q100000Q \le 100000,K200000\sum{K} \le 200000.

Output

You should output Q lines in total,each line contains a number indicating the answer of each query.

Sample Input

6 5
1 3
2 1
4 2
6 4
5 6
3 5 3
3 3 3
3 4 3
4 5 1
3 3 1
1 5 6
3 5 2
3 4 2
3 3 3
6 5 3

Sample Output

3
-1
-1
3
2

Author

绍兴一中

Source

2016 Multi-University Training Contest 3