Task
In DinoLand there were two major dinosaur families: Ankylosaurus and Brachiosaurus. The family of Ankylosaurus' consists of species, numbered from to , while the family of Brachiosaurus' consists of species, numbered from to .
The families started out as their original species, which is species 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 and fossils of a Brachiosaurus species have been found near each other times already, then the strength of connection between species and is , where 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 and a Brachiosaurus species , 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 (), the number of Ankylosaurus species.
The second line contains integers , where Ankylosaurus species is a direct descendant of the species for each .
The third line contains the integer (), the number of Brachiosaurus species.
The fourth line contains integers , where Brachiosaurus species is a direct descendant of the species for each .
The fifth line contains the integer (), the number of connections.
After that there are lines, each containing three integers , , and (), where the strength of connection between the Ankylosaurus species and the Brachiosaurus species is , for each .
The next line contains the integer (), the number of queries.
After that there are lines, each containing two integers , , where is the Ankylosaurus species and is the Brachiosaurus species of the -th query, for each .
For tests worth points: .
For tests worth more points: , and for one of the dinosaur families all of its species has at most 5000 ancestors.
For tests worth more points: for one of the dinosaur families all of its species has at most one direct descendant.
Output
You need to print lines, the -th line should contain an integer, the answer for the -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 (and its ancestors: ), and Brachiosaurus species (and its ancestors: ). These have a single connection of strength , so the answer is .
In the second query we are interested in Ankylosaurus species (and its ancestors: and ), and Brachiosaurus species (and its ancestors: and ). All three connections are between some of these species, so the answer is .
In the third query we are interested in Ankylosaurus species (and its ancestors: and ), and Brachiosaurus species (and its ancestors: and ). These have two connections, connection of strength and connection of strength , so the answer is .
In the fourth query we are interested in Ankylosaurus species (and its ancestors: none), and Brachiosaurus species (and its ancestors: none). These don't have any connections, so the answer is .