// AOJ GRL_3_C Connected Components - Strongly Connected Components
// 2018.4.26 bal4u

#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define MAX_V 10002
int  V;    // 頂点数
int  *to[MAX_V], hi[MAX_V];
int  *r_to[MAX_V], r_hi[MAX_V];
int  vs[MAX_V], vsz;
char used[MAX_V];
int  cmp[MAX_V];    // 属する強連結成分のトポロジカル順序

void add_edge(int _from, int _to)
{
	to[_from][hi[_from]++] = _to;
	r_to[_to][r_hi[_to]++] = _from;
}

void dfs(int v)
{
	int i;
	used[v] = 1;
	for (i = 0; i < hi[v]; i++) {
		if (!used[to[v][i]]) dfs(to[v][i]);
	}
	vs[vsz++] = v;
}

void rdfs(int v, int k)
{
	int i;
	used[v] = 1;
	cmp[v] = k;
	for (i = 0; i < r_hi[v]; i++) {
		if (!used[r_to[v][i]]) rdfs(r_to[v][i], k);
	}
}

#if 0
void init(int n)
{
	V = n;
	memset(  lim, 0, V<<2), memset(  hi, 0, V<<2);
	memset(r_lim, 0, V<<2), memset(r_hi, 0, V<<2);
}
#endif

// 強連結成分への分解
int scc()
{
	int v, i, k;
	memset(used, 0, V);
	vsz = 0;
	for (v = 0; v < V; v++) if (!used[v]) dfs(v);
	memset(used, 0, V);
	k = 0;
	for (i = vsz-1; i >= 0; i--) {
		if (!used[vs[i]]) rdfs(vs[i], k++);
	}
	return k;		// 得られた強連結成分の数
}

// バッファを経ずstdinから数値を得る
//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0, c = getchar_unlocked();
	do n = 10*n + (c & 0xf), c = getchar_unlocked(); while (c >= '0');
	return n;
}

int main()
{
	int E, Q, s, t, i, j;
	int *memo;

	V = in(), E = in();
	memo = malloc(V<<3);
	j = 0; for (i = 0; i < E; i++) {
		memo[j++] = s = in(), memo[j++] = t = in();
		hi[s]++, r_hi[t]++;

	}
	for (i = 0; i < V; i++) {
		if (  hi[i])   to[i] = malloc(  hi[i]<<2);
		if (r_hi[i]) r_to[i] = malloc(r_hi[i]<<2);
	}
	memset(hi, 0, V<<2), memset(r_hi, 0, V<<2);

	j = 0; while (E--) {
		s = memo[j++], t = memo[j++];
		to[s][hi[s]++] = t;
		r_to[t][r_hi[t]++] = s;
	}

	scc();

	Q = in();
	while (Q--) {
		s = in(), t = in();
		putchar('0'+ (cmp[s] == cmp[t]));
		putchar('\n');
	}
	return 0;
}
