#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, n) for (int i = (int)(l); i < (int)(n); i++)
#define drep(i, l, n) for (int i = (int)(n); i > (int)(l); i--)
#define INF INT_MAX
#define def (200007)
#define MOD (1000000007)
typedef vector<int> veci;
typedef vector<vector<int>> Veci;
typedef vector<int64_t> vecit;
typedef vector<vector<int64_t>> Vecit;
typedef int64_t intt;
typedef vector<vector<double>> Vecd;
typedef vector<double> vecd;
typedef pair<int, int> P;
/*veci のときdist.resize(N) Veciのとき2回resize;
dp.assign(N,-1);*/
    
int main(){
  int a,b,c;cin>>a>>b>>c;
  int at;
  if(a<b){
    at=a;
    a=b;
    b=at;
  }
  if(b<c){
    at=b;b=c;c=at;
  }
  if(a<b){
    at=a;a=b;b=at;
  }
  if((a-b)==(b-c))cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
  return 0;
}
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")

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

typedef long double ld;
typedef long long int ll;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;

#define ff first
#define ss second
#define pb push_back
#define int long long   // Make sure to use ll everywhere while commenting this, to avoid overflow
#define sz(v) (int)v.size()
#define inf 2147483647
#define llinf 9223372036854775807
#define all(v) v.begin(),v.end()
#define bp(n) __builtin_popcountll(n)
#define f(i,l,r) for(long long i=l;i<=r;i++)
#define rf(i,r,l) for(long long i=r;i>=l;i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }


const int N = 2e5 + 5, mod = 1e9 + 7, bit = 61;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getRand(int l, int r)
{
	uniform_int_distribution<int> uid(l, r);
	return uid(rng);
}

int p[N], dep[N], tin[N], tout[N], tim = 0;
vector<int> adj[N], store[N];
int n;

void dfs(int u, int par, int d)
{
	dep[u] = d;
	tin[u] = ++tim;
	store[dep[u]].pb(tin[u]);
	for (auto &v : adj[u])
	{
		if (v == par)
		{
			continue;
		}
		dfs(v, u, d + 1);
	}
	tout[u] = tim;
}

signed main()
{
	fast;


	int t = 1;
	// cin >> t;
	f(tc, 1, t)
	{
		cin >> n;
		f(i, 2, n)
		{
			cin >> p[i];
			adj[p[i]].pb(i);
			adj[i].pb(p[i]);
		}
		dfs(1, 0, 0);
		f(i, 0, n - 1)
		{
			sort(all(store[i]));
		}
		int q;
		cin >> q;
		f(i, 1, q)
		{
			int u, d;
			cin >> u >> d;
			int ans = upper_bound(all(store[d]), tout[u]) - upper_bound(all(store[d]), tin[u] - 1);
			cout << ans << endl;
		}
	}
	return 0;
}