//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <type_traits>

using namespace std;

#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}

using LL = long long;

//------------------------------------------
//------------------------------------------

template<class T>
struct Edge {
    int from, to;
    T cost;

    Edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}

    operator int() const { return to; }
};

template<class T>
using Edges = vector<Edge<T>>;
template<class T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template<class T>
using DistMatrix = vector<vector<T>>;


template<typename G>
struct StrongConnectedComponents {

    /// @bried graph SCCする元グラフ
    const G &graph;
    int V;

    /// @brief graph SCC用の内部グラフ
    UnWeightedGraph go_g, rv_g;

    /// @brief comp comp[v] = vの所属するSCCの番号 トポロジカル順
    vector<int> comp;

    /// @brief dfs用
    vector<bool> used;

    /// @brief dfsの帰りがけ順
    vector<int> order;

    StrongConnectedComponents(G &graph) :
            graph(graph), V(graph.size()), go_g(V), rv_g(V), comp(V, -1), used(V, false) {
        for (int i = 0; i < V; i++) {
            for (auto e: graph[i]) {
                go_g[i].emplace_back((int) e);
                rv_g[(int) e].emplace_back(i);
            }
        }
    }

    /// @brief 頂点vの所属するSCCのIDのを返す
    int operator[](int v) {
        return comp[v];
    }

    void dfs(int v) {
        if (used[v])return;
        used[v] = true;
        for (auto to: go_g[v]) dfs(to);
        order.emplace_back(v);
    }

    void rdfs(int v, int cnt) {
        if (comp[v] != -1) return;
        comp[v] = cnt;
        for (auto to: rv_g[v]) rdfs(to, cnt);
    }

    vector<int> get_comp() const {
        return comp;
    }

    /// @param gt 参照で空グラフを与える gt[SCCID]が繋がる他の頂点
    void build(UnWeightedGraph &gt) {
        for (int i = 0; i < V; i++) dfs(i);
        reverse(order.begin(), order.end());
        int cnt = 0;
        for (auto v: order) if (comp[v] == -1) rdfs(v, cnt), cnt++;
        gt.resize(cnt);
        for (int i = 0; i < V; i++) {
            for (auto &to: graph[i]) {
                int x = comp[i];
                int y = comp[(int) to];
                if (x == y) continue;
                gt[x].push_back(y);
            }
        }
    }

};

int main() {

    int V, E;
    cin >> V >> E;

    UnWeightedGraph graph(V), buff;
    for (int i = 0; i < E; i++) {
        int s, t;
        cin >> s >> t;
        graph[s].push_back(t);
    }

    StrongConnectedComponents<UnWeightedGraph> scc(graph);
    scc.build(buff);

    int Q;
    cin >> Q;

    while (Q--) {
        int s, t;
        cin >> s >> t;

        if (scc[s] == scc[t]) cout << 1 << endl;
        else cout << 0 << endl;
    }

    return 0;
}





































Measured memory_bytes: 5016