#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second

int n;
int p[200005];
vector<int> adj[200005];

int q;
pair<pair<int,int>,int> qq[200005];

int dep[200005];
bool vis[200005];
map<int,int> cnt[200005];

int ans[200005];

int dfs(int u) {
    if (dep[u] != 0) return dep[u];
    else return dep[u] = dfs(p[u]) + 1;
}

void presolve() {
    fill(vis, vis + n + 1, 0);
    for (int u = 1; u <= n; u++) {
        if (adj[u].size() == 0) {
            vis[u] = 1;
            cnt[u][dep[u]] = 1;
        }
    }
}

void recur(int u) {
    if (vis[u]) return;
    vis[u] = 1;
    cnt[u][dep[u]] = 1;

    for (int v : adj[u]) {
        recur(v);

        if (cnt[u].size() < cnt[v].size()) {
            swap(cnt[u], cnt[v]);
        }

        for (auto it : cnt[v]) {
            cnt[u][it.fi] += it.se;
        }
        cnt[v].clear();
    }
}

int solve(int u, int d) {
    recur(u);
    if (cnt[u].count(d)) return cnt[u][d];
    else return 0;
}

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

    cin >> n;
    for (int i = 2; i <= n; i++) {
        cin >> p[i];
        adj[p[i]].push_back(i);
    }

    dep[1] = 1;
    for (int i = 2; i <= n; i++) {
        dfs(i);
    }

    cin >> q;
    for (int i = 0; i < q; i++) {
        auto& cur = qq[i];
        cin >> cur.fi.fi >> cur.fi.se;
        ++cur.fi.se;
        cur.se = i;
    }

    sort(qq, qq + q, [&] (auto x, auto y) {
        return dep[x.fi.fi] > dep[y.fi.fi];
    });

    presolve();
    for (int i = 0; i < q; i++) {
        ans[qq[i].se] = solve(qq[i].fi.fi, qq[i].fi.se);
    }

    for (int i = 0; i < q; i++) {
        cout << ans[i] << "\n";
    }

    return 0;
}
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define rrep(i,a,b) for (int i = a - 1; i >= b; i--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ll long long
#define dbwt(x) std::cout << fixed << setprecision(15) << x << '\n' 
#define Wt(x) cout << x << '\n'
#define re0 return 0
#define Int(...) int __VA_ARGS__; in(__VA_ARGS__)
#define Ll(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; in(__VA_ARGS__)
#define Vec(t,a,s) vector<t>a(s); in(a)
#define Vec2(t,a,b,s) vector<t>a(s),b(s);rep(i,0,s)in(a[i],b[i])
#define Vec3(t,a,b,c,s) vector<t>a(s),b(s),c(s);rep(i,0,s)in(a[i],b[i],c[i])
#define Vec4(t,a,b,c,d,s) vector<t>a(s),b(s),c(s),d(s);rep(i,0,s) in(a[i],b[i],c[i],d[i])
#define mvec(t,a,i) vector<t>(a,i)
#define mvec2(t,a,b,i) vector<vector<t>>(a,vector<t>(b,i))
void scan(int& x) {std::cin >> x;}
void scan(ll& x) {std::cin >> x;}
void scan(double& x) {std::cin >> x;}
void scan(std::string& x) {std::cin >> x;}
void scan(std::vector<int>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void scan(std::vector<ll>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void in(){}
template<class Head, class... Tail>
void in(Head& head, Tail&... tail) {scan(head); in(tail...);}
template<class T, class U>
bool chmax(T& a, U b) {if(a < b){a=b; return true;} return false;}
template<class T, class U>
bool chmin(T& a, U b) {if(a > b){a=b; return true;} return false;}
using namespace std;
template<class T>
using vvec = vector<vector<T>>;
template<class T>
void emitvec(vector<T> a) {rep (i, 0, a.size()) cout << a[i] << " "; cout << endl;}
struct IoSetup {
   	IoSetup() {
     	cin.tie(nullptr);
     	ios::sync_with_stdio(false);
     	cout << fixed << setprecision(15);
   	}
} iosetup;

//----------------------------------------------------------------//

int N, M;
map<pair<int, int>, int> mp;
vector<vector<int>> g(100005);

vector<int> col(100005, -1);
vector<bool> visited(100005, false);

void dfs(int v) {
    visited[v] = true;
    for (int to: g[v]) {
        if (visited[to]) continue;
        if (col[v] == mp[{v, to}]) {
            col[to] = (col[v] == 1 ? N : 1);
        }
        else {
            col[to] = mp[{v, to}];
        }
        dfs(to);
    }
}

int main() {
    cin >> N >> M;
    Vec3(int, u, v, c, M);

    rep (i, 0, M) {
        mp[{u[i], v[i]}] = mp[{v[i], u[i]}] = c[i];
        g[u[i]].pb(v[i]);
        g[v[i]].pb(u[i]);
    }

    col[1] = 1;
    dfs(1);

    rep (i, 1, N + 1) cout << col[i] << endl; 

}