#include <bits/stdc++.h>
using namespace std;

int n,m,u,v;
int f[100005];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
void join(int x,int y){
	int fx=find(x),fy=find(y);
	if(fx!=fy)f[fx]=fy;
}
vector<int>e[100005];
vector<int>e2[100005];
bool ok[100005];
vector<int>q;
void dfs(int i){
	ok[i]=1;
	for(int j=0;j<e[i].size();j++)
		if(!ok[e[i][j]])dfs(e[i][j]);
	q.push_back(i);
}
void dfs2(int i,int top){
	ok[i]=0;
	for(int j=0;j<e2[i].size();j++)
		if(ok[e2[i][j]])dfs2(e2[i][j],top);
	join(i,top);
}
void solve(){
	for(int i=0;i<=n;i++)f[i]=i;
	for(int i=0;i<n;i++)if(!ok[i])dfs(i);
	for(int i=n-1;i>=0;i--)if(ok[q[i]])dfs2(q[i],q[i]);
}
int main(){
//	freopen("a.txt","r",stdin);
	cin.tie(0);
	cin>>n>>m;
	while(m--){
		cin>>u>>v;
		e[u].push_back(v);
		e2[v].push_back(u);
	}
	solve();
	cin>>m;
	while(m--){
		cin>>u>>v;
		if(find(u)!=find(v))cout<<0<<endl;
		else cout<<1<<endl;
	}
	return 0;
}
