#include <bits/stdc++.h>
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define nyan "(=^･ω･^=)"
#define mkp make_pair
#define lP pair<ll, ll>
#define iP pair<int,int>
typedef long long ll;
using namespace std;

int V, E, s, t, Q, a, b, G[10000];
bool B[10000];
vector<int>v[10000], r[10000], u;

void dfs(int x) {
	B[x] = true;
	for (int i = 0; i != v[x].size(); ++i) {
		if (!B[v[x][i]])
			dfs(v[x][i]);
	}
	u.push_back(x);
}
void rdfs(int x, int k) {
	G[x] = k;
	for (int i = 0; i != r[x].size(); ++i) {
		if (!G[r[x][i]])
			rdfs(r[x][i], k);
	}
}
int main() {
	cin >> V >> E;
	for (int i = 0; i != E; ++i) {
		cin >> s >> t;
		v[s].push_back(t);
		r[t].push_back(s);
	}
	for (int i = 0; i != V; ++i) {
		if (!B[i])
			dfs(i);
	}
	int k = 1;
	for (int i = u.size() - 1; i != -1; --i) {
		if (!G[u[i]])
			rdfs(u[i], ++k);
	}
	cin >> Q;
	for (int i = 0; i != Q; ++i) {
		cin >> a >> b;
		cout << (G[a] == G[b]) << endl;
	}
	return 0;
}
