import java.io.PrintWriter
import java.util.*

fun main(args: Array<String>) {
    Thread(null, {
        val scan = FastScanner()
        val V = scan.nextInt()
        val E = scan.nextInt()
        val edges = Array(E){Pair(scan.nextInt(), scan.nextInt())}
        val q = scan.nextInt()
        val scc = StronglyConnectedComponents(mkGraphInt(V, edges, true))
        scc.scc
        val out = PrintWriter(System.out)
        for (i in 0 until q) {
            val u = scan.nextInt()
            val v = scan.nextInt()
            out.println(if (scc.cmp[u] == scc.cmp[v]) 1 else 0)
        }
        out.flush()
    }, "", 16*1024*1024).start()
}

fun mkGraphInt(n: Int, edges: Array<Pair<Int, Int>>, isDirected: Boolean = false): Array<IntArray> {
    val ret: Array<IntArray> = Array(n){IntArray(0)}
    val cnt = IntArray(n)
    for ((f, t) in edges) {
        cnt[f]++
        if (!isDirected) cnt[t]++
    }
    for (i in 0 until n) ret[i] = IntArray(cnt[i])
    for ((f, t) in edges) {
        ret[f][--cnt[f]] = t
        if (!isDirected) ret[t][--cnt[t]] = f
    }
    return ret
}

class StronglyConnectedComponents(val graph: Array<IntArray>) {
    val n = graph.size
    val used: BooleanArray = BooleanArray(n)
    val vs: MutableList<Int> = mutableListOf()
    val cmp: IntArray = IntArray(n)
    val revgraph: Array<IntArray> = run {
        val ret = Array(n){IntArray(0)}
        val cnt = IntArray(n)
        for (i in graph.indices) for (j in graph[i].indices) cnt[graph[i][j]]++
        for (i in cnt.indices) ret[i] = IntArray(cnt[i])
        for (i in graph.indices) for (j in graph[i].indices) ret[graph[i][j]][--cnt[graph[i][j]]] = i
        ret
    }
    fun dfs(v: Int) {
        used[v] = true
        for (t in graph[v]) if (!used[t]) dfs(t)
        vs.add(v)
    }
    fun rdfs(v: Int, k: Int) {
        used[v] = true
        cmp[v] = k
        for (t in revgraph[v]) if (!used[t]) rdfs(t, k)
    }
    val scc: Int by lazy {
        for (v in 0 until n) if (!used[v]) dfs(v)
        used.fill(false)
        var k = 0
        for (i in vs.size-1 downTo 0) if (!used[vs[i]]) rdfs(vs[i], k++)
        k
    }
}

private class FastScanner {
    private val printable = 33..126
    val `in` = System.`in`
    val buffer = ByteArray(1024)
    var ptr = 0
    var buflen = 0
    fun hasNextByte(): Boolean {
        if (ptr < buflen) {
            return true
        } else {
            ptr = 0
            buflen = `in`.read(buffer)
            if (buflen <= 0) {
                return false
            }
        }
        return true
    }
    fun readByte(): Byte {
        return if (hasNextByte()) buffer[ptr++] else return -1
    }
    fun hasNext(): Boolean {
        while (hasNextByte() && buffer[ptr] !in printable) ptr++
        return hasNextByte()
    }
    fun next(): String {
        if (!hasNext()) throw NoSuchElementException()
        val sb = StringBuilder()
        var b = readByte()
        while (b in printable) {
            sb.appendCodePoint(b.toInt())
            b = readByte()
        }
        return sb.toString()
    }
    fun nextLong(): Long {
        if (!hasNext()) throw NoSuchElementException()
        var n = 0L
        var minus = false
        var b = readByte()
        if (b == '-'.toByte()) {
            minus = true
            b = readByte()
        }
        if (b !in '0'.toByte()..'9'.toByte()) {
            throw NumberFormatException()
        }
        while (true) {
            if (b in '0'.toByte()..'9'.toByte()) {
                n *= 10
                n += b - '0'.toByte()
            } else if (b == (-1).toByte() || b !in printable) {
                return if (minus) -n else n
            } else {
                throw NumberFormatException()
            }
            b = readByte()
        }
    }
    fun nextInt(): Int {
        val nl = nextLong()
        if (nl !in Integer.MIN_VALUE..Integer.MAX_VALUE) throw NumberFormatException()
        return nl.toInt()
    }
    fun nextDouble(): Double {
        return next().toDouble()
    }
}


Measured memory_bytes: 42492