#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>
struct GraphSCC {
public:
    const int n;
    vector<bool> isthrough;
    vector<int> vs, cmp;
    vector< vector< Edge<T> > > G, rG;

    GraphSCC(vector< vector< Edge<T> > > &_G) :
        n(_G.size()), isthrough(n, false), cmp(n, 0), G(_G), 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));
        }

    void SCC_dfsone(int cur) {
        isthrough[cur] = true;
        for(int i=0; i<G[cur].size(); i++) {
            if(!isthrough[G[cur][i].to]) {
                SCC_dfsone(G[cur][i].to);
            }
        }
        vs.pb(cur);
    }

    void SCC_dfstwo(int cur, int k) {
        cmp[cur] = k;
        isthrough[cur] = true;
        for(int i=0; i<rG[cur].size(); i++) {
            if(!isthrough[rG[cur][i].to]) {
                SCC_dfstwo(rG[cur][i].to, k);
            }
        }
    }

    pair<vector<int>, int> scc() {
        // 1?????????DFS
        for(int i=0; i<n; i++)
            if(!isthrough[i]) SCC_dfsone(i);

        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(vs[i], k++);
        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));
    }
    GraphSCC<int> tr(G);
    pair<vector<int>, int> ret = tr.scc();

    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: 6084