#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
using namespace std;
const long long INF = 1LL<<60; // 仮想的な無限大の値;
using ll = long long;
using P  = pair<int, int>;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
ll gcd(ll a, ll b){
    if(b == 0) return a;
    return gcd(b, a%b);
}
int main()
{
    int n;
    cin >> n;
    vll a(n);
    rep(i, n) cin >> a[i];
    ll x = a[0];
    rep(i, n){
        x = gcd(x, a[i]);
    }
    cout << x << endl;
    // ll MAX = -1, MIN = INF;
    // multiset<ll> X;
    // rep(i, n){
    //     cin >> a[i];
    //     chmax(MAX, a[i]);
    //     chmin(MIN, a[i]);
    //     X.insert(a[i]);
    // } 
    // rep(i, n){
    //     ll max = *X.rbegin();
    //     if(a[i] == max){
    //         cout << a[i] << endl;return 0;
    //     }
    //     rep(i, X.count(max)) X.insert(*X.rbegin()-*X.begin());
    //     X.erase(max);    
    // }
    // ll ans = *X.begin();
    // cout << *X.begin() << endl;return 0;
    return 0;
}
#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

vector<llvec> e;
llvec p;

using PP = pair<ll, P>;
vector<ll> d;
llvec tour;
vector<P> range;

void dfs(ll i){
  range[i].fi = tour.size();
  tour.push_back(d[i]);
  for(auto ie: e[i]){
    d[ie] = d[i] + 1;
    dfs(ie);
  }
  //tour.push_back(d[i]);
  range[i].se = tour.size();
  return;
}

/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N;
  cin >> N;
  p = llvec(N);
  d = llvec(N, -1);

  
  e = vector<llvec>(N);
  //tour = llvec(N);
  range = vector<P>(N);
  
  rep(i, N-1){
    ll t;
    cin >> t;
    t--;
    e[t].push_back(i+1);
  }
  d[0] = 0;
  dfs(0);
  
  ll Q;
  cin >> Q;
  vector<P> v(Q);
  
  rep(i, Q){
    ll u, t;
    cin >> u >> t;
    u--;
    v[i] = {u, t};
  }
  
  vector<llvec> dd(N+1);
  ll cnt = 0;
  for(auto i: tour){
    dd[i].push_back(cnt);
    cnt++;
  }
  
  for(auto iv: v){
    ll num = lower_bound(ALL(dd[iv.se]), range[iv.fi].se) - lower_bound(ALL(dd[iv.se]), range[iv.fi].fi);
    cout << num << endl;
  }
  
  return 0;
}
