Game on a Tree

아직 제출이 없습니다시간 제한5초메모리 제한512 MB

문제

Some friends, numbered from 00 to m1m-1, are playing a game. There are cards of cc different colors, where colors are numbered from 00 to c1c-1. Each card of color ii has a fixed value p_ip\_i.  At the beginning of the game, each player chooses a certain amount of cards, without taking more than one card of the same color (though different friends may choose cards of the same color). We define the score of a deck as the GCD (greatest common divisor) of the values of its cards. Notice that an empty deck has a score of 00 points. The game will be played on an undirected graph which is a tree consisting of nn nodes, numbered from 00 to n1n-1. Each node vv contains a card with color c_vc\_v, and different nodes may have cards of the same color.

The game has mm rounds. In round ii, the ii-th friend gets to play. First, he chooses two different vertices vv and uu. Then, for each node v_iv\_i on the path from vv to uu (including vv and uu), each player (including himself) draws a card of color c_v_ic\_{v\_i}. If they already had a card of color c_v_ic\_{v\_i}, they will discard both cards. After doing this, player ii gets as many points as the sum of the points of each player's deck (including himself). Finally, he chooses a node of the graph and draws a new card from the pile. Then he changes the node's card for the card he has just drawn (and discards the old card), ending his round.

Given the information of each round, print how many points each friend will have at the end of the game modulo 109+710^9 + 7.

입력

The first line contains three integers mm, cc, nn (2n,m1052 \leq n, m \leq 10^5; 1c201 \leq c \leq 20): the number of friends (and hence, rounds), the number of different colors of the cards, and the number of nodes the tree will have.

The second line contains cc integers p_0,p_1,,p_c1p\_0, p\_1, \dots, p\_{c-1} (0p_i1090 \leq p\_i \leq 10^9), where p_ip\_i is the value of a card of color ii.

Each of the next mm lines starts with an integer xx (0xc0 \leq x \leq c), the number of cards the ii-th player starts with. It is followed by xx integers y_0,y_1,,y_x1y\_0, y\_1, \dots, y\_{x-1} (0y_i<c0 \leq y\_i < c): the colors of all the player's cards.

The next line contains nn integers c_0,c_1,,c_n1c\_0, c\_1, \dots, c\_{n-1} (0c_i<c0 \leq c\_i < c), the color of the card in each node of the graph.

Then n1n-1 lines follow, each one containing two integers uu and vv (0u,v<n0 \leq u, v < n), meaning that there is an edge between nodes uu and vv. It is guaranteed that these edges form a tree.

Finally, each of the last mm lines has four integers uu, vv, ww, yy (0u,v,w<n0 \leq u, v, w < n; uvu \neq v; 0y<c0 \leq y < c) that encode the information about each of the mm rounds: the two chosen nodes (uu and vv) and the node ww that will have its card changed for one with color yy.

출력

Print one line with mm integers: points_0,points_1,,points_m1\mathit{points}\_0, \mathit{points}\_1, \dots, \mathit{points}\_{m-1}, where points_i\mathit{points}\_i is the number of points the ii-th friend ends up with, modulo 109+710^9 + 7.