#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;

vector<int> stronglyConnectedComponents(Graph const & g, string const & name){
    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;
    vector<int> lavel(n);
    vector<int> ord;
    vector<int> used(n);
    rep(i,n){
        if(used[i]) continue;
        stack<int> S;
        vector<int> inS(n);
        S.push(i);
        while(S.size()){
            int v = S.top();
            used[v] = true;
            bool flg = true;
            for(auto & e : g[v]){
                int dst = e.dst;
                if(!used[dst] && !inS[e.dst]){
                    S.push(dst);
                    flg = false;
                }
            }
            if(flg){
                if(!lavel[S.top()]){
                    lavel[S.top()] = cnt++;
                    ord.push_back(S.top());
                }
                S.pop();
            }
        }
    }

    reverse(all(ord));
    used.assign(n,0);
    vector<int> com(n);
    int k = 0;
    rep(i,n){
        int u = ord[i];
        if(used[u]) continue;
        stack<int> S;
        S.push(u);
        while(S.size()){
            int v = S.top(); S.pop();
            used[v] = true;
            com[v] = k;
            for(auto & e : rg[v]){
                int d = e.dst;
                if(!used[d]) S.push(d);
            }
        }
        k++;
    }

#ifdef DEBUG
#endif
    return com;
}

int main(){
    int n,m;
    int cnt = 0;
    while(cin>>n>>m){
        Graph g(n);
        rep(i,m){
            int a,b; cin >> a >> b;
            g[a].eb(a,b,1);
        }

        static char buf[100];
        sprintf(buf, "graph%02d", cnt++);
        auto com = stronglyConnectedComponents(g, buf);

        int q; cin >> q;
        rep(i,q){
            int a,b; cin >> a >> b;
            cout << (com[a]==com[b]) << endl;
        }
    }
}

Measured memory_bytes: 2584