Under construction

Still being built. Things may change

A Game with Two Trees

Time limit2sMemory limit512 MB

Summary
Given two trees with marked special nodes, add one edge between a node of each tree and sum the perfect paths for every pair, weighted by i+j, modulo 1e9+7.
Level

Hard8 of 10

Topics
Tree, DFS, Divide and conquer
Solved
No attempts yet

Problem

Albert is playing a game with two trees. The first tree TAT_A has NAN_A nodes, written a1,a2,…,aNAa_1, a_2, \dots, a_{N_A}. The nodes in SAS_A are called "special". The second tree TBT_B has NBN_B nodes, written b1,b2,…,bNBb_1, b_2, \dots, b_{N_B}. The nodes in SBS_B are special.

The figure below shows an example where both trees have 3 nodes (NA=NB=3N_A = N_B = 3). SAS_A has 3 nodes and SBS_B has 1 node. Special nodes are drawn with double lines.

Pick one node from each tree and connect them with an edge. Let Ui,jU_{i,j} be the tree formed by adding the edge e=(ai,bj)e = (a_i, b_j) to TA∪TBT_A \cup T_B. The score S(i,j)S(i, j) of Ui,jU_{i,j} is the number of simple paths in Ui,jU_{i,j} that contain exactly one node from SAS_A and exactly one node from SBS_B. Call such a path a "perfect path".

In the example, S(1,1)=1S(1, 1) = 1, S(1,2)=3S(1, 2) = 3, and S(1,3)=1S(1, 3) = 1. For U1,1U_{1,1}, the only perfect path is a1−b1−b3−b2a_1 - b_1 - b_3 - b_2. For U1,2U_{1,2}, there are three perfect paths: a1−b2a_1 - b_2, a1−b2−b3a_1 - b_2 - b_3, and a1−b2−b3−b1a_1 - b_2 - b_3 - b_1. For U1,3U_{1,3}, the only perfect path is a1−b3−b2a_1 - b_3 - b_2.

The values S(2,1)=1S(2, 1) = 1, S(2,2)=3S(2, 2) = 3, S(2,3)=1S(2, 3) = 1, S(3,1)=1S(3, 1) = 1, S(3,2)=3S(3, 2) = 3, and S(3,3)=1S(3, 3) = 1 are obtained the same way.

Computing S(i,j)S(i, j) for one pair is easy, so Albert wants to compute V(TA,TB)=∑1≤i≤NA∑1≤j≤NBS(i,j)⋅(i+j)V(T_A, T_B) = \sum_{1 \le i \le N_A} \sum_{1 \le j \le N_B} S(i, j) \cdot (i + j). In the example, this value is 60.

Given both trees, compute V(TA,TB)V(T_A, T_B).

Input

The first line contains the number of test cases CC.

The first line of each test case contains NAN_A and MA=∣SA∣M_A = |S_A|, separated by a space. The second line contains the MAM_A indices of the nodes in SAS_A, separated by spaces. The next NA−1N_A - 1 lines each contain two integers, separated by a space, describing an edge of TAT_A.

Then comes a line with NBN_B and MB=∣SB∣M_B = |S_B|, separated by a space. The next line contains the MBM_B indices of the nodes in SBS_B, separated by spaces. The last NB−1N_B - 1 lines each contain two integers describing an edge of TBT_B.

Output

For each test case, print V(TA,TB)V(T_A, T_B) modulo 109+710^9 + 7 on its own line.

Constraints

  • 1≤C≤101 \le C \le 10
  • 2≤NA,NB≤65,0002 \le N_A, N_B \le 65,000
  • 1≤MA≤NA1 \le M_A \le N_A
  • 1≤MB≤NB1 \le M_B \le N_B
  • No element appears twice in SAS_A.
  • No element appears twice in SBS_B.
  • Both TAT_A and TBT_B are valid trees.

Examples1

  1. Example 1

    Input
    5
    2 2
    1 2
    1 2
    2 2
    1 2
    2 1
    3 3
    1 2 3
    1 2
    2 3
    3 1
    2
    1 3
    2 3
    3 3
    1 2 3
    1 2
    2 3
    3 3
    1 2 3
    1 3
    2 3
    5 3
    1 3 5
    2 1
    1 4
    1 5
    3 2
    6 3
    2 3 5
    3 1
    2 1
    2 4
    3 5
    3 6
    8 4
    1 3 5 7
    2 1
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    4 2
    2 3
    1 2
    4 1
    3 4
    
    Expected output
    12
    60
    36
    904
    1600