Baby Ming and Matrix tree

Accepts: 3
Submissions: 12
Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description
Give a $2*2$ 01-matrix A, which can deal with the following $2$ kinds of transformation: **Rotation transformation**: clockwise rotation. Move $A(i, j)$ to $A(j,1- i)$ **Replace**: replace the matrix $A$ into matrix $B$. Given a tree, there is a matrix $A_i$ in every node of the tree, Baby Ming likes to play the matrix in the tree. Every time, Baby Ming choose two node $a, b$ in the tree and change all the matrixes in the path from $a$ to $b$ into matrix $B$ by the above two kinds of transformation. Suppose rotation transformation takes $2$ seconds every time while replace takes $10$. Baby Ming wants to know the minimum time it costs.
Input
In the first line contains a single positive integer $T$, indicating number of test case. For every test case: In the first line there is a number $n$ to show the tree has $n$ nodes. In the next $n-1$ lines, each line contains $2$ integer $a, b$, indicate the edge between $a$ and $b$. In the next $n*2$ lines, each line contains $2$ numbers $01$, indicate the matrix $A_i$ in the node $i$. Input a positive integer $q$, indicate the number of queries. For each query: In the first line there is two positive numbers $a, b$. In the second line and third line there is a $2*2$ 01-matrix B, It represents select two nodes $a, b$, transform all the matrix $A_i$ in the path from $a$ to $b$ into matrix $B$ **(matrix $A_i, B$ consist by $2$ zeros and $2$ ones)** $1 \leq T \leq 30, 1 < n \leq 20000, 1 \leq q \leq 20000, 1 \leq a, b \leq n$
Output
For each of $Q$ operation, print one line to show minimum time the change needed.
Sample Input
1
5
3 4
1 2
2 5
1 4
0 0
1 1
0 1
1 0
1 1
0 0
0 1
0 1
1 0
0 1
3
1 5
0 1
1 0
2 3
0 1
0 1
1 2
0 0
1 1
Sample Output
12
22
4