fossils

Time limit: 0.7s Memory limit: 64MB Input: Output:

Task

In DinoLand there were two major dinosaur families: Ankylosaurus and Brachiosaurus. The family of Ankylosaurus' consists of NN species, numbered from 11 to NN, while the family of Brachiosaurus' consists of MM species, numbered from 11 to MM.

The families started out as their original species, which is species 11 for both families. As evolution went on, they developed into multiple directions.

Each new species was a direct descendant of exactly one other species from the same family, and every species evolves completely independently.

Fortunately, we know their ancestry, so we can trace back the evolution of each species.

Archeologists are researching these families, and they started to note down how many times distinct species from different families lived in the same place.

Formally, if fossils of an Ankylosaurus species XX and fossils of a Brachiosaurus species YY have been found near each other CC times already, then the strength of connection between species XX and YY is CC, where CC is a positive integer.

Evolving does not mean that the ancestors of the species disappeared, so there are no restrictions for connections between any two species.

According to a new theory, dinosaur species were familiar with each other if they or some of their ancestors lived in the same place.
Now that they have collected a lot of data, they want to find answers for queries of the following type: given an Ankylosaurus species UU and a Brachiosaurus species VV, how familiar they were with each other?

Formally, answer the sum of the strengths of connections between them or any of their ancestors.

Input

The first line contains the integer NN (1N21051 \le N \le 2 * 10^5), the number of Ankylosaurus species.

The second line contains N1N-1 integers PAiPA_i, where Ankylosaurus species (i+1)(i+1) is a direct descendant of the species (PAi)(PA_i) for each i=1N1i=1\ldots N-1.

The third line contains the integer MM (1M21051 \le M \le 2 * 10^5), the number of Brachiosaurus species.

The fourth line contains M1M-1 integers PBiPB_i, where Brachiosaurus species (i+1)(i+1) is a direct descendant of the species (PBi)(PB_i) for each i=1M1i=1\ldots M-1.

The fifth line contains the integer KK (1K21051 \le K \le 2 * 10^5), the number of connections.

After that there are KK lines, each containing three integers XiX_i, YiY_i, and CiC_i (1Ci1091 \le C_i \le 10^9), where the strength of connection between the Ankylosaurus species XiX_i and the Brachiosaurus species YiY_i is CiC_i, for each i=1Ki=1\ldots K.

The next line contains the integer QQ (1Q21051 \le Q \le 2 * 10^5), the number of queries.

After that there are QQ lines, each containing two integers UiU_i, ViV_i, where UiU_i is the Ankylosaurus species and ViV_i is the Brachiosaurus species of the ii-th query, for each i=1Qi=1\ldots Q.

For tests worth 1515 points: N,M,K,Q1000N, M, K, Q \le 1000.

For tests worth 2525 more points: Q2000Q \le 2000, and for one of the dinosaur families all of its species has at most 5000 ancestors.

For tests worth 3535 more points: for one of the dinosaur families all of its species has at most one direct descendant.

Output

You need to print QQ lines, the ii-th line should contain an integer, the answer for the ii-th query in the given order.

Example

stdin

5
1 4 2 4
4
4 4 1
3
1 4 5
4 1 7
2 2 3
4
2 4
3 2
5 3
1 1

stdout

5
15
12
0

Notes

In the first query we are interested in Ankylosaurus species 22 (and its ancestors: 11), and Brachiosaurus species 44 (and its ancestors: 11). These have a single connection (1,4)(1, 4) of strength 55, so the answer is 55.

In the second query we are interested in Ankylosaurus species 33 (and its ancestors: 4,24, 2 and 11), and Brachiosaurus species 22 (and its ancestors: 44 and 11). All three connections are between some of these species, so the answer is 5+7+3=155 + 7 + 3 = 15.

In the third query we are interested in Ankylosaurus species 55 (and its ancestors: 4,24, 2 and 11), and Brachiosaurus species 33 (and its ancestors: 44 and 11). These have two connections, connection (1,4)(1, 4) of strength 55 and connection (4,1)(4, 1) of strength 77, so the answer is 5+7=125 + 7 = 12.

In the fourth query we are interested in Ankylosaurus species 11 (and its ancestors: none), and Brachiosaurus species 11 (and its ancestors: none). These don't have any connections, so the answer is 00.

Log in or sign up to be able to send submissions!