#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<complex>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

class Edge{
public:
    Int from, to, rev;
    Int cost, lim;
    Edge(Int from, Int to, Int cost, Int lim = -1, Int rev = -1):
        from(from),to(to),cost(cost),lim(lim),rev(rev){}
};


class Graph{
public:
    Int n;
    Int m;
    vector<vector<Edge>> edge;
    Graph(Int n = 0):n(n),m(0){
        edge = vector<vector<Edge>>(n);
    }
    void add_edge(Int from, Int to, Int cost = 1, Int lim = -1)
        {
            if(lim == -1){
                edge[from].push_back(Edge(from, to, cost));
                return;
            }
            edge[from].push_back(Edge(from, to, cost, lim, edge[to].size()));
            edge[to].push_back(Edge(to, from, -cost, 0, edge[from].size() - 1));
        }
};


class SCC{
public:
    Graph *g_;
    Graph rev;
    Graph h;
    vector<int> nodes;
    vector<int> come;
    vector<int> group;
    vector<vector<int>> tmp_edge;
    set<P> edges;
    SCC(Graph &g):come(g.n, false), group(g.n), tmp_edge(g.n), rev(g.n){
        g_ = &g;
        for(int v = 0;v < g.n;v++)dfs(v), group[v] = v;
        reverse(nodes.begin(), nodes.end());
        fill(come.begin(), come.end(), false);
        for(int v:nodes)dfs2(v);
        for(int v = 0;v < g.n;v++)
            for(auto e:g.edge[v])
                edges.insert(P(group[e.from], group[e.to]));
        for(P e:edges)
            tmp_edge[e.first].push_back(e.second);
        nodes.clear();
        fill(come.begin(), come.end(), false);
        for(int v = 0;v < g.n;v++)dfs3(group[v]);
        reverse(nodes.begin(), nodes.end());
        vector<int> rev(g.n);
        h = Graph(nodes.size());
        for(int i = 0;i < h.n;i++)rev[nodes[i]] = i;
        for(P e:edges)
            h.add_edge(rev[group[e.first]], rev[group[e.second]]);
        for(int i = 0;i < g.n;i++){
            group[i] = rev[group[i]];
        }
    }

    void dfs(int x){
        if(come[x])return;
        come[x] = true;
        for(auto &e:g_->edge[x]){
            dfs(e.to);
            rev.add_edge(e.to, e.from);
        }
        nodes.push_back(x);
    }

    void dfs2(int x){
        if(come[x])return;
        come[x] = true;
        for(auto &e:rev.edge[x]){
            if(!come[e.to]){
                group[e.to] = group[x];
                dfs2(e.to);
            }
        }
    }

    void dfs3(int x){
        if(come[x])return;
        come[x] = true;
        for(auto to:tmp_edge[x]){
            dfs3(to);
        }
        nodes.push_back(x);
    }
    
};

void scc(Graph &g){
    vector<int> nodes;
    
}

int main(){
    int v, e, s, t, q;
    cin >> v >> e;
    Graph g(v);
    for(int i = 0;i < e;i++){
        cin >> s >> t;
        g.add_edge(s, t);
    }
    SCC scc(g);
    Graph h = scc.h;
    cin >> q;
    for(int i = 0;i < q;i++){
        cin >> s >> t;
        cout << (scc.group[s] == scc.group[t]) << endl;
    }
    return 0;
}


Measured memory_bytes: 6044