#define _CRT_SECURE_NO_WARNINGS
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define int ll
//#define endl "\n"
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> 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<class T, class U> ostream & operator<<(ostream & os, pair<T,U> const & p){ return os << "(" << p.first << ", " << p.second << ") "; }
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; }
template<class T> inline bool chmax(T & x, T const & y){ return x<y ? x=y,true : false; }
template<class T> inline bool chmin(T & x, T const & y){ return x>y ? x=y,true : false; }
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<mt(__VA_ARGS__)<<" ["<<__LINE__<<"]"<<endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;

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 = 10010;

vector<vector<int>> stronglyConnectedComponents(Graph const & g){
    static int n, sz, add[MAX_V], vis[MAX_V], ord[MAX_V], S[MAX_V];
    n = g.size();
    Graph rg(n);
    rep(i,n){
        for(auto & e : g[i]) rg[e.dst].eb(e.dst, e.src, e.weight);
        add[i] = vis[i] = 0;
    }
    sz = 0;
    rep(i,n){
        if(vis[i]) continue;
        int s = 0;
        S[s++] = i;
        while(s){
            int v = S[s-1];
            vis[v] = true;
            bool pushed = false;
            for(auto & e : g[v]){
                int d = e.dst;
                if(!vis[d]){
                    S[s++] = d;
                    pushed = true;
                }
            }
            if(!pushed){
                int t = S[s-1];
                if(!add[t]){
                    add[t] = true;
                    ord[sz++] = t;
                }
                s--;
            }
        }
    }
    vector<vector<int>> res;
    rep(i,n) vis[i] = false;
    rep(i,n){
        int u = ord[n-i-1];
        if(vis[u]) continue;
        res.push_back({});
        sz = 0;
        S[sz++] = u;
        while(sz){
            int v = S[--sz];
            res.back().push_back(v);
            vis[v] = true;
            for(auto & e : rg[v]){
                int d = e.dst;
                if(!vis[d]) S[sz++] = d;
            }
        }
        sz++;
    }
    return res;
}

int main(){
    int V,E;
    cin >> V >> E;
    Graph g(V);
    rep(i,E){
        int a,b;
        cin >> a >> b;
        g[a].eb(a,b,0);
    }
    vvi a = stronglyConnectedComponents(g);
    vi b(V);
    rep(i,a.size())rep(j,a[i].size()) b[a[i][j]] = i;
    int Q;
    cin >> Q;
    rep(i,Q){
        int u,v;
        cin >> u >> v;
        puts(b[u]==b[v] ? "1" : "0");
    }
}

// signed main(){
//     ios_base::sync_with_stdio(0); cin.tie(0);
//     int N;
//     cin >> N;
//     Graph g(N);
//     rep(i,N)rep(j,N){
//         int x;
//         cin >> x;
//         if(x) g[i].eb(i,j,1);
//     }
//     vvi col = stronglyConnectedComponents(g);
//     vector<int>
// }

Measured memory_bytes: 2316