#include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>

using namespace std;

#define rep(i, n) for(i = 0;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define pb push_back

struct edge{
  int to;
};
vector<edge> G[10000];

int v, e;
#define maxV 10000
vector<edge> sccrG[maxV];
bool sccused[maxV] = {false};
int sccr[maxV], sccrhd = 0;
int scctopo[maxV];

void sccdfs(int now){
  sccused[now] = true;
  for(int i = 0;i < G[now].size();++i)if(!sccused[G[now][i].to])sccdfs(G[now][i].to);
  sccr[sccrhd++] = now;
  return;
}
void sccrdfs(int now, int num){
  sccused[now] = true;
  scctopo[now] = num;
  for(int i = 0;i < sccrG[now].size();++i)if(!sccused[sccrG[now][i].to])sccrdfs(sccrG[now][i].to, num);
  return;
}
void scc(void){
  for(int i = 0;i < v;++i)for(int j = 0;j < G[i].size();++j)sccrG[G[i][j].to].push_back((edge){i});
  for(int i = 0;i < v;++i)if(!sccused[i])sccdfs(i);

  int num = 0;
  for(int i = 0;i < v;++i)sccused[i] = false;
  for(;sccrhd--;)if(!sccused[sccr[sccrhd]])sccrdfs(sccr[sccrhd], num++);
  return;
}

signed main(void){
  int i, j;
  scanf("%d%d", &v, &e);
  rep(i, e){
    int2(a, b);
    G[a].pb((edge){b});
  }
  scc();

  int(q);
  for(;q--;){
    int2(u, v);
    puts((scctopo[u] == scctopo[v]) ? "1" : "0");
  }

  return 0;
}