import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

const long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new DirectedGraph(N);
    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G.add_edge(s[0], s[1]);
    }

    auto scc = new StronglyConnectedComponent(G);

    auto Q = readln.chomp.to!int;
    while (Q--) {
        s = readln.split.map!(to!int);
        auto u = s[0];
        auto v = s[1];
        writeln( scc.index[u] == scc.index[v] ? 1 : 0 );
    }
}


class DirectedGraph {
    int n;
    int[][] adj;
    int[][] inv;

    this(int n) {
        this.n = n;
        adj = new int[][](n);
        inv = new int[][](n);
    }

    void add_edge(int u, int v) {
        adj[u] ~= v;
        inv[v] ~= u;
    }
}


class StronglyConnectedComponent : DirectedGraph {
    int[] index;
    int[][] scc;

    this(DirectedGraph g) {
        int cnt = 0;
        auto ord = new int[](g.n);
        auto used = new bool[](g.n);
        index = new int[](g.n);

        void dfs1(int n) {
            if (used[n]) return;
            used[n] = true;
            foreach (m; g.adj[n]) if (!used[m]) dfs1(m);
            ord[cnt++] = n;
        }

        void dfs2(int n) {
            if (used[n]) return;
            used[n] = true;
            index[n] = cnt;
            scc.back ~= n;
            foreach (m; g.inv[n]) if (!used[m]) dfs2(m);
        }

        foreach (i; 0..g.n) if (!used[i]) dfs1(i);

        cnt = 0;
        fill(used, false);

        foreach_reverse (i; 0..g.n) {
            if (used[ord[i]]) continue;
            scc.length += 1;
            dfs2(ord[i]);
            cnt += 1;
        }

        super(cnt);
        auto edge = new bool[int][](cnt);

        foreach (i; 0..g.n)
            foreach (j; g.adj[i])
                if (index[i] != index[j])
                    edge[index[i]][index[j]] = true;

        foreach (i; 0..cnt)
            foreach (j; edge[i].keys)
                add_edge(i, j);
    }
}

