The Humans are at war against a very powerful alien race that invaded our solar system. 
The human army has n soldiers. The soldiers are numbers from 1 to n. The army has a superiority hierarchy. Every soldier has one immediate superior. The superior of a superior of a soldier is also a superior to that soldier. So, a soldier may have one or more superiors but only one immediate superior.

When a soldier has to pass a message to his superior, he cannot do so directly. If a soldier ‘x’ has to pass a message to his superior ‘y’, ‘x’ has to first pass the message to his immediate superior who will review the message. After reviewing, the immediate superior of ‘x’ will pass on the message to ‘y’ following the same procedure described above.

Given the two soldiers, ’x’ and ‘y’, your job is to determine how many reviews will take place until the message reaches ‘y’.

NOTE: Among all soldiers there is one soldier who does not have any superior. He is the commander of the whole army.

Input:

The first line of the input contains t, the number of test cases.
The first line of each test case contains n, the number of soldiers. The next line contains n space separated integers. The ith integer represents the immediate superior of the ith soldier.

The immediate superior of the commander of the army will be '0'.

The third line of each test case contains an integer q, the number of queries.
Each of the next q lines contain the two integers, x and y (x!=y). 

Output:

For each query output a single integer on a line that is the number of reviews that’ll take place before the message reaches from ‘x’ to ‘y’.
In case ‘y’ is not the superior of ‘x’, print -1.

Constraints:
1 ≤ t ≤ 10
2 ≤ n ≤ 100000
1 ≤ q ≤ 100000
1 ≤ x,y ≤ n  

It is guaranteed that the commander of the army is a superior of all soldiers.

WARNING: Large Input data. Be careful with certain languages.
( For C++ users, use 'scanf' instead of 'cin')

SAMPLE INPUT
1
5
0 1 1 2 4
4
5 1
4 2
2 3
1 3

SAMPLE OUTPUT
2
0
-1
-1

Explanation

Soldier 5 will pass on the message to soldier 4, who will pass the message to soldier 2, and then soldier 1 will receive the message. 2 reviews took place.

Soldier 4 can directly pass the message to soldier 2. So, no reviews.

Soldier 3 is not the superior of Soldier 2, so, the answer is -1.

Soldier 3 is not the superior of Soldier 1, so, the answer is -1.
def bfs(x):
    b=[False]*(n+1)# an array of false flags to keep track of vertices visited
    #memo=[-1]*(n+1)
    global memo,component_id,root
    #found=False
    c=0
    q=[]
    q.append(x)
    component_id[x]=c
    #root=0
    memo[x]=0
    while len(q)!=0 :
        #t=s[-1]
        top=q.pop()
        #print "top",top
        #l=t
        b[top]=True
        #q.pop(0)
        for j in G[top]:
            if memo[j]==-2 :
                
                    memo[j]=1+memo[top]
                    if root<memo[j]:
                        root=memo[j]
                    b[j]=True
                    q.append(j)
                    component_id[j]=top
                
                    
def trail(i,j):
    global component_id,p
    found=False
    while memo[i]>=memo[j] and not found:
        if j==component_id[i]:
            found=True
        i=component_id[i] 
    return found          
t=input()

while t!=0:
 n=int(raw_input())
 m=map(int,raw_input().split())
 G=[[] for i in xrange(n+1)]
 root=0
 memo=[-2]*(n+1)
 component_id=[-2]*(n+1)
 memo[0]=-1
 component_id[0]=-1
 p=0
 for i in xrange(n):
     if m[i]==0:
         p=i+1
     G[i+1].append(m[i])
     G[m[i]].append(i+1)
 #print p    
 #print G    
 bfs(p)
 #print memo
 #print component_id
 #print root
 query=int(raw_input())
 for _ in xrange(query):
     x,y=map(int,raw_input().split())
     if memo[x]-memo[y]>0:
        if root==n-1:
            print memo[x]-memo[y]-1
        else:    
             
        
          if trail(x,y)==True:
            print memo[x]-memo[y]-1
          else:
            print -1    
                 
     else:
          print -1 
             
 #print memo
 #print component_id
 t-=1   
 
    #print memo[i]