#define _CRT_SECURE_NO_WARNINGS
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template<class T> istream & operator>>(istream & is, vector<T> &);
template<class T> ostream & operator<<(ostream & os, vector<T> const &);
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple<T...> const &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple<T...> const & t){ os << (n==0?"":" ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream & operator<<(ostream & os, tuple<T...> const & t){ _ot<0>(os, t); return os; }
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _it(istream &, tuple<T...> &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _it(istream & is, tuple<T...> & t){ is >> get<n>(t); _it<n+1>(is, t); }
template<class...T> istream & operator>>(istream & is, tuple<T...> & t){ _it<0>(is, t); return is; }
template<class T, class U> istream & operator<<(istream & is, pair<T,U> & p){ return is >> p.first >> p.second; }
template<class T, class U> ostream & operator<<(ostream & os, pair<T,U> const & p){ return os << "(" << p.first << ", " << p.second << ") "; }
template<class T> istream & operator>>(istream & is, vector<T> & v){ rep(i,v.size()) is >> v[i]; return is; }
template<class T> ostream & operator<<(ostream & os, vector<T> const & v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; }
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<mt(__VA_ARGS__)<<" ["<<__LINE__<<"]"<<endl)
#else
#define dump(...)
#endif

typedef int Weight;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src_, int dst_, Weight weight_) :
        src(src_), dst(dst_), weight(weight_) { }
};
bool operator < (const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
        e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

int const MAX_V = 100000;
vector<int> stronglyConnectedComponents(Graph const & g){
    int n = g.size();
    Graph rg(n);
    rep(i,n) for(auto & e : g[i]) rg[e.dst].eb(e.dst, e.src, e.weight);

    int cnt = 0;
    static int lavel[MAX_V], used[MAX_V], ord[MAX_V];
    static int inS[MAX_V], S[MAX_V];
    rep(i,n) lavel[i] = used[i] = inS[i] = false;
    int sz = 0;
    rep(i,n){
        if(used[i]) continue;
        int s = 0;
        S[s++] = i;
        while(s){
            int v = S[s-1];
            used[v] = true;
            bool flg = true;
            for(auto & e : g[v]){
                int dst = e.dst;
                if(!used[dst] && !inS[e.dst]){
                    S[s++] = dst;
                    flg = false;
                }
            }
            if(flg){
                int t = S[s-1];
                if(!lavel[t]){
                    lavel[t] = cnt++;
                    ord[sz++] = t;
                }
                s--;
            }
        }
    }
    rep(i,n) used[i] = false;
    vector<int> com(n);
    int k = 0;
    rep(i,n){
        int u = ord[n-i-1];
        if(used[u]) continue;
        int s = 0;
        S[s++] = u;
        while(s){
            int v = S[--s];
            used[v] = true;
            com[v] = k;
            for(auto & e : rg[v]){
                int d = e.dst;
                if(!used[d]) S[s++] = d;
            }
        }
        k++;
    }
    return com;
}

int main(){
    int n,m;
    while(cin>>n>>m){
        Graph g(n);
        rep(i,m){
            int a,b; cin >> a >> b;
            g[a].eb(a,b,1);
        }
        auto com = stronglyConnectedComponents(g);
        int q; cin >> q;
        rep(i,q){
            int a,b; cin >> a >> b;
            cout << (com[a]==com[b]) << endl;
        }
    }
}

Measured memory_bytes: 2316