#include<stdio.h>
int ta[10010],to[30010],nt[30010],rta[10010],rto[30010],rnt[30010];
int id[10010],rid[10010],cmp[100010],r=0,f[10010]={0};
void dfs(int s){
  int i;
  f[s]=1;
  for(i=ta[s];i+1;i=nt[i]){
    if(f[to[i]]==0)dfs(to[i]);
  }
  id[rid[s]=r++]=s;//printf("\n");
}
void rdfs(int s,int k){
  int i;
  f[s]=2;
  cmp[s]=k;
  for(i=rta[s];i+1;i=rnt[i]){
    if(f[rto[i]]==1)rdfs(rto[i],k);
  }
}
void scc(int v,int e,int *a,int *b){
  int i,k;
  for(i=0;i<v;i++)ta[i]=rta[i]=-1;
  for(i=0;i<e;i++){
     nt[i]= ta[rto[i]=a[i]];
    rnt[i]=rta[ to[i]=b[i]];
    ta[a[i]]=rta[b[i]]=i;
  }
  /*for(i=0;i<v;i++){printf("%d:",i);
    for(k=ta[i];k+1;k=nt[k])printf("%d ",to[k]);
    printf("\n");
  }printf("\n");
  for(i=0;i<v;i++){printf("%d:",i);
    for(k=rta[i];k+1;k=rnt[k])printf("%d ",rto[k]);
    printf("\n");
  }printf("\n");//*/
  for(i=k=0;i<v;i++){
    if(f[i]==0)dfs(i);
  }
  //for(i=0;i<v;i++)printf("%d %d %d\n",i,id[i],rid[i]);
  for(i=r;i;i--){
    if(f[id[i-1]]==1)rdfs(id[i-1],id[i-1]);
  }//printf("\n");
  //for(i=0;i<v;i++)printf("%d %d %d\n",i,id[i],cmp[i]);
}
int main(){
  int v,e,a[100010],b[100010],s,t,i;
  scanf("%d %d",&v,&e);
  for(i=0;i<e;i++)scanf("%d %d",&a[i],&b[i]);
  scc(v,e,a,b);
  scanf("%d",&v);
  while(v--){
    scanf("%d %d",&s,&t);
    printf("%d\n",cmp[s]-cmp[t]?0:1);
  }
  return 0;
}