#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

typedef std::vector<std::vector<int>> graph;

std::vector<int> makescc(const graph& g)
{
	const int n = g.size();
	std::vector<int> num(n), low(n), scc(n, -1);
	std::vector<bool> visited(n);
	std::stack<int> r, s;
	int index = 0;

	for (int u = 0; u < n; ++u) {
		if (visited[u]) {
			continue;
		}
		r.push(u);
		while (!r.empty()) {
			const int v = r.top();
			if (num[v] == 0) {
				s.push(v);
				num[v] = low[v] = ++index;
				for (const int w : g[v]) {
					if (num[w] == 0) {
						r.push(w);
					}
				}
				continue;
			}
			r.pop();
			if (visited[v]) {
				continue;
			}
			visited[v] = true;
			for (const int w : g[v]) {
				if (scc[w] == -1) {
					low[v] = std::min(low[v], low[w]);
				}
			}
			if (num[v] == low[v]) {
				int w;
				do {
					w = s.top();
					s.pop();
					scc[w] = v;
				} while (w != v);
			}
		}
	}

	return scc;
}

int main()
{
	std::cin.tie(0);
	std::ios::sync_with_stdio(0);

	int V, E;
	std::cin >> V >> E;
	graph g(V);
	for (int i = 0; i < E; ++i) {
		int s, t;
		std::cin >> s >> t;
		g[s].push_back(t);
	}
	auto scc = makescc(g);

	int Q;
	std::cin >> Q;
	for (int i = 0; i < Q; ++i) {
		int u, v;
		std::cin >> u >> v;
		std::cout << (scc[u] == scc[v]) << '\n';
	}
}