#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T>
void printv(const vector<T> &v)
{
    int sz = v.size();
    for (int i = 0; i < sz; i++)
    {
        cout << v[i] << " \n"[i == sz - 1];
    }
}

int V, E;
vector<vector<int>> g(10000), rg(10000);
vector<int> vs; // 帰りがけ順の並び
vector<bool> used(10000, false);
vector<int> cmp(10000); // 属する強連結成分のトポロジカル順序

void add_edge(int from, int to)
{
    g[from].push_back(to);
    rg[to].push_back(from);
}

void dfs(int cur)
{
    used[cur] = true;
    for (const auto &v : g[cur])
    {
        if (!used[v])
            dfs(v);
    }
    vs.push_back(cur);
}

void rdfs(int cur, int k)
{
    used[cur] = true;
    cmp[cur] = k;
    for (const auto &v : rg[cur])
    {
        if (!used[v])
            rdfs(v, k);
    }
}

int scc()
{
    for (int i = 0; i < V; i++)
        used[i] = false;
    vs.clear();
    for (int v = 0; v < V; v++)
    {
        if (!used[v])
            dfs(v);
    }
    for (int i = 0; i < V; i++)
        used[i] = false;
    int k = 0;
    for (int i = (int)vs.size() - 1; i >= 0; i--)
    {
        if (!used[vs[i]])
            rdfs(vs[i], k++);
    }
    return k;
}

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

    cin >> V >> E;
    for (int i = 0; i < E; i++)
    {
        int s, t;
        cin >> s >> t;
        add_edge(s, t);
    }
    scc();
    int Q;
    cin >> Q;
    for (int i = 0; i < Q; i++)
    {
        int u, v;
        cin >> u >> v;
        if (cmp[u] == cmp[v])
            cout << 1 << endl;
        else
            cout << 0 << endl;
    }
}

