#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define ld long double
#define forr(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) forr(i, 0, n)
#define repp(i, n) forr(i, 1, n + 1)
#define foreach(it, t) for (__typeof(t.begin()) it = t.begin(); it != t.end(); it++)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define init(a, i) memset(a, i, sizeof(a))
#define pii pair<int, int>
#define vvi vector<vector<int>>
#define MAXN 0x3f3f3f3f
int min(int a, int b)
{
    if (a < b)
        return a;
    else
        return b;
}
int max(int a, int b)
{
    if (a > b)
        return a;
    else
        return b;
}
int n, p, q, u, d[200005], cur = -1, B;
int l[200005], r[200005];
int deep[200005], num[200005], cnt[200005], ans[200005];
vvi v;
vector<pair<pii, int>> s;
void dfs(int k, int dp)
{
    cur++;
    deep[cur] = dp;
    num[k] = cur;
    l[num[k]] = cur;
    rep(i, v[k].size()) dfs(v[k][i], dp + 1);
    r[num[k]] = cur;
}
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b)
{
    int a1 = a.first.first, b1 = a.first.second, a2 = b.first.first, b2 = b.first.second;
    if (a1 / B != a2 / B)
        return a1 / B < a2 / B;
    if (b1 != b2)
        return b1 < b2;
    return a1 < a2;
}
void solve()
{
    cin >> n;
    B = sqrt(n);
    v.resize(n);
    rep(i, n - 1)
    {
        cin >> p;
        v[p - 1].pb(i + 1);
    }
    dfs(0, 0);
    cin >> q;
    rep(i, q)
    {
        cin >> u >> d[i];
        u--;
        u = num[u];
        s.pb(mp(mp(l[u], r[u]), i));
    }
    sort(s.begin(), s.end(), cmp);
    int x = 0, y = -1;
    rep(i, q)
    {
        int L = s[i].first.first, R = s[i].first.second;
        while (y < R)
            cnt[deep[++y]]++;
        while (y > R)
            cnt[deep[y--]]--;
        while (x < L)
            cnt[deep[x++]]--;
        while (x > L)
            cnt[deep[--x]]++;
        ans[s[i].second] = cnt[d[s[i].second]];
    }
    rep(i, q) cout << ans[i] << endl;
}
int main()
{
    int T;
    T = 1;
    //cin>>T;
    while (T--)
        solve();
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
double dp[100][100][100];
double dfs(ll a,ll b,ll c){
    if(a==99 && b==99 && c==99)return 1.0;
    if(a==100 || b==100 || c==100)return 0.0;
    if(dp[a][b][c]!=0)return dp[a][b][c];
    double ans=0.0;
    if(a>0)ans+=((double)a/(a+b+c))*(1+dfs(a+1,b,c));
    if(b>0)ans+=((double)b/(a+b+c))*(1+dfs(a,b+1,c));
    if(c>0)ans+=((double)c/(a+b+c))*(1+dfs(a,b,c+1));
    //cout<<a<<" "<<b<<" "<<c<<" "<<ans<<endl;
    return dp[a][b][c]=ans;
}
int main(){
    ll a,b,c;
    cin>>a>>b>>c;
    memset(dp,0,sizeof(dp));
    double ans=dfs(a,b,c);
    cout<<fixed<<setprecision(10)<<ans<<endl;
    return 0;
}

