/**
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_C&lang=jp
 */

// 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 = bool;

template <std::size_t SIZE>
struct GraphArray {
    GraphArray(const std::size_t V) : V(V)
    {
        for (std::size_t i = 0; i < V; i++) {
            for (std::size_t j = 0; j < V; j++) {
                weight[i][j] = 0;
            }
        }
    }
    const std::size_t V;
    Weight weight[SIZE][SIZE];
};

struct Edge {
    std::size_t from;
    std::size_t to;
    Weight cost;
};

template <std::size_t SIZE>
struct GraphList {
    GraphList(const std::size_t V) : V(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;
    }

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

// ?????£?????????????§£
template <std::size_t SIZE>
void dfs1(const GraphList<SIZE>& g, const std::size_t s, std::size_t cmp[SIZE], 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);
};

template <std::size_t SIZE>
void dfs2(const GraphList<SIZE>& rg, const std::size_t s, const std::size_t index, std::size_t cmp[SIZE])
{
    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);
        }
    }
};

template <std::size_t SIZE>
void SCC(const GraphList<SIZE>& g, std::size_t cmp[SIZE])
{
    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<SIZE> rg = g.reversed();
    for (std::size_t i = 0; i < g.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++;
        }
    }
}

constexpr std::size_t MAX = 10000;
std::size_t cmp[MAX];

int main()
{
    std::size_t V, E, Q;
    cin >> V >> E;
    GraphList<MAX> g(V);
    rep(i, E){
        std::size_t s, t;
        cin >> s >> t;
        g.insert(s, t);
    }
    SCC(g, cmp);
    cin >> Q;
    rep(i, Q){
        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: 5652