// Slower C++ solution:
#include<iostream>

#include<vector>

#include<algorithm>

#include<cstdio>

#include<cstring>



using namespace std;



const int MAX_N = 10000;

const int V = 10000;



vector<int> G[MAX_N];

vector<int> rG[MAX_N];

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

bool used[MAX_N]; //?????§????????????????????????

int cmp[MAX_N];	//?±????????????£????????????????????????????????????



void add_edge(int from,int to)

{

	G[from].push_back(to);

	rG[to].push_back(from);

}



void dfs(int v)

{

	used[v] = true;

	for(int i=0;i<G[v].size();i++){

		if(!used[G[v][i]]){

			dfs(G[v][i]);

		}

	}

	vs.push_back(v);

}



void rdfs(int v,int k)

{

	used[v] = true;

	cmp[v] = k;

	for(int i=0;i<rG[v].size();i++){

		if(!used[rG[v][i]]){

			rdfs(rG[v][i],k);

		}

	}

}



int scc(int n)	//?????£??????????????°?????????

{

	memset(used,0,sizeof(used));

	vs.clear();

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

		if(!used[v]){

			dfs(v);

		}

	}

	memset(used,0,sizeof(used));

	int k=0;

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

		if(!used[vs[i]]){

			rdfs(vs[i],k++);

		}

	}

	return k;

}



int main()

{

	int n,m,from,to,k,l,u,v;

	cin >> n >> m;

	for(int i=0;i<m;i++){

		cin >> from >> to;

		add_edge(from,to);

	}

	k = scc(n);	

	cin >> l;

	for(int i=0;i<l;i++){

		cin >> u >> v;

		printf("%d\n",(cmp[u] == cmp[v]));

	}

}

// Faster C++ solution for the same problem (measured speedup: 1.475784606439112x):
#include <bits/stdc++.h>

#define ll long long

#define INF 1000000005

#define MOD 1000000007

#define EPS 1e-10

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

#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)

#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)

#define each(a,b) for(auto& (a): (b))

#define all(v) (v).begin(),(v).end()

#define len(v) (int)(v).size()

#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())

#define cmx(x,y) x=max(x,y)

#define cmn(x,y) x=min(x,y)

#define fi first

#define se second

#define pb push_back

#define show(x) cout<<#x<<" = "<<(x)<<endl

#define spair(p) cout<<#p<<": "<<p.fi<" "<<p.se<<endl

#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl

#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl

#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl

#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl



using namespace std;



typedef pair<int,int> P;

typedef pair<ll,ll> pll;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<ll> vl;

typedef vector<vl> vvl;

typedef vector<double> vd;

typedef vector<P> vp;

typedef vector<string> vs;



const int MAX_N = 100005;



class SCC{

public:

	vector<vector<int> > G,rG,graph;

	vector<int> vs,cmp;

	vector<bool> used;

	int V,cnt;

	SCC(int node_size){

		V = node_size;

		G.resize(V),rG.resize(V);

		used.resize(V),cmp.resize(V);

	}

	void add_edge(int from,int to){

		G[from].push_back(to);

		rG[to].push_back(from);

	}

	void dfs(int u){

		used[u] = true;

		for(int v : G[u]){

			if(!used[v]){

				dfs(v);

			}

		}

		vs.push_back(u);

	}

	void dfs(int u,const int k){

		used[u] = true;

		cmp[u] = k;

		for(int v : rG[u]){

			if(!used[v]){

				dfs(v,k);

			}

		}

	}

	int solve(){ //強連結成分の数を返す

		fill(used.begin(),used.end(),false);

		rep(i,V){

			if(!used[i]){

				dfs(i);

			}

		}

		fill(used.begin(),used.end(),false);

		cnt = 0;

		for(int i=V-1;i>=0;i--){

			if(!used[vs[i]]){

				dfs(vs[i],cnt++);

			}

		}

		return cnt;

	}

	void make_graph(){

		graph.resize(cnt);

		rep(i,V){

			for(int v : G[i]){

				if(cmp[i] != cmp[v]){

					graph[cmp[i]].pb(cmp[v]);

				}

			}

		}

	}

};



int main()

{

    cin.tie(0);

    ios::sync_with_stdio(false);

    int n,m;

    cin >> n >> m;

    SCC sc(n);

    rep(i,m){

        int a,b;

        cin >> a >> b;

        sc.add_edge(a,b);

    }

    sc.solve();

    int q;

    cin >> q;

    rep(i,q){

        int a,b;

        cin >> a >> b;

        cout << (sc.cmp[a] == sc.cmp[b]) << "\n";

    }

    return 0;

}