#include<bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(false)
int a[200000], vs[200000],vs1[100006],p[300000];
vector<int>v[200000];
queue<int>q;
int f(int n)
{
    int i, k=0;
    vs[n]=1; q.push(n);
    while(q.size()){
            n=q.front(); q.pop();
    for(i=0; i<v[n].size(); i++){
        if(vs1[v[n][i]]==1){
        p[v[n][i]]=n;
        q.push(v[n][i]);
    }
    else vs1[v[n][i]]--;
    }
    }
    return 1;
}
int main()
{
    IO;
    int i, j, m, k, n, t, l=0, mx,ll,mxx,mx1;
    cin>>n>>m;
    for(i=1; i<=n+m-1; i++){
        cin>>j>>k;
        v[j].push_back(k);
        vs1[k]++;
    }
    for(i=1; i<=n; i++)
        if(vs1[i]==0)k=i;
    f(k);
    for(i=1; i<=n; i++) cout<<p[i]<<endl;
    return 0;

}

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i <= n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define ALL(v) (v).begin(),(v).end()
 
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

const ll inf = 1e18;


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n;
    cin >> n;
    vll x(n);
    REP(i, n) cin >> x[i];
    ll l;
    cin >> l;

    // reach[k][i] : i から出発して 2^k 日で到達できる一番遠いホテル
    vvll reach(31, vll(n));

    REP(i, n){
        ll nxt = upper_bound(ALL(x), x[i] + l) - x.begin() - 1;
        reach[0][i] = nxt;
    }
    REP(k, 30){
        REP(i, n){
            reach[k + 1][i] = reach[k][reach[k][i]];
        }
    }
    ll q;
    cin >> q;
    ll a, b;
    while (q) {
        cin >> a >> b;
        a--, b--;
        if (a > b) swap(a, b);
        ll res = 1;
        REPR(k, 30){
            if (reach[k][a] < b) {
                res += (1 << k);
                a = reach[k][a];
            }
        }
        cout << res << endl;
        q--;
    }
    return 0;
}