#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
using ll = long long;
// #define int ll
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int,int>;
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;
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;
}

using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

vector<int> tarjanScc(const Graph &g){
    int n = g.size();
    int idx = 0;
    int k = 0;
    vector<int> index(n, -1), lowlink(n), onStack(n);
    vector<int> cmp(n);
    vector<int> stk;
    function<void(int)> strongconnect;
    strongconnect = [&](int v){
        index[v] = idx;
        lowlink[v] = idx;
        ++idx;
        stk.push_back(v);
        onStack[v] = true;
        for(auto &e : g[v]){
            int w = e.dst;
            if(index[w] == -1){
                strongconnect(w);
                lowlink[v] = min(lowlink[v], lowlink[w]);
            } else if(onStack[w]){
                lowlink[v] = min(lowlink[v], index[w]);
            }
        }
        if(lowlink[v] == index[v]){
            int w;
            dump(v);
            dump(stk);
            do {
                w = stk.back(); stk.pop_back();
                dump(w);
                onStack[w] = false;
                cmp[w] = k;
            } while(w != v);
            ++k;
        }
    };
    for(int v = 0; v < n; v++){
        if(index[v] == -1){
            strongconnect(v);
        }
    }
    dump(lowlink);
    dump(index);
    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 = tarjanScc(g);
    int q; cin >> q;
    rep(i,q){
        int a,b;
        cin >> a >> b;
        cout << (c[a] == c[b]) << '\n';
    }
}

Measured memory_bytes: 4380