#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;


typedef vector<vector<int> > graph_t;


class SCC {
public:
  SCC(int _n, int _k) : n(_n), k(_k), g(_n), gr(_n) {
    visisted.assign(n, false);
  }

  void solve() {
    for (int i = 0; i < k; ++i) {
      int u, v;
      scanf("%d %d", &u, &v);
      g[u].push_back(v);
      gr[v].push_back(u);
    }

    vector<int> order;
    for (int i = 0; i < n; ++i) {
      if (!visisted[i])
        dfs(i, g, order);
    }
    reverse(order.begin(), order.end()); 

    visisted.assign(n, false);
    id.assign(n, -1);
    int cnt = 1;
    for (int i = 0; i < n; ++i) {
      int u = order[i];
      if (!visisted[u]) {
        vector<int> component;
        dfs(u, gr, component);
        for (vector<int>::iterator it = component.begin(); it != component.end(); ++it) {
          id[*it] = cnt;
        }
        ++cnt;
      }
    }
  }

  bool same_component(int u, int v) {
    return id[u] == id[v];
  }

private:
  void dfs(int u, graph_t& g, vector<int>& res) {
    visisted[u] = true;

    for (size_t i = 0; i < g[u].size(); ++i) {
      int to = g[u][i];
      if (!visisted[to])
        dfs(to, g, res);
    }

    res.push_back(u);
  }
private:
  int n, k;
  graph_t g, gr; 
  vector<bool> visisted;
  vector<int> id;
};


void scc_query(SCC& scc, int u, int v) {
  if (scc.same_component(u, v))
    printf("1\n");
  else
    printf("0\n");
}

int main() {
  
  int n, k;
  scanf("%d %d", &n, &k);

  SCC scc(n, k);
  scc.solve();

  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    scc_query(scc, u, v);
  }
  return 0;
}

