#include<stdio.h>
#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <map>
#include <functional>
#include <stdio.h>
#include <array>
#include <algorithm>
#include <string>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <queue>
#include<iomanip>
#include<bitset>
#include<stack>
#include<set>
#include<limits>
#include <complex>
using namespace std;

const int mod = 1000000007;
struct Mod {
public:
	int num;
	Mod() : num(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) { ; }
	Mod(int n) : num((n % mod + mod) % mod) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	assert(b.num != 0);
	return Mod(a) * inv(b);
}
Mod operator/=(Mod &a, const Mod b) {
	assert(b.num != 0);
	return a = a * inv(b);
}

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < MAX_N - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}

vector<int>renketus;

vector<int>nums;
int now = 0;

vector<vector<int>>edges;
vector<vector<int>>revedges;

void dd(const int num) {
	nums[num] = -2;
	for (auto i : edges[num]) {
		if (nums[i] == -1) {
			dd(i);
		}
	}
	nums[num] = now;
	now++;
}

bool revdd(const int num,const int aa,const int from) {
	
	renketus[num] = aa;
	bool ok = false;
	for (auto i : revedges[num]) {
		if (renketus[i] == -1) {
			if (revdd(i, aa, from)) {
				ok = true;
			}
		}
		else if (i==from|| renketus[i] == aa) {
			ok = true;
		}
	}
	return ok;
}

int main() {
	int V, E; cin >> V >> E;
	nums.resize(V,-1);
	renketus.resize(V, -1);
	edges.resize(V);
	revedges.resize(V);
	for (int i = 0; i < E; ++i) {
		int f, t; cin >> f >> t;
		edges[f].push_back( t);
		revedges[t].push_back( f);
	}
	vector<int>que;
	que.push_back(0);
	for (int i = 0; i < V; ++i) {
		if (nums[i] == -1) {
			dd(i);
		}
	}

	int aaa = 0;
	vector<int>rnums(V);
	for (int i = 0; i < V; ++i) {
		rnums[nums[i]] = i;
	}
	reverse(rnums.begin(), rnums.end());
	for (int i = 0; i < V; ++i) {	
		if (renketus[rnums[i]] == -1) {

			renketus[rnums[i]] = aaa;
			revdd(rnums[i], aaa, rnums[i]);

			renketus[rnums[i]] = aaa;
			aaa++;
		}
	}
	


	int Q; cin >> Q;
	for (int i = 0; i < Q; ++i) {
		int a, b; cin >> a >> b;
		if (renketus[a] == renketus[b]) {
			cout << 1 << endl;
		}
		else {
			cout << 0 << endl;
		}
	}
	return 0;
}

Measured memory_bytes: 12292