/**
 *    author:  Ujjwal Tripathi
 *    created: 29.05.2021 16:07:47
**/

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

#define ar array<ll, 2>
#define ll long long
#define in insert
#define pb push_back
#define pii pair<ll,ll>
#define vt vector
#define P_Q(x) priority_queue<x>
#define p_q(x) priority_queue<x, vector<x>, greater<x>>
#define For(i, n) for(ll i = 0; i < n; i++)
#define Rev(i, n) for(ll i = n-1; i >= 0; i--)
#define FOR(i, n) for(ll i = 1; i <= n; i++)
#define REV(i, n) for(ll i = n; i >= 1; i--)
#define Rep(i,a,b) for(ll i = a; i <= b; i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int) (x).size()

// Maximum priority queue
template <typename T>
using mxpq = priority_queue<T>;

// Minimum priority queue
template <typename T>
using mnpq = priority_queue<T, vt<T>, greater<T>>;


// x | (x + 1) sets lowest unset bit of x
// x & (x - 1) unsets lowest set bit of x

const ll mod = 1e9L + 7;
const ll INF = 1e18L + 5;
const ll N = 2e5 + 1;

ll timer=0;
// remember to read no of test_cases
void solve()
{
  ll n;
  cin>>n;
   //vt<ll> a(n+1,0);
   vt<ll> g[N+1];
   vt<ll> depth[N];
   vt<ll> intime(N);
   vt<ll> outtime(N);
   for(int i=2;i<=n;i++)
   {
      ll x;
      cin>>x;
      g[x].pb(i);
   }
   function<void(int i,int dep)> dfs=[&](int i,int dep=0)
   {
      intime[i]=timer++;
      depth[dep].pb(intime[i]);
      for(auto x:g[i])
      {
         dfs(x,dep+1);
      }
      outtime[i]=timer;
   };
   dfs(1,0);
   ll q;
   cin>>q;
   for(int i=0;i<q;i++)
   {
     ll x,y;
     cin>>x>>y;
     cout<<lower_bound(all(depth[y]),outtime[x])-lower_bound(all(depth[y]),intime[x])<<endl;
   }
   return;
}

int main()
{
  ios::sync_with_stdio(0);  cin.tie(0);
  ll t = 1;
  // cin >> t;
  FOR(tt, t) solve();
  return 0;
}






#include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i,n) for(int i=0; i<(n); i++)
#define ALL(n) n.begin(), n.end()

int main(){
    int n; cin >> n;
    double x0, y0, xn, yn;
    cin >> x0 >> y0;
    cin >> xn >> yn;
    
    double cx=(x0+xn)/2, cy=(y0+yn)/2;

    double move_x=x0-cx, move_y=y0-cy;

    // 角度を求める
    double sita=2*M_PI/n;

    // 回転する
    double rotation_x = move_x*cos(sita) - move_y*sin(sita);
    double rotation_y = move_x*sin(sita) + move_y*cos(sita);

    // 平行移動
    rotation_x += cx, rotation_y += cy;

    printf("%.10f %.10f\n", rotation_x, rotation_y);
}