#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main(){cin.tie(0);ios::sync_with_stdio(0);_main();}
//---------------------------------------------------------------------------------------------------
template<int MV> struct SCC {
    vector<vector<int> > SC; int NV, GR[MV], rev[MV]; vector<int> E[MV], RE[MV], NUM; int vis[MV];
    void init(int NV) { this->NV = NV; for (int i = 0; i<MV; i++) { E[i].clear(); RE[i].clear(); } }
    void add_edge(int x, int y) { E[x].push_back(y); RE[y].push_back(x); }
    void dfs(int cu) { vis[cu] = 1;
        for (int i = 0; i<E[cu].size(); i++) if (!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
    void revdfs(int cu, int ind) { int i; vis[cu] = 1; GR[cu] = ind; SC[ind].push_back(cu);
        rev[cu] = ind; rep(i, 0, RE[cu].size()) if (!vis[RE[cu][i]]) revdfs(RE[cu][i], ind); }
    void scc() { int c = 0; SC.clear(); SC.resize(MV); NUM.clear();
        rep(i, 0, MV) vis[i] = 0; for (int i = 0; i < NV; i++) if (!vis[i]) dfs(i);
        rep(i, 0, MV) vis[i] = 0; for (int i = NUM.size() - 1; i >= 0; i--) if (!vis[NUM[i]]) {
        SC[c].clear(); revdfs(NUM[i], c); sort(SC[c].begin(), SC[c].end()); c++;} SC.resize(c); }};
template<int NV> struct UnionFind {
    int par[NV], cnt[NV]; UnionFind() { rep(i, 0, NV) par[i] = i, cnt[i] = 1; }
    int operator[](int x) { return par[x] == x ? x : par[x] = operator[](par[x]); }
    void operator()(int x, int y) {
        x = operator[](x); y = operator[](y);
        if (x != y) par[x] = y, cnt[y] += cnt[x];
    }
}; // x???y????????£?????????
struct TopologicalSort {
    vector<set<int>> E; TopologicalSort(int N) { E.resize(N); }
    void add_edge(int a, int b) { E[a].insert(b); }
    bool visit(int v, vector<int>& order, vector<int>& color) { color[v] = 1;
        for (int u : E[v]) { if (color[u] == 2) continue; if (color[u] == 1) return false;
        if (!visit(u, order, color)) return false; } order.push_back(v); color[v] = 2; return true; }
    bool sort(vector<int> &order) { int n = E.size(); vector<int> color(n);
        for (int u = 0; u < n; u++) if (!color[u] && !visit(u, order, color)) return false;
        reverse(order.begin(), order.end()); return true; } };
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§  
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i     
?????????????????????????????? ???  |???|     
????????? /?????? /??£??£??£??£/??????|  
??? ???_(__??????/???    ???/ .| .|????????????  
??? ????????????/????????????/??????u??????  
---------------------------------------------------------------------------------------------------*/




//---------------------------------------------------------------------------------------------------
void _main() {
    int V, E; cin >> V >> E;
    SCC<10101> scc;
    scc.init(V);
    rep(i, 0, E) {
        int a, b; cin >> a >> b;
        scc.add_edge(a, b);
    }
    scc.scc();

    int Q; cin >> Q;
    rep(i, 0, Q) {
        int a, b; cin >> a >> b;
        if (scc.GR[a] == scc.GR[b]) printf("1\n");
        else printf("0\n");
    }
}

Measured memory_bytes: 4672