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

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::stack<int> s;
	int index = 0;

	std::function<void(int)> visit = [&](int v) -> void {
		s.push(v);
		num[v] = low[v] = ++index;
		for (const int u : g[v]) {
			if (scc[u] == -1) {
				if (num[u] == 0) {
					visit(u);
				}
				low[v] = std::min(low[v], low[u]);
			}
		}

		if (num[v] == low[v]) {
			int u;
			do {
				u = s.top();
				s.pop();
				scc[u] = v;
			} while (u != v);
		}
	};

	for (int v = 0; v < n; ++v) {
		if (num[v] == 0) {
			visit(v);
		}
	}

	return scc;
}

int main()
{
	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';
	}
}