#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORq(i, m, n) for(int i = (m);i <= (n);++i)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define SCD3(m,n,k) scanf("%d%d%d",&m,&n,&k)
#define PB push_back
#define MP make_pair
#define ARSCD(A,N) REP(i,N){SCD(A[i]);}
#define ARSCD1(A,N) FORq(i,1,N){SCD(A[i]);}
#define PRINTD(n) printf("%d\n",n)
#define PRINTLLD(n) printf("%lld\n",n)
#define DEBUG printf("%s\n","debug")
#define fst first
#define snd second
#define IN(x,S) (S.count(x) != 0)
using namespace std;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long ll;
//////////////////////////////////////////////////////
const int V_MAX = 10001;
const int E_MAX = 30001;

static VI adj[V_MAX];
static VI cadj[V_MAX];
static VI ord1;
static bool visited[V_MAX] = {};
static int grp[V_MAX];

stack<int> S;

static int V,E;

void dfs(int v){ // 1 dfs
	if (visited[v]) return;
	visited[v] = true;
	REP(i,adj[v].size()){
		if (!visited[adj[v][i]]) dfs(adj[v][i]);
	}
	
	S.push(v);
}

void dfs2(int v,int u){
	visited[v] = true;
	grp[v] = u;
	REP(i,cadj[v].size()){
		if (!visited[cadj[v][i]]) dfs2(cadj[v][i],u);
	}
}

int scc(){
	memset(visited,0,sizeof(visited));
	//S.clear();
	REP(i,V){
		if (!visited[i]) dfs(i);
	}
	
	memset(visited,0,sizeof(visited));
	int cind = 0;
	while(!S.empty()){
		int s = S.top(); S.pop();
		if (!visited[s]){
			dfs2(s,cind);
			cind++;
		}
	}	
	return cind;
}

int main(){

	SCD2(V,E);
	
	REP(i,E){
		int s,t; SCD2(s,t); // 0-indexed
		adj[s].PB(t);
		cadj[t].PB(s);
	}
	
	scc();
	
	//output
	int Q; SCD(Q);
	
	REP(i,Q){
		int u,v; SCD2(u,v);
		if (grp[u] == grp[v]) PRINTD(1);
		else PRINTD(0);
	}
	
	
	return 0;
}

