// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define forn(i, s, n)  for(ll i=(ll)s; i<(ll)(n); i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define lld long double
#define F first
#define S second
#define PI 3.141592653589793238
#define prec(n) fixed<<setprecision(n)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); }
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

void itval(istream_iterator<string> it) {}
template<typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    itval(++it, args...);
}
const ll MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {cout << x << "\n";}
template <typename T>
inline void printvec(T x) {for (auto a : x)cout << a << ' '; cout << '\n';}
// -----------------------------------------------------------------------

struct custom {
    bool operator()(const pair<int, pii> &p1, const pair<int, pii> &p2)const {
        if (p1.F == p2.F)
            return p1.S.F > p2.S.F;
        return p1.F > p2.F;
    }
};

// Calculate a^b % MOD -------------------------------------------------
ll get_pow(ll a, ll b, ll M = MOD) {
    ll res = 1;
    while (b) {
        if (b & 1) res = (res * a) % M;
        a = (a * a) % M;
        b >>= 1;
    }
    return res;
}
// ---------------------------------------------------------------------
const ll N = 2e5 + 5, inf = 2e18;
vector<int> adj[N];
int tin[N], tout[N], tim, height[N];
std::vector<int> have[N];
void dfs(int s, int p, int h)
{
    tin[s] = ++tim;
    height[s] = h;
    have[h].pb(tin[s]);
    for (auto x : adj[s])
    {
        if (x != p)
        {
            dfs(x, s, h + 1);
        }
    }
    tout[s] = ++tim;
}
void solve()
{
    int n, x, y, q;
    cin >> n;
    forn(i, 1, n)
    {
        cin >> x;
        x--;
        adj[x].pb(i);
        adj[i].pb(x);
    }
    dfs(0, -1, 0);
    cin >> q;
    forn(i, 0, q)
    {
        cin >> x >> y;
        x--;
        if (height[x] > y)
        {
            cout << 0 << '\n';
            continue;
        }
        cout << upper_bound(all(have[y]), tout[x]) - lower_bound(all(have[y]), tin[x]) << '\n';
    }
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int test = 1;
    clock_t z = clock();
    // cin >> test;
    forn(tes, 0, test)
    {

        solve();
    }
    debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
    return 0;
}
/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Train
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_M = 100000;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<pii> vpii;

/* global variables */

int ts[MAX_M], ks[MAX_M];
vpii nbrs[MAX_N];
ll ds[MAX_N];

/* subroutines */

/* main */

int main() {
  int n, m, st, gl;
  scanf("%d%d%d%d", &n, &m, &st, &gl);
  st--, gl--;

  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d%d%d", &a, &b, ts + i, ks + i);
    a--, b--;
    nbrs[a].push_back(pii(b, i));
    nbrs[b].push_back(pii(a, i));
  }

  fill(ds, ds + n, LINF);
  ds[st] = 0;

  priority_queue<pli> q;
  q.push(pli(0, st));

  while (! q.empty()) {
    pli u = q.top(); q.pop();
    ll ud = -u.first;
    int ui = u.second;
    if (ds[ui] != ud) continue;
    if (ui == gl) break;

    vpii &nbru = nbrs[ui];
    for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
      int vi = vit->first, ei = vit->second;
      ll vd = (ud + ks[ei] - 1) / ks[ei] * ks[ei] + ts[ei];
      if (ds[vi] > vd) {
	ds[vi] = vd;
	q.push(pli(-vd, vi));
      }
    }
  }

  printf("%lld\n", (ds[gl] < LINF) ? ds[gl] : -1LL);
  return 0;
}
