#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define irep(i, n) for (int64_t i = 0; i <= (int64_t)(n); i++)
#define rrep(i, n) for (int64_t i = (n)-1; i >= 0; i--)
#define rirep(i, n) for (int64_t i = n; i >= 0; i--)

#define chmax(a, b) (a) = max(a, b)
#define chmin(a, b) (a) = min(a, b)

vector<vector<int>> inversed_edge(const vector<vector<int>> &edge) {
  vector<vector<int>> result(edge.size());
  for (int i = 0; i < edge.size(); i++) {
    for (int from : edge[i]) {
      result[from].push_back(i);
    }
  }
  return result;
}

class TopologicalSort {
  const vector<vector<int>> &mOutEdge;
  vector<int> mIsVisited;

 public:
  TopologicalSort(const TopologicalSort &) = delete;
  TopologicalSort &operator=(const TopologicalSort &) = delete;
  TopologicalSort(TopologicalSort &&) = delete;
  TopologicalSort &operator=(TopologicalSort &&) = delete;

  explicit TopologicalSort(const vector<vector<int>> &outEdge)
      : mOutEdge(outEdge), mIsVisited(outEdge.size()) {}
  vector<int> build() {
    const int N = mIsVisited.size();
    fill(mIsVisited.begin(), mIsVisited.end(), false);
    vector<int> sorted;
    for (int i = 0; i < N; i++) {
      if (!mIsVisited[i]) {
        dfs(i, sorted);
      }
    }
    return sorted;
  }

 private:
  void dfs(int node, vector<int> &sorted) {
    mIsVisited[node] = true;
    for (int c : mOutEdge[node]) {
      if (!mIsVisited[c]) {
        dfs(c, sorted);
      }
    }
    sorted.push_back(node);
  }
};

class StrictlyConnectedComponent {
  const vector<vector<int>> &mInEdge;
  const vector<int> mSorted;
  vector<int> mIsVisited;

 public:
  StrictlyConnectedComponent(const StrictlyConnectedComponent &) = delete;
  StrictlyConnectedComponent &operator=(const StrictlyConnectedComponent &) =
      delete;
  StrictlyConnectedComponent(StrictlyConnectedComponent &&) = delete;
  StrictlyConnectedComponent &operator=(StrictlyConnectedComponent &&) = delete;

  explicit StrictlyConnectedComponent(const vector<vector<int>> &inEdge,
                                      const vector<int> &sorted)
      : mInEdge(inEdge), mSorted(sorted), mIsVisited(inEdge.size()) {}
  vector<int> build() {
    const int N = mIsVisited.size();
    vector<int> group(N);
    fill(mIsVisited.begin(), mIsVisited.end(), false);
    for (int i = N - 1; i >= 0; i--) {
      if (!mIsVisited[mSorted[i]]) {
        dfs(mSorted[i], i, group);
      }
    }
    return group;
  }

 private:
  void dfs(int node, int curr, vector<int> &group) {
    mIsVisited[node] = true;
    group[node] = curr;
    for (int c : mInEdge[node]) {
      if (!mIsVisited[c]) {
        dfs(c, curr, group);
      }
    }
  }
};

vector<int> solve(const vector<vector<int>> &outEdge) {
  auto inEdge = inversed_edge(outEdge);

  auto sorted = TopologicalSort(outEdge).build();
  /*rep(i, sorted.size()) {
    cerr << "sorted[" << i << "] = " << sorted[i] << "\n";
  }*/
  return StrictlyConnectedComponent(inEdge, sorted).build();
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int V, E;
  cin >> V >> E;

  vector<vector<int>> outEdge(V);
  rep(i, E) {
    int u, v;
    cin >> u >> v;
    outEdge[u].push_back(v);
  }

  vector<int> group = solve(outEdge);

  int Q;
  cin >> Q;
  rep(i, Q) {
    int u, v;
    cin >> u >> v;
    cout << (group[u] == group[v]) << "\n";
  }

  return 0;
}


Measured memory_bytes: 4476