#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <stack>
#include <limits>
#include <array>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <memory>
#include <numeric>
#include <tuple>

constexpr long long mod = static_cast<long long>(1e9 + 7);

void dfs1(int i, const std::vector<std::vector<int>>& edges, std::vector<std::pair<int, int>>& memo, int& cnt) {
    memo[i] = {i, 0};
    for(auto v: edges[i]) {
        if(memo[v].second < 0) dfs1(v, edges, memo, cnt);
    }
    memo[i].second = cnt++;
}

void dfs2(int i, const std::vector<std::vector<int>>& edges, std::vector<int>& memo, int cnt) {
    memo[i] = cnt;
    for(auto v: edges[i]) {
        if(memo[v] < 0) dfs2(v, edges, memo, cnt);
    }
}

int main() {
    int V, E;
    std::cin >> V >> E;
    std::vector<std::vector<int>> edges(V);
    std::vector<std::vector<int>> redges(V);
    while(E--) {
        int s, t;
        std::cin >> s >> t;
        edges[s].push_back(t);
        redges[t].push_back(s);
    }
    int cnt = 0;
    std::vector<std::pair<int, int>> memo(V, {-1, -1});
    for(int i = 0; i < V; ++i) {
        if(memo[i].second < 0) dfs1(i, edges, memo, cnt);
    }
    std::sort(begin(memo), end(memo), [](auto x, auto y){return x.second > y.second;});
    std::vector<int> ans(V, -1);
    cnt = 0;
    for(auto val: memo) {
        if(ans[val.first] < 0) {
            dfs2(val.first, redges, ans, cnt++);
        }
    }

    int Q;
    std::cin >> Q;
    while(Q--) {
        int u, v;
        std::cin >> u >> v;
        std::cout << (ans[u] == ans[v] ? 1 : 0) << "\n";
    }
    std::cout << std::flush;

    return 0;
}


#include <climits>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

void dfs(int u, vector<vector<int> >& G, int& t, vector<int>& ord, vector<int>& low, int& k, vector<int>& cmp, stack<int>& s) {
	ord[u] = low[u] = t++;
	s.push(u);
	for (int v: G[u]) {
		if (ord[v] == -1) {
			dfs(v, G, t, ord, low, k, cmp, s);
			low[u] = min(low[u], low[v]);
		}
		else if (cmp[v] == -1)
			low[u] = min(low[u], ord[v]);
	}
	if (ord[u] == low[u]) {
		for (;;) {
			int v = s.top(); s.pop();
			cmp[v] = k;
			if (v == u) break;
		}
		k++;
	}
}

vector<int> scc(vector<vector<int> >& G) {
	int N = G.size(), t = 0, k = 0;
	vector<int> ord(N, -1), low(N), cmp(N, -1);
	stack<int> s;
	for (int u = 0; u < N; u++)
		if (ord[u] == -1)
			dfs(u, G, t, ord, low, k, cmp, s);
	return cmp;
}

int main() {
	int N; cin >> N;
	vector<vector<int> > G(N);
	int M; cin >> M;
	while (M--) {
		int u, v; scanf("%d%d", &u, &v);
		G[u].push_back(v);
	}
	vector<int> cmp = scc(G);
	int Q; cin >> Q;
	while (Q--) {
		int u, v; scanf("%d%d", &u, &v);
		printf("%d\n", cmp[u] == cmp[v]);
	}
}