#include <bits/stdc++.h>
using namespace std;

struct SCC {
    int N;
    vector<vector<int> > g, gr, g2i, t;
    vector<bool> visited;
    vector<int> i2g;
    stack<int> order;

    SCC(){}
    SCC(int n){init(n);}
    void init(int n){
        N = n;
        g.clear();
        g.resize(N);
        gr.clear();
        gr.resize(N);
        visited.resize(N);
        i2g.resize(N);
    }
    void add_edge(int u, int v) {
        g[u].emplace_back(v);
        gr[v].emplace_back(u);
    }

    void dfs(int x) {
        if (visited[x]) return;
        visited[x] = true;
        for (int i : g[x]) dfs(i);
        order.push(x);
    }

    void rdfs(int x, int k) {
        if (i2g[x] != -1) return;
        i2g[x] = k;
        for (int i : gr[x]) rdfs(i, k);
    }

    void build() {
        fill(visited.begin(), visited.end(), false);
        fill(i2g.begin(), i2g.end(), -1);
        for (int i = 0; i < N; i++) dfs(i);
        int p = 0;
        while (!order.empty()) {
            rdfs(order.top(), p++);
            order.pop();
        }
    }
};

int main(){
    int V, E, Q;
    cin >> V >> E;
    SCC scc(V);
    for(int i=0;i<E;i++){
        int a, b;
        cin >> a >> b;
        scc.add_edge(a,b);
    }
    scc.build();
    cin >> Q;
    for(int i=0;i<Q;i++){
        int a, b;
        cin >> a >> b;
        cout << int(scc.i2g[a] == scc.i2g[b]) << endl;
    }
    return 0;
}
