#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n, m;
vector<int> adj[MAXN];
int c[MAXN];

int d[18][MAXN];
int dp[MAXN][20];
void djk(int src, int idx){
    d[idx][src] = 0;
    queue<int> q;
    q.push(src);

    while(!q.empty()){
        int cur = q.front();
        q.pop();

        for(int j: adj[cur]){
            if(d[idx][j] <= d[idx][cur] + 1) 
                continue;
            d[idx][j] = d[idx][cur] + 1;
            q.push(j);
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    memset(d, 0x7f, sizeof(d));
    memset(dp, 0x7f, sizeof(dp));
    
    cin>>n>>m;

    for(int i = 0; i < m; i++){
        int a, b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    int k; 
    cin>>k;

    for(int i = 0; i < k; i++){
        cin>>c[i];
        djk(c[i], i);
        dp[(1<<i)][i] = 1;
    }

    for(int i = 0; i < (1<<k); i++){
        for(int j = 0; j < k; j++){
            if((1<<j) & i){
                for(int l = 0; l < k; l++){
                    if((1<<l) & i)
                        continue;
                    if(dp[i][j] < mod)
                        dp[i^(1<<l)][l] = min(dp[i^(1<<l)][l], dp[i][j] + d[j][c[l]]);
                }
            }
        }
    }

    int ans = mod;

    for(int i = 0; i < k; i++){
        ans = min(ans, dp[(1<<k) - 1][i]);
    }
    if(ans >= mod) ans = -1;

    cout<<ans<<endl;

    



    





    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
#include<bits/stdc++.h>
#define MAX 200000
#define MOD 1000000007
using namespace std;
vector<int> v[MAX+10];
vector<int> dist[MAX+10];
vector<int> path;
int in[MAX+10], out[MAX+10];
int count(int l,int r, int k)
{
	return upper_bound(dist[k].begin(),dist[k].end(),r)-lower_bound(dist[k].begin(),dist[k].end(),l);	
}
void dfs(int x,int e, int pr)
{
	path.push_back(e);
	in[x] = (int)path.size()-1;
	dist[e].push_back(in[x]);
	for(int y:v[x])
	{
		if(y==pr)
			continue;
		dfs(y,e+1,x);
	}
	out[x] = (int)path.size()-1;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	for(int i=2,x;i<=n;++i)
		cin>>x, v[i].push_back(x), v[x].push_back(i);
	dfs(1,0,0);
	int q;
	cin>>q;
	while(q--)
	{
		int x, n;
		cin>>x>>n;
		int l = in[x], r = out[x];
		cout<<count(l,r,n)<<endl;
	}
	return 0;
}