// clang-format off
#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (b)-1; i >= (a); i--)
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep(i, n) for(int i = (n)-1; i >= 0; i--)

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define pii pair<int, int>
#define vi vector<int>

using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T>& p){return o<<"("<<p.first<<","<<p.second<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T>& vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
using ll = long long;
constexpr ll MOD = 1000000007;

using Weight = long long;

struct Edge {
    std::size_t from;
    std::size_t to;
    Weight cost;
    bool operator<(const Edge& e)
    {
        return cost < e.cost;
    }
    bool operator>(const Edge& e)
    {
        return cost > e.cost;
    }
};

struct GraphList {
    GraphList(const std::size_t V) : V(V)
    {
        edge.resize(V);
    }
    void insert(const std::size_t from, const std::size_t to, const Weight cost = 1)
    {
        edge[from].push_back(Edge{from, to, cost});
    }
    GraphList reversed() const
    {
        GraphList g(V);
        for (std::size_t from = 0; from < V; from++) {
            for (const auto& e : edge[from]) {
                g.insert(e.to, from, e.cost);
            }
        }
        return g;
    }

    std::vector<std::vector<Edge> > edge;
    const std::size_t V;
};

void dfs1(const GraphList& g, const std::size_t s, std::size_t cmp[], std::stack<std::size_t>& st)
{
    cmp[s] = 0;
    for (const auto& e : g.edge[s]) {
        if (cmp[e.to]) {
            dfs1(g, e.to, cmp, st);
        }
    }
    st.push(s);
};

void dfs2(const GraphList& rg, const std::size_t s, const std::size_t index, std::size_t cmp[])
{
    cmp[s] = index;
    for (const auto& e : rg.edge[s]) {
        if (cmp[e.to] == std::numeric_limits<std::size_t>::max()) {
            dfs2(rg, e.to, index, cmp);
        }
    }
};

void SCC(const GraphList& g, std::size_t cmp[])
{
    std::stack<std::size_t> st;
    for (std::size_t i = 0; i < g.V; i++) {
        cmp[i] = 1;
    }

    for (std::size_t i = 0; i < g.V; i++) {
        if (cmp[i]) {
            dfs1(g, i, cmp, st);
        }
    }

    const GraphList rg = g.reversed();
    for (std::size_t i = 0; i < rg.V; i++) {
        cmp[i] = std::numeric_limits<std::size_t>::max();
    }

    std::size_t comp = 0;
    while (not st.empty()) {
        const std::size_t s = st.top();
        st.pop();
        if (cmp[s] == std::numeric_limits<std::size_t>::max()) {
            dfs2(rg, s, comp, cmp);
            comp++;
        }
    }
}


std::size_t cmp[10000];
int main()
{
    std::size_t V, E, Q;
    cin >> V >> E;
    GraphList g(V);
    for (std::size_t i = 0; i < E; i++) {
        std::size_t s, t;
        cin >> s >> t;
        g.insert(s, t);
    }
    SCC(g, cmp);

    cin >> Q;
    for (std::size_t i = 0; i < Q; i++) {
        std::size_t u, v;
        cin >> u >> v;
        if (cmp[u] == cmp[v]) {
            cout << 1 << endl;
        } else {
            cout << 0 << endl;
        }
    }

    return 0;
}

Measured memory_bytes: 5440