#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

// ?§?????????¨????????¨???????????????????¨???????????§???????
template <typename T>
struct Edge {
    int from, to;
    T cost;
    Edge(int s, T d) : to(s), cost(d) {}
    Edge(int f, int s, T d) : from(f), to(s), cost(d) {}

    bool operator<(const Edge &e) const {
        return cost < e.cost;
    }
    bool operator>(const Edge &e) const {
        return cost > e.cost;
    }
};
#define Graph(type) vector< vector< Edge<type> > >

template <typename T>
void SCC_dfsone(vector< vector< Edge<T> > > &G, int cur, vector<int> &v, vector<bool> &isthrough) {
    isthrough[cur] = true;
    for(auto x : G[cur]) {
        if(!isthrough[x.to]) {
            SCC_dfsone(G, x.to, v, isthrough);
        }
    }
    v.pb(cur);
}

template <typename T>
void SCC_dfstwo(vector< vector< Edge<T> > > &rG, int cur, vector<int> &w, int k, vector<bool> &isthrough) {
    w[cur] = k;
    isthrough[cur] = true;
    for(auto x : rG[cur]) {
        if(!isthrough[x.to]) {
            SCC_dfstwo(rG, x.to, w, k, isthrough);
        }
    }
}

template <typename T>
pair<vector<int>, int> scc(vector< vector< Edge<T> > > &G) {
    int n = G.size();

    // ????????°??????
    vector< vector< Edge<T> > > rG(n);
    for(int i=0; i<n; i++) for(int j=0; j<G[i].size(); j++)
        rG[ G[i][j].to ].push_back(Edge<T>(i, G[i][j].cost));

    vector<bool> isthrough(n, false);
    vector<int> vs, cmp(n);
    // 1?????????DFS
    for(int i=0; i<n; i++)
        if(!isthrough[i]) SCC_dfsone(G, i, vs, isthrough);

    fill(isthrough.begin(), isthrough.end(), false);
    reverse(vs.begin(), vs.end());
    int k = 0;

    // 2?????????DFS
    for(int i=0; i<vs.size(); i++)
        if(!isthrough[vs[i]]) SCC_dfstwo(rG, vs[i], cmp, k++, isthrough);
    return make_pair(cmp, k);
}

signed main() {
    int v, e; cin >> v >> e;
    Graph(int) G(v);
    int s, t;
    rep(i,0,e) {
        cin >> s >> t;
        G[s].pb(Edge<int>(t, 1));
    }
    pair<vector<int>, int> ret = scc(G);

    int q; cin >> q;
    rep(i,0,q) {
        cin >> s >> t;
        if(ret.fr[s] == ret.fr[t]) cout << 1 << endl;
        else cout << 0 << endl;
    }
    return 0;
}

Measured memory_bytes: 5724