#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int n = 10005; // グラフの頂点の個数
int c = 0; //強連結成分
vector<vector<int>> graph(n,vector<int>()); //普通のグラフ
vector<vector<int>> revgraph(n,vector<int>());//逆辺を貼ったグラフ
vector<bool> seen(n,false); //dfsですでにその頂点を見たかどうかの判定
vector<int> postorder; //１回目のdfsでのpostorder
vector<int> sccgroup(n,-1); //強連結分解index 小さいほうがDAGの前の方 

void dfs(int i){
    seen[i] = true;
    for(int v:graph[i]){
        if(seen[v]) continue;
        dfs(v);
    }
    postorder.push_back(i);
}

void revdfs(int i){
    sccgroup[i] = c;
    seen[i] = true;
    for(int v:revgraph[i]){
        if(seen[v]) continue;
        revdfs(v);
    }
}

void scc(){
    for(int i=0;i<n;i++){
        if(!seen[i]) dfs(i);
    }

    for(int i=0;i<n;i++) seen[i] = false;
    for(int i=n-1;i>=0;i--){
        if(!seen[postorder[i]]){
            revdfs(postorder[i]);
            ++c;
        }
    }
}

int main(){
    int e;
    cin >> n >> e;
    rep(i,e){
        int s,t;
        cin >> s >> t;
        graph[s].push_back(t);
        revgraph[t].push_back(s);
    }

    scc();

    int q;
    cin >> q;
    while(q--){
        int u,v;
        cin >> u >> v;
        if(sccgroup[u] == sccgroup[v]) cout << 1 << endl;
        else cout << 0 << endl;
    }    
    return 0;
}
