using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int N = Reader.Int(), M = Reader.Int();
        var E = Reader.IntTable(M);
        int NQ = Reader.Int();
        var Q = Reader.IntTable(NQ);
        var scc = new StronglyConnectedComponent(N, E, 0);
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        Array.ForEach(Q, q => Console.WriteLine(scc.Component(q[0]) == scc.Component(q[1]) ? 1 : 0));
        Console.Out.Flush();
    }
}


class Component
{
    public int[] Vertexes;
    public int[] Edges;
}

class StronglyConnectedComponent
{
    public int NumComponents { get; private set; }
    public int[] Vertexes(int at) { return vertexes[component[at]]; }
    public int Component(int v) { return component[v]; }
    int[] component;
    List<int>[] E, RevE;
    List<int[]> vertexes = new List<int[]>();

    public StronglyConnectedComponent(int N) { Init(N); }
    public StronglyConnectedComponent(int N, int[][] edges, int origin = 0)
    {
        Init(N);
        Array.ForEach(edges, e => AddEdge(e[0] - origin, e[1] - origin));
        Decompose();
    }

    void Init(int N)
    {
        component = new int[N];
        E = new List<int>[N];
        RevE = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            component[i] = -1;
            E[i] = new List<int>();
            RevE[i] = new List<int>();
        }
    }

    public void AddEdge(int from, int to)
    {
        E[from].Add(to);
        RevE[to].Add(from);
    }

    public void Decompose()
    {
        var vs = new List<int>();
        var seen = new bool[E.Length];
        for (int v = 0; v < E.Length; v++)
            if (!seen[v]) DFS(v, seen, vs);
        vs.Reverse();
        foreach (int v in vs)
            if (component[v] == -1)
                vertexes.Add(DFSRev(v, vertexes.Count));
    }

    public Component[] GetComponents()
    {
        var components = new Component[vertexes.Count];
        for (int i = 0; i < components.Length; i++)
        {
            components[i] = new Component();
            components[i].Vertexes = vertexes[i].ToArray();
            var es = new HashSet<int>();
            foreach (int v in components[i].Vertexes)
                foreach (int next in E[v])
                    if (component[next] != i)
                        es.Add(component[next]);
            components[i].Edges = es.ToArray();
        }
        return components;
    }

    void DFS(int v, bool[] seen, List<int> vs)
    {
        var stack = new Stack<int>();
        stack.Push(v);
        for (; stack.Count > 0; )
        {
            v = stack.Pop();
            if (v < 0) { vs.Add(~v); continue; }
            if (seen[v]) continue;
            stack.Push(~v);
            seen[v] = true;
            for (int i = E[v].Count - 1; i >= 0; i--)
                if (!seen[E[v][i]])
                    stack.Push(E[v][i]);
        }
    }
    int[] DFSRev(int v, int componentId)
    {
        var vs = new List<int>();
        var stack = new Stack<int>();
        stack.Push(v);
        for (; stack.Count > 0; )
        {
            v = stack.Pop();
            if (component[v] != -1) continue;
            component[v] = componentId;
            vs.Add(v);
            for (int i = RevE[v].Count - 1; i >= 0; i--)
                if (component[RevE[v][i]] == -1) stack.Push(RevE[v][i]);
        }
        return vs.ToArray();
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { Dispose(); A = new string[0]; }
    public static void Set(TextReader r) { Init(); reader = r; }
    public static void Set(string file) { Init(); reader = new StreamReader(file); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    public static void Dispose() { reader.Dispose(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}

Measured memory_bytes: 21732