#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const double EPS = 1e-9;

class StronglyConnectedComponentDecomposition {
private:
    std::vector<bool> used;
    std::vector<std::vector<int>> graph, revgraph;
    std::vector<int> comp;
    std::vector<int> sortNode;
    
    void dfs(int now) {
        used[now] = true;
        for (const int e : graph[now]) {
            if (!used[e]) dfs(e);
        }
        sortNode.emplace_back(now);
        return;
    }
    void dfs2(int now, int cnt) {
        used[now] = true;
        for (const int e : revgraph[now]) {
            if (!used[e]) dfs2(e, cnt);
        }
        comp[now] = cnt;
        return;
    }
public:
    StronglyConnectedComponentDecomposition() {}
    StronglyConnectedComponentDecomposition(const int n) : used(n, false), graph(n), revgraph(n), comp(n, 0) {}
    void resize(const int n) {
         used.resize(n, false);
         graph.resize(n);
         revgraph.resize(n);
         comp.resize(n, 0);
    }
    void addEdge(const int from, const int to) {
        graph[from].emplace_back(to);
        revgraph[to].emplace_back(from);
        return;
    }
    int solve() {
        std::fill(std::begin(used), std::end(used), false);
        for (int i = 0; i < (int)used.size(); i++) {
            if (!used[i]) dfs(i);
        }
        std::fill(std::begin(used), std::end(used), false);
        std::reverse(std::begin(sortNode), std::end(sortNode));
        int res = 0;
        for (const int n : sortNode) {
            if (!used[n]) dfs2(n, res++);
        }
        return res;
    }
    std::vector<int> getComp() {
        return this->comp;
    }
    bool same(const int a, const int b) {
        return comp[a] == comp[b];
    }
};

int V, E;
StronglyConnectedComponentDecomposition scc;

int main(void) {
    scanf("%d%d", &V, &E);
    scc.resize(V);
    for (int i = 0; i < E; i++) {
        int s, t;
        scanf("%d%d", &s, &t);
        scc.addEdge(s, t);
    }
    scc.solve();
    int Q;
    scanf("%d", &Q);
    for (int i = 0; i < Q; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        printf("%d\n", scc.same(u, v));
    }
}

Measured memory_bytes: 4564