#include <assert.h>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
 
#define rep(i, a, b) for (long long(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cerr << "debug " << i << std::endl
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (int i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
 
template<typename T1,typename T2>
inline bool chmax(T1& a,T2 b){return a<b && (a=b,true);}
 
template<typename T1,typename T2>
inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);}
 
long long pow_mod(long long a, long long b, long long mod=-1) {
  if ((a == 0)||(mod!=-1&&a%mod==0)) {
    return 0;
  }
 
  long long x = 1;
 
  while (b > 0) {
    if (b & 1) {
      x = (mod!=-1)?(x * a) % mod:x*a;
    }
    a = (mod!=-1)?(a * a) % mod:a*a;
    b >>= 1;
  }
  return x;
}

using ll = long long;
using P = std::pair<ll, ll>;
 
// const long long MOD = 998244353;
const long long MOD = 1e9 + 7;

class StrongConnectedComponents{
  private:

  std::vector<std::vector<long long>> graph,graph_inv;
  long long v;
  std::vector<long long> compo,order,used;

  //WIP:dfsの戻り掛け順を非再帰で書きたい　バグってる
  /*void calc_order(){
    std::vector<long long> used(v,-1),par(v,-1);
    for(long long i=0;i<v;i++){
      if(used[i]==-1){
        std::stack<long long> que;
        que.push(i);
        while(!que.empty()){
          long long now=que.top();que.pop();
          if(used[now]==-1){
            used[now]=0;
            for(long long next:graph[now]){
              if(used[next]==-1){
                used[now]++;
                que.push(next);
                par[next]=now;
              }
            }
          }
          
          if(used[now]==0){
            order.push_back(now);
            if(par[now]!=-1){
              used[par[now]]--;
              que.push(par[now]);
            }
          }
        }
      }
    }
  }*/

  void dfs(int idx) {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : graph[idx]) dfs(to);
    order.push_back(idx);
  }

  void calc_compo(){
    std::reverse(all(order));
    long long num=0;
    for(long long now:order){
      if(compo[now]==-1){
        std::stack<long long> que;
        que.push(now);
        while(!que.empty()){
          long long e=que.top();que.pop();
          compo[e]=num;
          for(long long next:graph_inv[e]){
            if(compo[next]==-1){
              que.push(next);
            }
          }
        }
        num++;
      }
    }
  }

  public:

  StrongConnectedComponents(std::vector<std::vector<long long>>& g):graph(g.size()),graph_inv(g.size()),v(g.size()),compo(g.size(),-1),used(g.size(),false){
    for(long long i=0;i<v;i++){
      for(long long next:g[i]){
        graph[i].push_back(next);
        graph_inv[next].push_back(i);
      }
    }
  }

  std::vector<long long> calc(){
    for(long long i=0;i<v;i++)dfs(i);
    calc_compo();

    return compo;
  }

};

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  ll v,e;
  std::cin>>v>>e;
  std::vector<std::vector<ll>> graph(v);

  rep(i,0,e){
    ll s,t;
    std::cin>>s>>t;
    graph[s].push_back(t);
  }

  StrongConnectedComponents dag(graph);

  std::vector<ll> ans=dag.calc();

  ll q;
  std::cin>>q;

  rep(i,0,q){
    ll u,v;
    std::cin>>u>>v;

    if(ans[u]==ans[v])std::cout<<1<<"\n";
    else std::cout<<0<<"\n";
  }

  return 0;
}



Measured memory_bytes: 5276