#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

struct Edge{
    int from, to;
    int cost=1;

    Edge(int from, int to, int cost=1): from(from), to(to), cost(cost) {}
    Edge() {}

    friend istream &operator>>(istream  &input, Edge &e ){ 
        input >> e.from >> e.to;
        return input;            
    }

    Edge rev(void){
        return Edge(to, from, cost);
    }
};

vector<vector<Edge>> edge, re;
vector<bool> vis, done;
VI one_group, order;
VVI group;
int n,m,k;

void dfs1(int v){
    vis[v] = 1;
    for(auto& e : edge[v]){
        if(vis[e.to]) continue;
        dfs1(e.to);
    }
    order.push_back(v);
    return;
}

void dfs2(int v){
    vis[v] = 0;
    for(auto& e : re[v]){
        if(vis[e.to]==0) continue;
        dfs2(e.to);
    }
    one_group.push_back(v);
    return;
}

void decomp(int v){
    dfs1(v);
    while(!order.empty()){
        if(vis[order.back()]==0){
            order.pop_back();
            continue;
        }
        dfs2(order.back());
        for(int u : one_group) done[u] = 1;
        group.emplace_back(one_group);
        one_group = {};
    }
    return;
}

struct UnionFind{
    vector<int> par; // par[i]:iの親の番号
    vector<int> s; // s[i]:iの属する木のノード数
    int n_group; // 木の数
    
    UnionFind(int N) : par(N) {
        for(int i=0; i<N; i++) par[i] = i;
        s.assign(N, 1);
        n_group = N;
    }
    
    int root(int x){ // xが属する木の根
        if(par[x] == x) return x;
        return par[x] = root(par[x]);
    }
    
    void unite(int x, int y){ // xとyの木を併合
        int rx = root(x);
        int ry = root(y);
        if(rx == ry) return;
        par[rx] = ry;
        s[ry] += s[rx];
        s[rx] = 0;
        n_group--;
    }
    
    bool same(int x, int y){ // xとyが同じ木に属するかどうか
        return root(x) == root(y);
    }

    int size(int x){ // xが属する木のノード数
        return s[root(x)];
    }
};

void Main(){
    cin >> n >> m;
    edge.resize(n); re.resize(n);
    REP(i,m){
        Edge e; cin >> e;
        edge[e.from].emplace_back(e);
        re[e.to].emplace_back(e.rev());
    }

    vis.assign(n,0);done.assign(n,0);
    REP(i,n) 
        if(done[i]==0)
            decomp(i);

    UnionFind uf(n);
    for(auto& v : group){
        int k = v.size();
        REP(i,k-1) uf.unite(v[i],v[i+1]);
    }

    int Q; cin >> Q;
    REP(_,Q){
        int u,v; cin >> u >> v;
        cout << (uf.same(u,v) ? 1 : 0) << en;
    }
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}


Measured memory_bytes: 4804