#include <iostream>
#include <forward_list>
#include <vector>
#include <cstdio>
#include <stack>
#include <algorithm>

using namespace std;

int cnt0 = 0;
int cnt1 = 0;
vector<int> sc;
vector<int> pre;
vector<int> low;

class Edge{
public:
    int v;
    int w;
    Edge(int vi,int wi):v(vi),w(wi){;}
};

class Graph{
public:
    int V;
    int E;
    vector<forward_list<int>> adj;
    Graph(int v,int e):V(v),E(e){
        adj.resize(V);
        sc.resize(V);pre.resize(V);low.resize(V);
        for(int i = 0; i< V;i++) {pre[i] = -1;low[i] = -1;}
    }
    void addEdge(int v,int w){adj[v].push_front(w);}
    void SCdfsR(Edge e);
};

stack<int> s;


void Graph::SCdfsR(Edge e){
    int w = e.w;
    int min_pre;
    s.push(w);
    pre[w] = cnt0++; low[w] = pre[w];min_pre = low[w];

    for(auto a = adj[w].begin();a!=adj[w].end();++a){
        int v = *a;
        if(pre[v] == -1) SCdfsR(Edge(w,v));
        min_pre = min(min_pre,low[v]);

    }

    if(min_pre < low[w]) {low[w] = min_pre;return;}
    else{
        int now;
        do{
            sc[(now = s.top())] = cnt1;
            s.pop();
            low[now] = V;
        }while(now != w);
        cnt1++;
    }
}


int main()
{
    int V,E,v,w;
    scanf("%d %d",&V,&E);
    Graph G(V,E);
    for(int i = 0; i < E;i++){
        scanf("%d %d",&v,&w);
        G.addEdge(v,w);
    }
    for(int i = 0; i < V;i++){
        if(pre[i] == -1){
            G.SCdfsR(Edge(i,i));
        }
    }
    int R;
    scanf("%d",&R);
    for(int i = 0; i < R;i++){
        scanf("%d %d",&v,&w);
        if(sc[v] == sc[w]) printf("%d\n",1);
        else printf("%d\n",0);
    }
}

