// Original C++ solution (slower):
#include <vector>



#include <iostream>

#define rep(i,n) for(int i=0; i<(n); i++)



using namespace std;



typedef vector<int> vi;



// ?????£?????????????§£ (Strongly Connected Component)

// ????????°???????????°????????????????????£??¬??????????????? ???????????§

struct SCCGraph: public vector<vi> {



	vector<int> order;    // ????????????????????????(?????£?????????????£?????????????)

	vector<int> vs;   // ??°????????????

	vector<bool> used;    // ?????§???????????????



	using vector::vector;



	// a -> b ????????????????????????push_back()??§?????????

	void add_edge(int a, int b){

		(*this)[a].push_back(b);

	}



	// ??¢??°???????????§?????????scc(v)??¨????????????????????????

	vector<int> operator()(vector<vi> v){

		(*this).assign( v.begin(), v.end() );

		scc();

		return order;

	}



	// scc()??§??????

	void dfs(int n, int k, vector<vi> &v, vector<vi> &rv){

		used[n] = true;

		order[n] = k;

		for(auto t: v[n]){

			if( !rv.empty() ) rv[t].push_back(n);	// ???????????°???????????????

			if( !used[t] ) dfs(t, k, v, rv);

		}

		vs.push_back(n);

	}



	// ?????£?????????????§£?????????

	int scc(){

		int N = size();

		used.assign(N, false);

		order.resize(N);

		vs.clear();

		vector<vi> rG(N), tmp;   // ????????????????????°????????¨

		for(int n=0; n < N; n++){

			if( !used[n] ) dfs(n, n, (*this), rG);

		}

		used.assign(N, false);

		int k = 0;

		for(int i = vs.size()-1; i >= 0; i--){

			if( !used[ vs[i] ] ) dfs(vs[i], k++, rG, tmp);

		}

		return k++;

	}



	// ?±????????????£????????????????????????????????????

	bool find(int x, int y){

		return order[x] == order[y];

	}



} scc(0);



// 2-SAT ??????

class TwoSAT{

};





// test

// AOJ GRL_3_C (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_C&lang=jp)

int main(){

	int V, E;

	cin >> V >> E;

	//SCCGraph graph(V);

	vector<vi> edge(10005);

	rep(i,E){

		int s, t;

		cin >> s >> t;

		//graph.add_edge( s, t );

		edge[s].push_back( t );

	}



	//graph.scc();

	//rep(i,V) cout << "graph: " << i << " " << graph.order[i] << endl;



	auto g = scc(edge);



	int Q;

	cin >> Q;

	rep(i,Q){

		int u, v;

		cin >> u >> v;

		//cout << (int)graph.find(u, v) << endl;

		cout << (int)(g[u] == g[v]) << endl;

	}



	return 0;

}

// Optimized solution to the same problem (faster):
#include <vector>



#include <iostream>

#define rep(i,n) for(int i=0; i<(n); i++)



using namespace std;



typedef vector<int> vi;



// ?????£?????????????§£ (Strongly Connected Component)

// ????????°???????????°????????????????????£??¬??????????????? ???????????§

struct SCCGraph: public vector<vi> {



	vector<int> order;    // ????????????????????????(?????£?????????????£?????????????)

	vector<int> vs;   // ??°????????????

	vector<bool> used;    // ?????§???????????????



	using vector::vector;	// ?¶?????????????????????????

	SCCGraph(const vector<vi> &v): vector::vector(v) {}	// ??????????????????????????????????????¨???????????????



	// a -> b ????????????????????????push_back()??§?????????

	void add_edge(int a, int b){

		(*this)[a].push_back(b);

	}



	// scc()??§??????

	void dfs(int n, int k, vector<vi> &v, vector<vi> &rv){

		used[n] = true;

		order[n] = k;

		for(auto t: v[n]){

			if( !rv.empty() ) rv[t].push_back(n);	// ???????????°???????????????

			if( !used[t] ) dfs(t, k, v, rv);

		}

		vs.push_back(n);

	}



	// ?????£?????????????§£?????????

	int scc(){

		int N = size();

		used.assign(N, false);

		order.resize(N);

		vs.clear();

		vector<vi> rG(N), tmp;   // ????????????????????°????????¨

		for(int n=0; n < N; n++){

			if( !used[n] ) dfs(n, n, (*this), rG);

		}

		used.assign(N, false);

		int k = 0;

		for(int i = vs.size()-1; i >= 0; i--){

			if( !used[ vs[i] ] ) dfs(vs[i], k++, rG, tmp);

		}

		return k++;

	}



	// ?±????????????£????????????????????????????????????

	bool find(int x, int y){

		return order[x] == order[y];

	}



};

// ??¢??°??¨??????scc??????????????????????????????????????????????±????????????£??????????????????

vector<int> scc(const vector<vi> &v){

	SCCGraph g(v);

	g.scc();

	return g.order;

}





// 2-SAT ??????

class TwoSAT{

};





// test

// AOJ GRL_3_C (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_C&lang=jp)

int main(){

	int V, E;

	cin >> V >> E;

	//SCCGraph graph(V);

	vector<vi> edge(10005);

	rep(i,E){

		int s, t;

		cin >> s >> t;

		//graph.add_edge( s, t );

		edge[s].push_back( t );

	}



	//graph.scc();

	//rep(i,V) cout << "graph: " << i << " " << graph.order[i] << endl;



	auto g = scc(edge);



	int Q;

	cin >> Q;

	rep(i,Q){

		int u, v;

		cin >> u >> v;

		//cout << (int)graph.find(u, v) << endl;

		cout << (int)(g[u] == g[v]) << endl;

	}



	return 0;

}

// Fastest known solution for this problem: