#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define endl "\n"
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
const int N = 2e5 + 5;
vector<int> adj[N], depth[N];
vector<int> in(N), out(N);
int timer = 0;

void dfs(int node, int par, int d)
{
    in[node] = timer++;
    depth[d].pb(in[node]);
    for (auto child : adj[node])
    {
        if (child != par)
            dfs(child, node, d + 1);
    }
    out[node] = timer++;
}

void solve()
{
    int n, q, x, y;
    cin >> n;
    for (int i = 2; i <= n; i++)
    {
        cin >> x;
        adj[i].pb(x);
        adj[x].pb(i);
    }
    dfs(1, 0, 0);
    cin >> q;
    rep(i, q)
    {
        cin >> x >> y;
        vector<int> v = depth[y];
        auto it = lower_bound(v.begin(), v.end(), out[x]) - lower_bound(v.begin(), v.end(), in[x]);
        cout << it<<endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();

    return 0;
}
#include <bits/stdc++.h>
#define ll long long
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define P pair<ll,ll>
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
#define per(i,n) for(ll (i)=(n)-1;(i)>=0;--(i))
#define all(x) (x).begin(),(x).end()
using namespace std;

std::vector<int> Eratosthenes( const int N )
{
    std::vector<bool> is_prime( N + 1 );
    for( int i = 0; i <= N; i++ )
    {
        is_prime[ i ] = true;
    }
    std::vector<int> p;
    for( int i = 2; i <= N; i++ )
    {
        if( is_prime[ i ] )
        {
            for( int j = 2 * i; j <= N; j += i )
            {
                is_prime[ j ] = false;
            }
            p.emplace_back( i );
        }
    }
    return p;
}

long long gcd(long long a, long long b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}

int main(){
    ll n;
    cin>>n;
    V x(n);
    rep(i,n)cin>>x[i];
    vector<int> pri=Eratosthenes(50);
    ll N=pri.size();
    ll ans=1e18;
    ll tmp=1;
    for(ll bit=0;bit<(1<<N);bit++){
        ll num=1;
        rep(k,N){
            if(1<<k&bit){
                num*=pri[k];
            }
        }
        if(num==1)continue;
        bool ok=true;
        rep(i,n){
            if(gcd(num,x[i])==1){
                ok=false;
                break;
            }
        }
        if(ok)ans=min(ans,num);
    }
    cout<<ans<<endl;
}
