#include "bits/stdc++.h"

// Begin Header {{{
#define let             const auto
#define all(x)          (x).begin(), (x).end()
#define rep(i, n)       for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t)   for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t)   for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define var(Type, ...)  Type __VA_ARGS__; input(__VA_ARGS__)

using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template<class T, class U>inline bool chmax(T &a, const U &b){return b>a && (a=b, true);}
template<class T, class U>inline bool chmin(T &a, const U &b){return b<a && (a=b, true);}
inline i64  sigma(i64 n)            { return (n * (n + 1) >> 1); }
inline i64  updiv(i64 a, i64 b)     { return (a + b - 1) / b; }
inline i64  sqr(i64 n)              { return n * n; }
inline string to_string(char c)     { return string(1, c); }
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T>
inline vector<T> makeVec(size_t sz) { return vector<T>(sz); }

template <class T, class... Args>
inline auto makeVec(size_t sz, Args... args) {
    return vector<decltype(makeVec<T>(args...))>(sz, makeVec<T>(args...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v) {
    for(auto &e:t) fill_v(e,v);
}

template <class T>
inline void input(T &x) { cin >> x; }

template <class T>
inline void input(vector<T> &vec) { for (auto &e : vec) cin >> e; }

template <class Head, class... Tail>
inline void input(Head &head, Tail&... tail) { cin >> head; input(tail...); }

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T, class U>
inline ostream& operator<< (ostream &out, const pair<T, U> &p) {
    return out << p.first << ' ' << p.second;
}

template <class T>
inline ostream& operator<< (ostream &out, const vector<T> &vec) {
    static constexpr const char *delim[] = { " ", "" };
    for (const auto &e : vec) out << e << delim[&e == &vec.back()];
    return out;
}

template <class T>
inline ostream& operator<< (ostream &out, const vector<vector<T>> &mat) {
    static constexpr const char *tail[] = { "\n", "" };
    for (const auto &row : mat) out << row << tail[&row == &mat.back()];
    return out;
}

template <class T>
void trace_(const char *s, T&& x) {
    clog << '{';
    while(*s != '\0') clog << *(s++);
    clog << ":" << setw(3) << x << '}' << endl;
}

template <class Head, class... Tail>
void trace_(const char *s, Head&& head, Tail&&... tail) {
    clog << '{';
    while(*s != ',') clog << *(s++);
    clog << ":" << setw(3) << head << "}, ";
    for (++s; !isgraph(*s); ++s);
    trace_(s, std::forward<Tail>(tail)...);
}
// }}} End Header

class StronglyConnectedComponents {
private:
    const int V;
    vector<vector<int>> gg, rg;
    vector<int> topoOrder;
    int sccCnt;

public:
    // ノード番号 [0, n) を管理する
    StronglyConnectedComponents(int n) noexcept :
        V(n),
        gg(V), rg(V),
        topoOrder(V, -1),
        sccCnt(0) {}

    // fromからtoへ辺を張る
    inline void addEdge(int from, int to) noexcept {
        gg[from].push_back(to);
        rg[to].push_back(from);
    }

    // ノード v のトポロジカル順序。 [0, sccCount) 未満の値を取る。
    inline int operator[](int v) const noexcept {
        return topoOrder[v];
    }

    // 各ノードのトポロジカル順序を求める(=どの強連結成分に属するか求める)
    void build() noexcept {
        vector<int> used(V, 0);
        vector<int> visitedNodes;
        visitedNodes.reserve(V);
        sccCnt = 0;

        for (int i = 0; i < V; ++i) {
            dfs(i, visitedNodes, used);
        }
        for (auto it = visitedNodes.crbegin(); it != visitedNodes.crend(); ++it) {
            if (topoOrder[*it] == -1) rdfs(*it, sccCnt++);
        }
    }

    // 強連結成分の個数
    inline int sccCount() const noexcept {
        return sccCnt;
    }

    // 強連結成分ごとに縮約した結果のDAGを新しく作る
    // ノード番号が小さいほどトポロジカル順序が早い
    // (2つのノード番号a, bについて、a < b なら b から a へ行けない)
    vector<vector<int>> createDAG() const noexcept {
        vector<vector<int>> C(sccCnt);
        for (int v = 0; v < V; ++v) {
            for (const int &to : gg[v]) {
                if (topoOrder[v] != topoOrder[to]) {
                    C[topoOrder[v]].push_back(topoOrder[to]);
                }
            }
        }
        for (int i = 0; i < sccCnt; ++i) {
            sort(C[i].begin(), C[i].end());
            C[i].erase(unique(C[i].begin(), C[i].end()), C[i].end());
        }
        return C;
    }

private:
    void dfs(int v, vector<int> &visitedNodes, vector<int> &used) noexcept {
        if (used[v]++) return;
        for (const int nxt : gg[v]) dfs(nxt, visitedNodes, used);
        visitedNodes.push_back(v);
    }

    void rdfs(int v, int cnt) noexcept {
        if (topoOrder[v] != -1) return;
        topoOrder[v] = cnt;
        for (const int nxt : rg[v]) rdfs(nxt, cnt);
    }
};

using namespace std;

int GRL_3_C()
{
    cin.tie(nullptr); ios::sync_with_stdio(false);
    
    var(int, V, E);

    StronglyConnectedComponents scc(V);

    rep(i, E) {
        var(int, s, t);
        scc.addEdge(s, t);
    }

    scc.build();

    var(int, Q);
    while(Q--) {
        var(int, u, v);
        print(scc[u] == scc[v]);
    }

    return 0;
}

int main()
{
    return GRL_3_C();
}




Measured memory_bytes: 4396