#include <iostream>
#include <vector>
#include <queue>
using namespace std;

vector< vector< int > > g;
vector< vector< int > > rvs_g; //reversed_graph
vector< int > f;
vector< int > label;
priority_queue< pair< int, int > > pque;
int cnt;


void rec1(int v){
	
	f[v] = 0;
	
	for(vector< int >::iterator it = g[v].begin(); it != g[v].end(); ++it){
		if(f[*it] == -1){
			rec1(*it);
		}
	}
	
	f[v] = cnt;
	
	pque.push(make_pair(f[v], v));
	
	++cnt;
	
	return;
}

void rec2(int v, int k){
	
	label[v]= k;
	
	for(vector< int >::iterator it = rvs_g[v].begin(); it != rvs_g[v].end(); ++it){
		if(label[*it] == 0){
			rec2(*it, k);
		}
	}
}

int main(){
	int v, e, s, t, q, x, y;
	
	cin >> v >> e;
	
	g.resize(v);
	rvs_g.resize(v);
	f.resize(v);
	label.resize(v);
	
	for(int i = 0; i < e; ++i){
		cin >> s >> t;
		g[s].push_back(t);
		rvs_g[t].push_back(s);
	}
	
	for(int i = 0; i < v; ++i){
		f[i] = -1;
	}
	
	cnt = 1;
	for(int i = 0; i < v; ++i){
		if(f[i] == -1){
			rec1(i);
		}
	}
	
	while(!pque.empty()){
		int i = pque.top().second;
		pque.pop();
		
		if(label[i] == 0){
			rec2(i, f[i]);
		}
	}
	
	cin >> q;
	for(int i = 0; i < q; ++i){
		cin >> x >> y;
		
		if(label[x] == label[y]){
			cout << 1 << endl;
		}else{
			cout << 0 << endl;
		}
	}
	
	return 0;
}