Functions overriding, is a well-known concept, when we are using inheritance in Object-Oriented Programming (OOP). For those who are not familiar with OOP, I will recall a few things to perhaps refresh your memory.
As you might have guessed, we are interested in finding out in which class the function is written when some instance of a particular class calls it.
The input is in the following format:

n (2 ≤ n ≤ 105) is the number of classes. Classes are numbered from 1 to n. pi (1 ≤ pi ≤ i - 1) is the parent class of class i. Class 1 is the root class, superclass of all classes. It has no parent.
ti denotes the number of functions written in class i, including both new functions and overriding functions. Then follows ai1, ai2, ..., aiti a list of these functions. Functions are also denoted using positive integers. It's guaranteed that every number will appear at most once in one list. 1 ≤ aij ≤ 106, 0 ≤ ti ≤ 106,
.
q (1 ≤ q ≤ 105) is the query number. Then follows q queries. ui, ri (1 ≤ ui ≤ n, 1 ≤ ri ≤ 106) is the i-th query, asking when an instance of class ui calls function ri, in which class is this function written?
For each query, print answer. If it is illegal, that is, a "runtime error" is raised, then output - 1.
5
1 2 3 3
2 2 1
0
2 5 2
2 4 5
1 5
4
3 4
5 2
4 5
1 3
-1
3
4
-1
The sample is equivalent to the following Java code.
class Class1 {
void function2() { System.out.println("1"); }
void function1() { System.out.println("1"); }
}
class Class2 extends Class1 {
}
class Class3 extends Class2 {
void function5() { System.out.println("3"); }
void function2() { System.out.println("3"); }
}
class Class4 extends Class3 {
void function4() { System.out.println("4"); }
void function5() { System.out.println("4"); }
}
class Class5 extends Class3 {
void function5() { System.out.println("5"); }
}
void test() {
new Class3().function4();
new Class5().function2();
new Class4().function5();
new Class1().function3();
}
Some of the tests in the raw problem package have been removed due to the "Maximal summary testset file size exceeded" error on Codeforces.