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

using Weight = int;
using Capacity = int;
struct Edge {
    int src, dst; Weight weight;
    Edge(int s, int d, int w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

vector<int> kosaraju(const Graph &g){
    int n = g.size(), sz = 0; Graph rg(n);
    vector<int> stk, cmp(n, -1), added(n), visited(n), ord(n);
    for(auto &es : g){
        sz += es.size();
        for(const Edge &e : es) rg[e.dst].eb(e.dst, e.src, e.weight);
    }
    stk.resize(n + sz); sz = 0;
    for(int i = 0; i < n; i++){
        if(visited[i]) continue;
        int s = 0; stk[s++] = i;
        while(s != 0){
            int v = stk[s-1]; visited[v] = true;
            bool pushed = false;
            for(auto &e : g[v]){
                int dst = e.dst;
                if(!visited[dst]){ stk[s++] = dst; pushed = true; }
            }
            if(pushed) continue;
            int t = stk[s-1];
            if(!added[t]){ added[t] = true; ord[n-++sz] = t; }
            --s;
        }
    }
    int k = 0;
    for(int &u : ord){
        if(cmp[u] != -1) continue;
        int s = 0; stk[s++] = u;
        while(s != 0){
            int v = stk[--s]; cmp[v] = k;
            for(auto &e : rg[v]){
                int d = e.dst;
                if(cmp[d] == -1) stk[s++] = d;
            }
        }
        ++k;
    }
    return cmp;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    int n,m;
    cin >> n >> m;
    Graph g(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        g[a].eb(a,b,0);
    }
    vector<int> c = kosaraju(g);
    int q; cin >> q;
    rep(i,q){
        int a,b;
        cin >> a >> b;
        cout << (c[a] == c[b]) << '\n';
    }
}

Measured memory_bytes: 4332