|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|2 초||512 MB||0||0||0||0.000%|
Scientists are conducting research on the behavior of a newly discovered Agamic Cellular Microbe. This special kind of microbe is capable of massively reproducing by itself in a short time. The lifetime of an ACM consists of three phases:
At the beginning of the experiment, a newborn, single cell of ACM, is put into a suitable circumstance for its production. This cell, numbered as 0, starts to multiply and its descendants are numbered, starting from 1, according to their positions in the family hierarchy. During the experiment special equipment is used to record the numbers of the offspring generated by each of the ACM’s. The experiment is stopped after a certain time period.
Figure 1: The Family Tree of ACM’s in the First Case of Sample Input
Your task is to help the scientists to determine whether one ACM is an ancestor of another.
The input will contain multiple test cases. The first line of the input is a single integer T (1 ≤ T ≤ 20) which is the number of test cases. T test cases follow, each preceded by a single blank line.
Each test case starts with a single integer N (1 ≤ N ≤ 300, 000) which is the number of ACM’s that have their descendants recorded. The following N integers (not necessarily on a same line), Ci (0 ≤ i < N, 0 ≤ Ci ≤ 100), give the number of offspring of the i-th ACM. The next line contains an integer M (1 ≤ M ≤ 500, 000) which is the number of queries. M lines follow, each contains two distinct integers a and b, querying whether the a-th ACM is an ancestor of the b-th ACM.
The total number of ACM’s may be greater than N, but would never exceed 2,000,000.
For each case, print the number of queries which should be responded as yes, i.e. the a-th ACM is an ancestor of the b-th ACM.
2 6 3 2 1 1 0 2 5 0 1 2 4 3 5 1 8 6 9 5 2 0 3 0 1 4 2 6 1 6 2 3 3 5