#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
const ll mod=1000000007;
const ll INF=1e15;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
typedef pair<ll,ll> P;
vec dx={1,0,-1,0};
vec dy={0,1,0,-1};

ll V,E;
const ll MaxV=10001;
vec G[MaxV],rG[MaxV];
vec vs;
bool used[MaxV], rused[MaxV];
ll cmp[MaxV];

void dfs(ll v){
    used[v]=true;
    rep(i,G[v].size()){
        ll a=G[v].at(i);
        if(!used[a]) dfs(a);
    }
    vs.pb(v);
    return;
}

void rdfs(ll v, ll k){
    used[v]=true;
    cmp[v]=k;
    rep(i,rG[v].size()){
        ll a=rG[v].at(i);
        if(!used[a]) rdfs(a,k);
    }
    return;
}

void scc(){
    rep(i,MaxV) used[i]=false;
    rep(i,V) if(!used[i]) dfs(i);
    ll k=1;
    rep(i,MaxV) used[i]=false;
    for(ll i=vs.size()-1;i>=0;i--){
        if(!used[vs.at(i)]) {
            rdfs(vs.at(i),k);
            k++;
        }
    }
    return;
}

int main(){
    cin>>V>>E;
    rep(i,E){
        ll s,t; cin>>s>>t;
        G[s].pb(t); 
        rG[t].pb(s);
    }
    scc();
    
    ll Q; cin>>Q;
    vec ans;
    rep(i,Q){
        ll s,t; cin>>s>>t;
        if(cmp[s]==cmp[t]) ans.pb(1);
        else ans.pb(0);
    }
    
    //for(ll i=vs.size()-1;i>=0;i--) cout<<vs.at(i)<<' ';
    //cout<<endl;
    //rep(i,V) cout<<i<<'*'<<cmp[i]<<endl;
    rep(i,Q) cout<<ans.at(i)<<endl;



}
