#include using namespace std; void input(string f) { string one = f + ".in"; string two = f + ".out"; freopen(one.c_str(), "r", stdin); freopen(two.c_str(), "w", stdout); } const int N = 1e5 + 7; const int K = 27; const int inf = 1e9; struct for_q{ int u, fuel, price; bool pr; for_q(){ } for_q(int _u, int _fuel, int _price, bool _pr){ u = _u; fuel = _fuel; price = _price; pr = _pr; } friend bool operator<(for_q lvalue, for_q rvalue){ return lvalue.price > rvalue.price; } }; int d[N]; bool used[N][K][2]; int best[N][K][2]; int res[N]; vector adj[N]; int main(){ ios::sync_with_stdio(false); cin.tie(NULL); input("space"); int n, m, s, k; cin >> n >> m >> s >> k; for(int i = 2; i <= n; i++){ cin >> d[i]; } for(int i = 0; i < m; i++){ int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } priority_queue q; for_q beg = for_q(s, 1, 0, false); for(int i = 1; i <= n; i++){ res[i] = inf; } q.push(beg); used[beg.u][beg.fuel][beg.pr] = true; best[beg.u][beg.fuel][beg.pr] = beg.price; res[s] = 0; while(!q.empty()){ for_q f = q.top(); q.pop(); //cout << f.u << " " << f.fuel << " " << f.price << " " << f.pr << endl; res[f.u] = min(res[f.u], f.price); for_q to; if(f.u != 1){ to.u = d[f.u]; to.price = f.price; to.fuel = min(f.fuel + 1, k); to.pr = false; if(!used[to.u][to.fuel][to.pr] || best[to.u][to.fuel][to.pr] > to.price){ best[to.u][to.fuel][to.pr] = to.price; used[to.u][to.fuel][to.pr] = true; q.push(to); } } if(f.fuel){ for(int v: adj[f.u]){ to.u = v; if(f.pr){ to.price = f.price; } else{ to.price = f.price + 1; } to.fuel = f.fuel - 1; to.pr = true; if(!used[to.u][to.fuel][to.pr] || best[to.u][to.fuel][to.pr] > to.price){ best[to.u][to.fuel][to.pr] = to.price; used[to.u][to.fuel][to.pr] = true; q.push(to); } } } } int qq; cin >> qq; for(int i = 0; i < qq; i++){ int x; cin >> x; if(res[x] >= inf){ cout << "-1 "; } else{ cout << res[x] << " "; } } cout << "\n"; return 0; }