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 t = sc.nextInt() val size = 5000005 val isprime = BitSet(size) for (i in 2 until size) { isprime[i] = true } for (i in 2 until size) { if (isprime[i]) { var j = 2 while (i * j < size) { isprime[i * j] = false j++ } } } for (_i in 0 until t) { val a = sc.nextLong() val p = sc.nextInt() if (!isprime[p]) { println(-1) } else if (a % p == 0L) { println(0) } else { println(1) } } } 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() }