import scala.io.StdIn._
object Main extends App {
  val n = readInt
  for (_ ← 0 until n) {
    val Array(k, p) = readLine.trim.split(' ').map(_.toInt)
    println((if (k % p == 0) p else k % p))
  }
}
