import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*

fun PrintWriter.solve(sc: FastScanner) {
    val s = sc.nextInt()
    for (_i in 0 until s) {
        val x = sc.nextInt()
        val y = sc.nextInt()
        val cand_a = mutableSetOf<Long>()
        val n1 = x + y
        val n2 = Math.abs(x - y)
        var d = 1
        while (d * d <= n1) {
            if (n1 % d == 0) {
                val a1 = d - 1L
                if (a1 >= 2 && n2 % (a1 - 1) == 0L) {
                    cand_a.add(a1)
                }
                val a2 = n1 / d - 1L
                if (a2 >= 2 && n2 % (a2 - 1) == 0L) {
                    cand_a.add(a2)
                }
            }
            d++
        }
        var ans = 0L
        for (a in cand_a) {
            val num = a * a - 1
            if (a * x > y && a * y > x && (a * x - y) % num == 0L && (a * y - x) % num == 0L) {
                ans++
            }
        }
        if (x == y) {
            ans += x - 1
        }
        println(ans)
    }
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve(FastScanner(System.`in`))
    writer.flush()
}

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    fun next(): String {
        while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())

        return st.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextLine() = br.readLine()
    fun nextDouble() = next().toDouble()
    fun ready() = br.ready()
}