This is an interactive problem!Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with:  1 if $$$a \oplus c>b \oplus d$$$.  0 if $$$a \oplus c=b \oplus d$$$.  -1 if $$$a \oplus c<b \oplus d$$$. Operation $$$a \oplus b$$$ is the bitwise-xor operation of two numbers $$$a$$$ and $$$b$$$.Laggy should guess $$$(a,b)$$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab.It's guaranteed that $$$0 \le a,b<2^{30}$$$.

To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer.

NoteIn the sample:The hidden numbers are $$$a=3$$$ and $$$b=1$$$.In the first query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 1 = 0$$$, so the answer is 1.In the second query: $$$3 \oplus 1 = 2$$$ and $$$1 \oplus 2 = 3$$$, so the answer is -1.In the third query: $$$3 \oplus 2 = 1$$$ and $$$1 \oplus 0 = 1$$$, so the answer is 0.Then, we printed the answer.

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    var a, b = 0

    def query(k: Int, c: Int, d: Int): Int = {
      out.println(s"? ${a | (c << k)} ${b | (d << k)}")
      out.flush()
      ni()
    }

    def answer(a: Int, b: Int): Unit = {
      out.println(s"! $a $b")
      out.flush()
    }

    val ans00 = Array.fill[Int](31)(100) // 0, -1がつかえないので変な値だけど100で初期化

    REP_r(30) { k =>
      if (ans00(k+1) == 100) {
        ans00(k+1) = query(k, 0, 0)
      }

      val ans10 = query(k, 1, 0)
      val ans01 = query(k, 0, 1)

      (ans10, ans01) match {
        // (1, 1)
        case (-1, 1) =>
          a |= 1 << k
          b |= 1 << k
          ans00(k) = ans00(k+1)

        // (0, 0)
        case (1, -1) =>
          ans00(k) = ans00(k+1)

        // ans10 == ans01 == ans00(k)
        case (1, 1) | (-1, -1) | (0, 0) =>
          ans00(k) = ans10
          ans00(k + 1) match {
            // (1, 0)
            case 1 =>
              a |= 1 <<k

            // (0, 1)
            case -1 =>
              b |= 1 <<k
          }
      }
    }

    answer(a, b)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    var a, b = 0

    def query(k: Int, c: Int, d: Int): Int = {
      out.println(s"? $c $d")
      out.flush()
      ni()
    }

    def answer(a: Int, b: Int): Unit = {
      out.println(s"! $a $b")
      out.flush()
    }

    REP_r(30) { k =>
      /*
      (c, d) = (1, 1)

      a b xor
      1 1 0
      1 0 -1
      0 1 1
      0 0 0
       */
      query(k, a | (1 << k), b | (1 << k)) match {
        case -1 =>
          a |= 1 << k

        case 1 =>
          b |= 1 << k

        case 0 =>
          /*
          (c, d) = (1, 0)

          a b xor
          1 1 -1
          1 0 0
          0 1 0
          0 0 1
           */
          query(k, a | (1 << k), b) match {
            case -1 =>
              a |= 1 << k
              b |= 1 << k

            case 1 =>

            case x =>
              throw new Exception(x.toString)
          }

        case x =>
          throw new Exception(x.toString)
      }
    }

    answer(a, b)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    var a, b = 0

    def query(k: Int, c: Int, d: Int): Int = {
      out.println(s"? $c $d")
      out.flush()
      ni()
    }

    def answer(a: Int, b: Int): Unit = {
      out.println(s"! $a $b")
      out.flush()
    }

    REP_r(30) { k =>
      query(k, a, b) match {
        case 1 =>

          /*
            a > b
            (c, d) = (1, 0)

            a b xor
            0 0 1
            1 0 0
            1 1 -1
           */
          query(k, a | (1 << k), b) match {
            case 1 =>
            case 0 =>
              a |= 1 << k
            case -1 =>
              a |= 1 << k
              b |= 1 << k
          }

        case -1 =>

          /*
            a < b
            (c, d) = (0, 1)

            a b xor
            1 1 1
            0 1 0
            0 0 -1
           */
          query(k, a, b| (1 << k)) match {
            case 1 =>
              a |= 1 << k
              b |= 1 << k
            case 0 =>
              b |= 1 << k
            case -1 =>
          }

        case 0 =>
          /*
          a == b
          (c, d) = (1, 0)

          a b xor
          1 1 -1
          0 0 1
           */
          query(k, a | (1 << k), b) match {
            case 1 =>

            case -1 =>
              a |= 1 << k
              b |= 1 << k

            case x =>
              throw new Exception(x.toString)
          }

        case x =>
          throw new Exception(x.toString)
      }
    }

    answer(a, b)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    var a, b = 0

    def query(k: Int, cbit: Int, dbit: Int): Int = {
      val c = cbit << k
      val d = dbit << k
      out.println(s"? $c $d")
      out.flush()
      ni()
    }

    def answer(a: Int, b: Int): Unit = {
      out.println(s"! $a $b")
      out.flush()
    }

    REP(30) { k =>
      query(k, 1, 1) match {
        case -1 =>
          a |= 1 << k

        case 1 =>
          b |= 1 << k

        case 0 =>
          query(k, 1, 0) match {
            case -1 =>
              a |= 1 << k
              b |= 1 << k

            case 1 =>

            case x =>
              throw new Exception(x.toString)
          }
      }
    }

    answer(a, b)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    var a, b = 0

    def query(k: Int, c: Int, d: Int): Int = {
      out.println(s"? $c $d")
      out.flush()
      ni()
    }

    def answer(a: Int, b: Int): Unit = {
      out.println(s"! $a $b")
      out.flush()
    }

    REP_r(3) { k =>
      /*
      (c, d) = (1, 1)

      a b xor
      1 1 0
      1 0 -1
      0 1 1
      0 0 0
       */
      query(k, a | (1 << k), b | (1 << k)) match {
        case -1 =>
          a |= 1 << k

        case 1 =>
          b |= 1 << k

        case 0 =>
          /*
          (c, d) = (1, 0)

          a b xor
          1 1 -1
          1 0 0
          0 1 0
          0 0 1
           */
          query(k, a | (1 << k), b) match {
            case -1 =>
              a |= 1 << k
              b |= 1 << k

            case 1 =>

            case x =>
              throw new Exception(x.toString)
          }

        case x =>
          throw new Exception(x.toString)
      }
    }

    answer(a, b)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    var a, b = 0

    def query(k: Int, c: Int, d: Int): Int = {
      out.println(s"? $c $d")
      out.flush()
      ni()
    }

    def answer(a: Int, b: Int): Unit = {
      out.println(s"! $a $b")
      out.flush()
    }

    REP_r(30) { k =>
      query(k, a, b) match {
        case 1 =>

          /*
            a > b
            (c, d) = (1, 0)

            a b xor
            0 0 1
            1 0 0
            1 1 -1
           */
          query(k, a | (1 << k), b) match {
            case 1 =>
            case 0 =>
              a |= 1 << k
            case -1 =>
              a |= 1 << k
              b |= 1 << k
          }

        case -1 =>

          /*
            a < b
            (c, d) = (0, 1)

            a b xor
            1 1 1
            0 1 0
            0 0 -1
           */
          query(k, a | (1 << k), b) match {
            case 1 =>
              a |= 1 << k
              b |= 1 << k
            case 0 =>
              b |= 1 << k
            case -1 =>
          }

        case 0 =>
          /*
          a == b
          (c, d) = (1, 0)

          a b xor
          1 1 -1
          0 0 1
           */
          query(k, a | (1 << k), b) match {
            case 1 =>

            case -1 =>
              a |= 1 << k
              b |= 1 << k

            case x =>
              throw new Exception(x.toString)
          }

        case x =>
          throw new Exception(x.toString)
      }
    }

    answer(a, b)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    var a, b = 0

    def query(k: Int, c: Int, d: Int): Int = {
      println(s"? $c $d")
      ni()
    }

    def answer(a: Int, b: Int): Unit = {
      println(s"! $a $b")
    }

    REP_r(30) { k =>
      /*
      (c, d) = (1, 1)

      a b xor
      1 1 0
      1 0 -1
      0 1 1
      0 0 0
       */
      query(k, a | (1 << k), b | (1 << k)) match {
        case -1 =>
          a |= 1 << k

        case 1 =>
          b |= 1 << k

        case 0 =>
          /*
          (c, d) = (1, 0)

          a b xor
          1 1 -1
          1 0 0
          0 1 0
          0 0 1
           */
          query(k, a | (1 << k), b) match {
            case -1 =>
              a |= 1 << k
              b |= 1 << k

            case 1 =>

            case x =>
              throw new Exception(x.toString)
          }

        case x =>
          throw new Exception(x.toString)
      }
    }

    answer(a, b)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}


Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.

The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.

If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that:   the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company,  the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company,  the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,  Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.

6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val data = in.next().split(" ").map(_.toInt).grouped(2).flatMap(_.sorted.reverse)
  val t = data.grouped(2).map(_.toList).toList.zip(List("A", "B", "C")).map{
    case(List(a, b), c) => (a, b, c)
  }
  var Seq((y1, x1, ch1), (y2, x2, ch2), (y3, x3, ch3)) = t.sorted.reverse.toSeq

  val a = y1
  val first = (1 to x1).map(_ => ch1 * a).toList

    //разместим a
  val second =
    if (x1 * y1 + x2 * y2 + x3 * y3 != a * a) None
    else if (y2 == a && y3 != a) None
    else if (y2 == a)
      Some(
        (1 to x2).map(_ => ch2 * a).toList :::
        (1 to x3).map(_ => ch3 * a).toList)
    else {
      val b = a - x1
      if (x2 == y3 && x2 == b && x3 + y2 == a)
        Some((1 to b).map(_ => ch2 * y2 + ch3 * x3).toList)
      else if (x3 == y2 && y2 == b && x2 + y3 == a)
        Some((1 to b).map(_ => ch2 * x2 + ch3 * y3).toList)
      else if (y2 + y3 == a && x2 == x3 && x2== b)
        Some((1 to b).map(_ => ch2 * y2 + ch3 * y3).toList)
      else if (x2 + x3 == a && y2 == y3 && y2 == b)
        Some((1 to b).map(_ => ch2 * x2 + ch3 * x3).toList)
      else
        None
    }
  if (second.isEmpty)
    println(-1)
  else {
    println(a)
    println(second.map(t => first ::: t).get.mkString("\n"))
  }

}

import java.io._
import java.util._

import scala.StringBuilder
import scala.collection.mutable

object D {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close
  }

  def next: String = {
    while (st == null || !st.hasMoreTokens) {
      st = new StringTokenizer(br.readLine)
    }
    return st.nextToken
  }

  def nextInt: Int = return Integer.parseInt(next)

  def nextLong: Long = return java.lang.Long.parseLong(next)

  def nextDouble: Double = return java.lang.Double.parseDouble(next)

  class MultiHashSet[T <% Comparable[T]] {
    val map = new mutable.HashMap[T, Int]()

    def count(x: T): Int = {
      return map.getOrElse(x, 0)
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  class MultiTreeSet[T <% Comparable[T]] {
    val map = new TreeMap[T, Int]()

    def count(x: T): Int = {
      val res = map.get(x)
      if (res == null)
        return 0
      return res
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def first(): T = return map.firstKey()

    def last(): T = return map.lastKey()

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  /**
   * Segment tree for any commutative function
   * @param values Array of Int
   * @param commutative function like min, max, sum
   * @param zero zero value - e.g. 0 for sum, Inf for min, max
   */
  class SegmentTree(values: Array[Int])(commutative: (Int, Int) => Int)(zero: Int) {
    private val SIZE = 1e5.toInt
    private val n = values.length
    val t = new Array[Int](2 * n)
    Array.copy(values, 0, t, n, n)

    // build segment tree
    def build = {
      for (i <- n - 1 until 0 by -1) {
        t(i) = commutative(t(2 * i), t(2 * i + 1))
      }
    }

    // change value at position p to x
    // TODO beatify
    def modify(p: Int, x: Int) = {
      var pos = p + n
      t(pos) = x
      while (pos > 1) {
        t(pos / 2) = commutative(t(pos), t(pos ^ 1))
        pos /= 2
      }
    }

    // TODO implement me!
    def modify(p: Int, left: Int, right: Int) = ???

    def query(p: Int) = ???

    // sum [l, r)
    // min l = 0
    // max r = n
    // TODO beatify
    def query(left: Int, right: Int): Int = {
      var res = zero
      var r = right + n
      var l = left + n
      while (l < r) {
        if (l % 2 == 1) {
          res = commutative(res, t(l))
          l += 1
        }
        if (r % 2 == 1) {
          r -= 1
          res = commutative(res, t(r))
        }
        l /= 2
        r /= 2
      }
      res
    }
  }

  case class Rectangle(width: Int, height: Int) {

    def normalize(): Rectangle = {
      new Rectangle(Math.min(width, height), Math.max(width, height))
    }

    def maximize(): Rectangle = {
      new Rectangle(Math.max(width, height), Math.min(width, height))
    }

    def rotate(): Rectangle = {
      new Rectangle(height, width)
    }

  }

  def getSecond(i: Int): Int = {
    i match {
      case 0 => 1
      case 1 => 2
      case 2 => 0
    }
  }

  def getThird(i: Int): Int = {
    i match {
      case 0 => 2
      case 1 => 0
      case 2 => 1
    }
  }

  def rectLetter(i: Int): String = {
    i match {
      case 0 => "A"
      case 1 => "B"
      case 2 => "C"
    }
  }

  def appendZeroes(s: String): String = {
    val sb = new StringBuilder()
    while (sb.length < 3 - s.length) {
      sb.append("0")
    }
    sb.append(s).toString()
  }

  def rotate(rectangle: Rectangle, s: String, i: Int) = if (s.charAt(i) == '1') rectangle else rectangle.rotate()

  def checkRects(i: Int, rects: Array[Rectangle]): Boolean = {
    for (i <- 0 until 3) {
      for (k <- 0 to 8) {
        val s = appendZeroes(Integer.toBinaryString(k))
        val fst = rotate(rects(i), s, 0)
        val snd = rotate(rects(getSecond(i)), s, 1)
        val thrd = rotate(rects(getThird(i)), s, 2)
        if (fst.height == snd.width + thrd.width && fst.height == fst.width + snd.height && snd.height == thrd.height) {
          // output the result
          out.println(fst.height)
          for (j <- 0 until fst.height) {
            for (k <- 0 until fst.height) {
              if (j < snd.height) {
                if (k < snd.width) {
                  out.print(rectLetter(getSecond(i)))
                } else {
                  out.print(rectLetter(getThird(i)))
                }
              } else {
                out.print(rectLetter(i))
              }
            }
            out.println()
          }
          return true
        }
      }
    }
    false
  }

  def solve: Int = {
    val x1 = nextInt
    val y1 = nextInt
    val a = new Rectangle(x1, y1).normalize
    val a1 = new Rectangle(x1, y1).maximize
    val x2 = nextInt
    val y2 = nextInt
    val b = new Rectangle(x2, y2).normalize
    val b1 = new Rectangle(x2, y2).maximize
    val x3 = nextInt
    val y3 = nextInt
    val c = new Rectangle(x3, y3).normalize
    val c1 = new Rectangle(x3, y3).maximize
    if (a.height == b.height && b.height == c.height && a.width + b.width + c.width == a.height) {
      // output the result
      out.println(a.height)
      for (i <- 0 until a.height) {
        for (j <- 0 until a.height) {
          if (j < a.width) {
            out.print("A")
          } else if (j < a.width + b.width) {
            out.print("B")
          } else {
            out.print("C")
          }
        }
        out.println()
      }
      return 0
    }
    val rects = new Array[Rectangle](3)
    rects(0) = a
    rects(1) = b
    rects(2) = c
    val reversedRects = new Array[Rectangle](3)
    reversedRects(0) = a1
    reversedRects(1) = b1
    reversedRects(2) = c1
    for (i <- 0 until 3) {
      if (checkRects(i, rects)) {
        return 0
      }
    }
    for (i <- 0 until 3) {
      if (checkRects(i, reversedRects)) {
        return 0
      }
    }
    out.println(-1)
    return 0
  }
}

object _581D extends App {
  val scanner = new java.util.Scanner(System.in)
  val logos = Seq("A", "B", "C") map {c => c -> Seq(scanner.nextInt, scanner.nextInt)}
  var rows: Seq[String] = Nil
  for {
    Seq((c1, p), (c2, q), (c3, r)) <- logos.permutations if rows.isEmpty
    Seq(x1, y1) <- p.permutations
    Seq(x2, y2) <- q.permutations
    Seq(x3, y3) <- r.permutations
  } {
    if ((y1 == y2) && (y2 == y3) && ((x1 + x2 + x3) == y3)) {
      rows = Seq.fill(x1)(c1 * y1) ++ Seq.fill(x2)(c2 * y2) ++ Seq.fill(x3)(c3 * y3)
    } else if ((x2 == x3) && (y1 == y2 + y3) && (x1 + x2) == y1) {
      rows = Seq.fill(x1)(c1 * y1) ++ Seq.fill(x2)((c2 * y2) + (c3 * y3))
    }
  }
  println(if (rows.isEmpty) -1 else rows.length)
  rows foreach println
}

import annotation.tailrec, collection.mutable, util._, control.Breaks._, math.Ordering.Implicits._
import CodeForcesApp._
object _581D extends CodeForcesApp[Option[List[String]]]({scanner => import scanner._
  def rotate(p: (Int, Int)) = List(p, p.swap)

  val (ax, ay, bx, by, cx, cy) = (nextInt, nextInt, nextInt, nextInt, nextInt, nextInt)

  val stacks = for {
    (x1, y1) <- rotate(ax, ay)
    (x2, y2) <- rotate(bx, by) if y2 == y1
    (x3, y3) <- rotate(cx, cy) if y3 == y2
    if (x1 + x2 + x3) == y3
  } yield ((x1, y1), (x2, y2), (x3, y3))

  def block(d: (Int, Int), char: String) = {
    val (x, y) = d
    List.fill(x)(char * y)
  }

  val logos = List("A" -> (ax, ay), "B" -> (bx, by), "C" -> (cx, cy))
  lazy val sided = for {
    Seq((c1, p), (c2, q), (c3, r)) <- logos.permutations
    (x1, y1) <- rotate(p)
    (x2, y2) <- rotate(q)
    (x3, y3) <- rotate(r)
    if (x2 == x3) && (y1 == y2 + y3) && (x1 + x2) == y1
  } yield (c1 -> (x1, y1), c2 -> (x2, y2), c3 -> (x3, y3))

  stacks match {
    case (a, b, c) :: _ => Some(block(a, "A") ::: block(b, "B") ::: block(c, "C"))

    case _ => sided.toSeq.headOption map {case ((c1, p), (c2, (x2, y2)), (c3, (x3, y3))) =>
       block(p, c1) ::: List.fill(x2)((c2 * y2) + (c3 * y3))
    }
  }
}) {
  override def format(result: Result) = result match {
    case None => "-1"
    case Some(block) => s"""
    |${block.size}
    |${block.mkString("\n")}
    """.stripMargin
  }
}
/********************************************************************************************/
import java.util.Scanner
abstract class CodeForcesApp[A](solver: Scanner => A) {
  final type Result = A
  final def apply(problem: String): String = apply(new Scanner(problem))
  final def apply(scanner: Scanner): String = format(solver(scanner))
  def format(result: Result): String = result.toString
  final def main(args: Array[String]): Unit = println(apply(new Scanner(System.in)))
}
object CodeForcesApp {
  final val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  final def debug(x: Any): Unit = if (!isOnlineJudge) println(x)
  final val modulus: Int = 1000000007
  final val eps: Double = 1e-9
  final def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) else None
  final def repeat[A](n: Int)(f: => A): Seq[A] = (1 to n).map(_ => f)
  final def desc[A: Ordering]: Ordering[A] = implicitly[Ordering[A]].reverse
  import scala.collection.mutable.{Map => Dict, Set => Bag}
  final def nil[A]: List[A] = List.empty[A]
  final def bag[A]: Bag[A] = Bag.empty[A]
  final def map[A] = new {
    def to[B](default: B): Dict[A, B] = Dict.empty[A, B] withDefaultValue default
  }
}

import java.util.Scanner

object Main extends App {

  val sc = new Scanner(System.in)
  val x1 = sc.nextInt
  val y1 = sc.nextInt
  val x2 = sc.nextInt
  val y2 = sc.nextInt
  val x3 = sc.nextInt
  val y3 = sc.nextInt

  var a = false

  def print1(x: Int,
             y: Int,
             z: Int,
             pos: Seq[Char]
            ) {
    println(x)
    (1 to y).foreach(_ => {(1 to x).foreach(_ => print(pos(0))); println})
    (1 to z).foreach(_ => {(1 to x).foreach(_ => print(pos(1))); println})
    (1 to (x - y - z)).foreach(_ => {(1 to x).foreach(_ => print(pos(2))); println})
  }

  def print2(x: Int,
             y: Int,
             z: Int,
             pos: Seq[Char]
            ) {
    println(x)
    (1 to y).foreach(_ => {(1 to x).foreach(_ => print(pos(0))); println})
    (1 to (x - y)).foreach(_ => {
      (1 to z).foreach(_ => print(pos(1)))
      (1 to (x - z)).foreach(_ => print(pos(2)))
      println
    })
  }

  def check1(p1: (Int, Int),
             p2: (Int, Int),
             p3: (Int, Int),
             pos: Seq[Char]
            ) {
    def c1 =
      (p1._1 == p2._1 && p1._1 == p3._1) &&
        (p1._2 + p2._2 + p3._2  == p1._1) &&
        ({print1(p1._1, p1._2, p2._2, pos); true})
    def c2 =
      (p2._2 == p3._2) && (p2._2 + p1._2 == p1._1) &&
        (p2._1 + p3._1 == p1._1) &&
        ({print2(p1._1, p1._2, p2._1, pos); true})

    a = a || c1 || c2
  }

  def check(p1: (Int, Int),
            p2: (Int, Int),
            p3: (Int, Int)
           ) = {
    check1(p1, p2, p3, Seq('A', 'B', 'C'))
    check1(p1, p3, p2, Seq('A', 'C', 'B'))
    check1(p2, p1, p3, Seq('B', 'A', 'C'))
    check1(p2, p3, p1, Seq('B', 'C', 'A'))
    check1(p3, p1, p2, Seq('C', 'A', 'B'))
    check1(p3, p2, p1, Seq('C', 'B', 'A'))
  }

  def c = {
    val p1 = (x1, y1)
    val p2 = (x2, y2)
    val p3 = (x3, y3)
    val s1 = Seq(p1, p1.swap)
    val s2 = Seq(p2, p2.swap)
    val s3 = Seq(p3, p3.swap)

    s1.foreach(a => {
      s2.foreach(b => {
        s3.foreach(c => {
          check(a, b, c)
        })
      })
    })
  }

  c
  if (!a)
    println(-1)
}

import java.io._
import scala.collection.mutable.Map

//581D
object D_ThreeLogos {

  def main(args: Array[String]): Unit = {
    val bis = new BufferedInputStream(System.in);
    val br = new BufferedReader(new InputStreamReader(bis));

    val line1 = br.readLine().split(" ")
//    val line1 = "5 1 2 5 5 2".split(" ")
//    val line1 = "4 4 2 6 4 2".split(" ")

    val coo = Array[Array[Int]]( Array(Integer.parseInt(line1(0)),Integer.parseInt(line1(1))),
        Array(Integer.parseInt(line1(2)),Integer.parseInt(line1(3))),
        Array(Integer.parseInt(line1(4)),Integer.parseInt(line1(5)))
        )

    val x1 = Integer.parseInt(line1(0))
    val y1 = Integer.parseInt(line1(1))
    val x2 = Integer.parseInt(line1(0))
    val y2 = Integer.parseInt(line1(1))
    val x3 = Integer.parseInt(line1(0))
    val y4 = Integer.parseInt(line1(1))

    val layouts = Array("123", "132", "213", "231", "312", "321")

    for (i <- layouts) {
      rows(i)
    }
    println(-1)
    System.exit(0)

    def rows(lay:String) {
      flips(lay)
      flips(lay.replace("1", "1."))
      flips(lay.replace("2", "2."))
      flips(lay.replace("1", "1.").replace("2", "2."))
    }

    def flips(row:String) {
      check(row)
      check(row.replace("", ""))
      check(row.replace("1", "1a"))
      check(row.replace("2", "2a"))
      check(row.replace("3", "3a"))
      check(row.replace("1", "1a").replace("2", "2a"))
      check(row.replace("2", "2a").replace("3", "3a"))
      check(row.replace("1", "1a").replace("3", "3a"))
      check(row.replace("1", "1a").replace("2", "2a").replace("3", "3a"))
    }

    def check(comb:String) {
//      println("variant: " + comb)
      if (comb == "1a23") {
//        println("here")
      }
      val grid = comb.split('.')
      val line0 = grid(0)
      var line0Li = parseLine(line0).reverse.toArray
      var width = 0
      for (i <- line0Li) {
        val ind = Integer.parseInt(i(0).toString()) - 1
        if (i.length() == 1) {
          width += coo(ind)(0)
        } else {
          width += coo(ind)(1)
        }
      }

      var pixCounter = 0
      val result = Array.ofDim[Char](width, width)
      var y = 0
      for (line <- grid) {
        val figNames = parseLine(line).reverse.toArray
        var x = 0
        for (i <- figNames) {
          val fig = getFig(i)
          if (x + fig._1 > width) return
          if (y + fig._2 > width) return

          //render
          for (fy <- 0 until fig._2) {
            for (fx <- 0 until fig._1) {
//              println("---- " + result(fx)(fy).toInt)
              val pixX = x + fx
              val pixY = y + fy
              if (result(pixY)(pixX).toInt != 0) {
                return
              }
              val dispChar = getFigChar(i)
              result(pixY)(pixX) = dispChar
              pixCounter += 1
            }
          }
          x += fig._1

        }
        y += getFig(figNames(0))._2
      }

      //DELME
//      printResult

      //success
      if (pixCounter == width * width) {
        println(width)
        printResult
        System.exit(0)
      }

      def printResult() {
        for (x <- 0 until result.length) {
          for (y <- 0 until result.length) {
            print(result(y)(x))
          }
          println
        }
      }
    }

    def getFigChar(name:String): Char = {
      if (name.startsWith("1")) 'A'
      else  if (name.startsWith("2")) 'B'
      else    if (name.startsWith("3")) 'C'
          else 'X'
    }

    def getFig(name:String): (Int, Int) = {
      val ind = Integer.parseInt(name(0).toString) - 1
        if (name.length() == 1) {
          (coo(ind)(0), coo(ind)(1))
        } else {
          (coo(ind)(1), coo(ind)(0))
        }
    }

    def parseLine(line:String):List[String] = {
      var res = List[String]()
      var cur = line(0).toString()
      for (i <- 1 until line.length()) {
        if (line(i) == 'a') {
          res = (cur + 'a') :: res
          cur = null
        } else {
          if (cur != null) {
            res = cur :: res
          }
          cur = line(i).toString
        }
      }
      if (cur != null) {
        res = cur :: res
      }
      res
    }
  }
}

import java.util.Scanner

import scala.language.postfixOps
import scala.math.sqrt

object TaskD extends App {
  type Board = Array[Array[Char]]
  val sc = new Scanner(System.in)
  val logos = 'A' to 'C' map {
    Logo(sc.nextInt, sc.nextInt, _)
  }

  val sq = logos.map(l => l.x * l.y).sum
  val sqr = sqrt(sq) toInt

  if (sqr * sqr != sq) {
    println(-1)
  } else {
    val a = sqr
    val board = Array.fill(a)(new Array[Char](a))
    if (logos.flatMap(l => List(l.x, l.y)).count(_ > a) > 0) {
      println(-1)
    } else {
      val res = findBestMatch(board, 0, 0, logos)
      if (res) {
        println(a)
        println(board.map(_.mkString).mkString("\n"))
      } else {
        println(-1)
      }
    }
  }

  def findBestMatch(board: Board, iOff: Int, jOff: Int, logos: Seq[Logo]): Boolean = {
    if (logos.nonEmpty) {
      val (w, h) = (board(0).length - jOff, board.length - iOff)
      (logos ++ logos.map(_.rotate)).find(l => l.x == w || l.y == h).exists { best =>
        val (io, jo) = fillBoard(board, iOff, jOff, best)
        findBestMatch(board, io, jo, logos diff Seq(best, best.rotate))
      }
    } else {
      true
    }
  }

  def fillBoard(board: Board, iOff: Int, jOff: Int, logo: Logo) = {
    val (iEnd, jEnd) = (iOff + logo.y, jOff + logo.x)
    iOff until iEnd foreach (i => jOff until jEnd foreach (j => board(i)(j) = logo.char))
    if (iEnd == board.length) (iOff, jEnd) else (iEnd, jOff)
  }

  case class Logo(x: Int, y: Int, char: Char) {
    def rotate = Logo(y, x, char)
  }

}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val data = in.next().split(" ").map(_.toInt).grouped(2).flatMap(_.sorted.reverse)
  val t = data.grouped(2).map(_.toList).toList.zip(List("A", "B", "C")).map{
    case(List(a, b), c) => (a, b, c)
  }
  var Seq((y1, x1, ch1), (y2, x2, ch2), (y3, x3, ch3)) = t.sorted.reverse.toSeq

  val a = y1
  if (x1 * y1 + x2 * y2 + x3 * y3 != a * a) {
    println(-1)
  }
  else {
    //разместим a
    if (y2 == a)  {
      if (y3 == a) {
        //разместим a b и с горизонтально
        println(((1 to x1).map(_ => ch1 * a).toList :::
        (1 to x2).map(_ => ch2 * a).toList :::
        (1 to x3).map(_ => ch3 * a).toList).mkString("\n"))
      }
      else
        println(-1)
    } else {
      val b = a - x1
      if (x2 == y3 && x2 == b && x3 + y2 == a) {
        println(((1 to x1).map(_ => ch1 * a).toList :::
          (1 to b).map(_ => ch2 * y2 + ch3 * x3).toList).mkString("\n"))
      }
      else if (x3 == y2 && y2 == b && x2 + y3 == a){

        println(((1 to x1).map(_ => ch1 * a).toList :::
          (1 to b).map(_ => ch2 * x2 + ch3 * y3 ).toList).mkString("\n"))
      }
      else if (y2 + y3 == a && x2 == x3 && x2== b){
        println(((1 to x1).map(_ => ch1 * a).toList :::
          (1 to b).map(_ => ch2 * y2 + ch3 * y3 ).toList).mkString("\n"))
      }
      else if (x2 + x3 == a && y2 == y3 && y2 == b){
        println(((1 to x1).map(_ => ch1 * a).toList :::
          (1 to b).map(_ => ch2 * x2 + ch3 * x3 ).toList).mkString("\n"))
      }
      else {
        println(-1)
      }
    }
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val data = in.next().split(" ").map(_.toInt).grouped(2).flatMap(_.sorted.reverse)
  val t = data.grouped(2).map(_.toList).toList.zip(List("A", "B", "C")).map{
    case(List(a, b), c) => (a, b, c)
  }
  var Seq((y1, x1, ch1), (y2, x2, ch2), (y3, x3, ch3)) = t.sorted.reverse.toSeq

  val a = y1
  println(a)
  if (x1 * y1 + x2 * y2 + x3 * y3 != a * a) {
    println(-1)
  }
  else {
    //разместим a
    if (y2 == a)  {
      if (y3 == a) {
        //разместим a b и с горизонтально
        println(((1 to x1).map(_ => ch1 * a).toList :::
        (1 to x2).map(_ => ch2 * a).toList :::
        (1 to x3).map(_ => ch3 * a).toList).mkString("\n"))
      }
      else
        println(-1)
    } else {
      val b = a - x1
      if (x2 == y3 && x2 == b && x3 + y2 == a) {
        println(((1 to x1).map(_ => ch1 * a).toList :::
          (1 to b).map(_ => ch2 * y2 + ch3 * x3).toList).mkString("\n"))
      }
      else if (x3 == y2 && y2 == b && x2 + y3 == a){

        println(((1 to x1).map(_ => ch1 * a).toList :::
          (1 to b).map(_ => ch2 * x2 + ch3 * y3 ).toList).mkString("\n"))
      }
      else if (y2 + y3 == a && x2 == x3 && x2== b){
        println(((1 to x1).map(_ => ch1 * a).toList :::
          (1 to b).map(_ => ch2 * y2 + ch3 * y3 ).toList).mkString("\n"))
      }
      else if (x2 + x3 == a && y2 == y3 && y2 == b){
        println(((1 to x1).map(_ => ch1 * a).toList :::
          (1 to b).map(_ => ch2 * x2 + ch3 * x3 ).toList).mkString("\n"))
      }
      else {
        println(-1)
      }
    }
  }
}

import java.io._
import java.util._

import scala.collection.mutable

object D {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close
  }

  def next: String = {
    while (st == null || !st.hasMoreTokens) {
      st = new StringTokenizer(br.readLine)
    }
    return st.nextToken
  }

  def nextInt: Int = return Integer.parseInt(next)

  def nextLong: Long = return java.lang.Long.parseLong(next)

  def nextDouble: Double = return java.lang.Double.parseDouble(next)

  class MultiHashSet[T <% Comparable[T]] {
    val map = new mutable.HashMap[T, Int]()

    def count(x: T): Int = {
      return map.getOrElse(x, 0)
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  class MultiTreeSet[T <% Comparable[T]] {
    val map = new TreeMap[T, Int]()

    def count(x: T): Int = {
      val res = map.get(x)
      if (res == null)
        return 0
      return res
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def first(): T = return map.firstKey()

    def last(): T = return map.lastKey()

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  /**
   * Segment tree for any commutative function
   * @param values Array of Int
   * @param commutative function like min, max, sum
   * @param zero zero value - e.g. 0 for sum, Inf for min, max
   */
  class SegmentTree(values: Array[Int])(commutative: (Int, Int) => Int)(zero: Int) {
    private val SIZE = 1e5.toInt
    private val n = values.length
    val t = new Array[Int](2 * n)
    Array.copy(values, 0, t, n, n)

    // build segment tree
    def build = {
      for (i <- n - 1 until 0 by -1) {
        t(i) = commutative(t(2 * i), t(2 * i + 1))
      }
    }

    // change value at position p to x
    // TODO beatify
    def modify(p: Int, x: Int) = {
      var pos = p + n
      t(pos) = x
      while (pos > 1) {
        t(pos / 2) = commutative(t(pos), t(pos ^ 1))
        pos /= 2
      }
    }

    // TODO implement me!
    def modify(p: Int, left: Int, right: Int) = ???

    def query(p: Int) = ???

    // sum [l, r)
    // min l = 0
    // max r = n
    // TODO beatify
    def query(left: Int, right: Int): Int = {
      var res = zero
      var r = right + n
      var l = left + n
      while (l < r) {
        if (l % 2 == 1) {
          res = commutative(res, t(l))
          l += 1
        }
        if (r % 2 == 1) {
          r -= 1
          res = commutative(res, t(r))
        }
        l /= 2
        r /= 2
      }
      res
    }
  }

  case class Rectangle(width: Int, height: Int) {

    def normalize(): Rectangle = {
      new Rectangle(Math.min(width, height), Math.max(width, height))
    }
  }

  def getSecond(i: Int): Int = {
    i match {
      case 0 => 1
      case 1 => 2
      case 2 => 0
    }
  }

  def getThird(i: Int): Int = {
    i match {
      case 0 => 2
      case 1 => 0
      case 2 => 1
    }
  }

  def rectLetter(i: Int): String = {
    i match {
      case 0 => "A"
      case 1 => "B"
      case 2 => "C"
    }
  }

  def solve: Int = {
    val x1 = nextInt
    val y1 = nextInt
    val a = new Rectangle(x1, y1).normalize
    val x2 = nextInt
    val y2 = nextInt
    val b = new Rectangle(x2, y2).normalize
    val x3 = nextInt
    val y3 = nextInt
    val c = new Rectangle(x3, y3).normalize
    if (a.height == b.height && b.height == c.height && a.width + b.width + c.width == a.height) {
      // output the result
      for (i <- 0 until a.height) {
        for (j <- 0 until a.height) {
          if (j < a.width) {
            out.print("A")
          } else if (j < a.width + b.width) {
            out.print("B")
          } else {
            out.print("C")
          }
        }
        out.println()
      }
      return 0
    }
    val rects = new Array[Rectangle](3)
    rects(0) = a
    rects(1) = b
    rects(2) = c
    for (i <- 0 until 3) {
      val fst = rects(i)
      val snd = rects(getSecond(i))
      val thrd = rects(getThird(i))
      if (fst.height == snd.width + thrd.width && fst.height == fst.width + snd.height && snd.height == thrd.height) {
        for (j <- 0 until fst.height) {
          for (k <- 0 until fst.height) {
            if (j < snd.height) {
              if (k < snd.width) {
                out.print(rectLetter(getSecond(i)))
              } else {
                out.print(rectLetter(getThird(i)))
              }
            } else {
              out.print(rectLetter(i))
            }
          }
          out.println()
        }
        return 0
      }
    }
    out.println(-1)
    return 0
  }
}

import java.io._
import java.util._

import scala.collection.mutable

object D {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close
  }

  def next: String = {
    while (st == null || !st.hasMoreTokens) {
      st = new StringTokenizer(br.readLine)
    }
    return st.nextToken
  }

  def nextInt: Int = return Integer.parseInt(next)

  def nextLong: Long = return java.lang.Long.parseLong(next)

  def nextDouble: Double = return java.lang.Double.parseDouble(next)

  class MultiHashSet[T <% Comparable[T]] {
    val map = new mutable.HashMap[T, Int]()

    def count(x: T): Int = {
      return map.getOrElse(x, 0)
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  class MultiTreeSet[T <% Comparable[T]] {
    val map = new TreeMap[T, Int]()

    def count(x: T): Int = {
      val res = map.get(x)
      if (res == null)
        return 0
      return res
    }

    def add(x: T): Unit = map.put(x, count(x) + 1)

    def first(): T = return map.firstKey()

    def last(): T = return map.lastKey()

    def remove(x: T): Boolean = {
      val prev = count(x)
      if (prev == 0)
        return false
      if (prev == 1) {
        map.remove(x)
      } else {
        map.put(x, prev - 1)
      }
      return true
    }
  }

  /**
   * Segment tree for any commutative function
   * @param values Array of Int
   * @param commutative function like min, max, sum
   * @param zero zero value - e.g. 0 for sum, Inf for min, max
   */
  class SegmentTree(values: Array[Int])(commutative: (Int, Int) => Int)(zero: Int) {
    private val SIZE = 1e5.toInt
    private val n = values.length
    val t = new Array[Int](2 * n)
    Array.copy(values, 0, t, n, n)

    // build segment tree
    def build = {
      for (i <- n - 1 until 0 by -1) {
        t(i) = commutative(t(2 * i), t(2 * i + 1))
      }
    }

    // change value at position p to x
    // TODO beatify
    def modify(p: Int, x: Int) = {
      var pos = p + n
      t(pos) = x
      while (pos > 1) {
        t(pos / 2) = commutative(t(pos), t(pos ^ 1))
        pos /= 2
      }
    }

    // TODO implement me!
    def modify(p: Int, left: Int, right: Int) = ???

    def query(p: Int) = ???

    // sum [l, r)
    // min l = 0
    // max r = n
    // TODO beatify
    def query(left: Int, right: Int): Int = {
      var res = zero
      var r = right + n
      var l = left + n
      while (l < r) {
        if (l % 2 == 1) {
          res = commutative(res, t(l))
          l += 1
        }
        if (r % 2 == 1) {
          r -= 1
          res = commutative(res, t(r))
        }
        l /= 2
        r /= 2
      }
      res
    }
  }

  case class Rectangle(width: Int, height: Int) {

    def normalize(): Rectangle = {
      new Rectangle(Math.min(width, height), Math.max(width, height))
    }
  }

  def getSecond(i: Int): Int = {
    i match {
      case 0 => 1
      case 1 => 2
      case 2 => 0
    }
  }

  def getThird(i: Int): Int = {
    i match {
      case 0 => 2
      case 1 => 0
      case 2 => 1
    }
  }

  def rectLetter(i: Int): String = {
    i match {
      case 0 => "A"
      case 1 => "B"
      case 2 => "C"
    }
  }

  def solve: Int = {
    val x1 = nextInt
    val y1 = nextInt
    val a = new Rectangle(x1, y1).normalize
    val x2 = nextInt
    val y2 = nextInt
    val b = new Rectangle(x2, y2).normalize
    val x3 = nextInt
    val y3 = nextInt
    val c = new Rectangle(x3, y3).normalize
    if (a.height == b.height && b.height == c.height && a.width + b.width + c.width == a.height) {
      // output the result
      out.println(a.height)
      for (i <- 0 until a.height) {
        for (j <- 0 until a.height) {
          if (j < a.width) {
            out.print("A")
          } else if (j < a.width + b.width) {
            out.print("B")
          } else {
            out.print("C")
          }
        }
        out.println()
      }
      return 0
    }
    val rects = new Array[Rectangle](3)
    rects(0) = a
    rects(1) = b
    rects(2) = c
    for (i <- 0 until 3) {
      val fst = rects(i)
      val snd = rects(getSecond(i))
      val thrd = rects(getThird(i))
      if (fst.height == snd.width + thrd.width && fst.height == fst.width + snd.height && snd.height == thrd.height) {
        // output the result
        out.println(fst.height)
        for (j <- 0 until fst.height) {
          for (k <- 0 until fst.height) {
            if (j < snd.height) {
              if (k < snd.width) {
                out.print(rectLetter(getSecond(i)))
              } else {
                out.print(rectLetter(getThird(i)))
              }
            } else {
              out.print(rectLetter(i))
            }
          }
          out.println()
        }
        return 0
      }
    }
    out.println(-1)
    return 0
  }
}

import java.io._
import scala.collection.mutable.Map

//581D
object D_ThreeLogos {

  def main(args: Array[String]): Unit = {
    val bis = new BufferedInputStream(System.in);
    val br = new BufferedReader(new InputStreamReader(bis));

    val line1 = br.readLine().split(" ")
//    val line1 = "5 1 2 5 5 2".split(" ")
//    val line1 = "4 4 2 6 4 2".split(" ")

    val coo = Array[Array[Int]]( Array(Integer.parseInt(line1(0)),Integer.parseInt(line1(1))),
        Array(Integer.parseInt(line1(2)),Integer.parseInt(line1(3))),
        Array(Integer.parseInt(line1(4)),Integer.parseInt(line1(5)))
        )

    val x1 = Integer.parseInt(line1(0))
    val y1 = Integer.parseInt(line1(1))
    val x2 = Integer.parseInt(line1(0))
    val y2 = Integer.parseInt(line1(1))
    val x3 = Integer.parseInt(line1(0))
    val y4 = Integer.parseInt(line1(1))

    val layouts = Array("123", "132", "213", "231", "312", "321")

    for (i <- layouts) {
      rows(i)
      println(-1)
    }

    def rows(lay:String) {
      flips(lay)
      flips(lay.replace("1", "1."))
      flips(lay.replace("2", "2."))
      flips(lay.replace("1", "1.").replace("2", "2."))
    }

    def flips(row:String) {
      check(row)
      check(row.replace("", ""))
      check(row.replace("1", "1a"))
      check(row.replace("2", "2a"))
      check(row.replace("3", "3a"))
      check(row.replace("1", "1a").replace("2", "2a"))
      check(row.replace("2", "2a").replace("3", "3a"))
      check(row.replace("1", "1a").replace("3", "3a"))
      check(row.replace("1", "1a").replace("2", "2a").replace("3", "3a"))
    }

    def check(comb:String) {
//      println("variant: " + comb)
      if (comb == "1a23") {
//        println("here")
      }
      val grid = comb.split('.')
      val line0 = grid(0)
      var line0Li = parseLine(line0).reverse.toArray
      var width = 0
      for (i <- line0Li) {
        val ind = Integer.parseInt(i(0).toString()) - 1
        if (i.length() == 1) {
          width += coo(ind)(0)
        } else {
          width += coo(ind)(1)
        }
      }

      var pixCounter = 0
      val result = Array.ofDim[Char](width, width)
      var y = 0
      for (line <- grid) {
        val figNames = parseLine(line).reverse.toArray
        var x = 0
        for (i <- figNames) {
          val fig = getFig(i)
          if (x + fig._1 > width) return
          if (y + fig._2 > width) return

          //render
          for (fy <- 0 until fig._2) {
            for (fx <- 0 until fig._1) {
//              println("---- " + result(fx)(fy).toInt)
              val pixX = x + fx
              val pixY = y + fy
              if (result(pixY)(pixX).toInt != 0) {
                return
              }
              val dispChar = getFigChar(i)
              result(pixY)(pixX) = dispChar
              pixCounter += 1
            }
          }
          x += fig._1

        }
        y += getFig(figNames(0))._2
      }

      //DELME
//      printResult

      //success
      if (pixCounter == width * width) {
        println(width)
        printResult
        System.exit(0)
      }

      def printResult() {
        for (x <- 0 until result.length) {
          for (y <- 0 until result.length) {
            print(result(y)(x))
          }
          println
        }
      }
    }

    def getFigChar(name:String): Char = {
      if (name.startsWith("1")) 'A'
      else  if (name.startsWith("2")) 'B'
      else    if (name.startsWith("3")) 'C'
          else 'X'
    }

    def getFig(name:String): (Int, Int) = {
      val ind = Integer.parseInt(name(0).toString) - 1
        if (name.length() == 1) {
          (coo(ind)(0), coo(ind)(1))
        } else {
          (coo(ind)(1), coo(ind)(0))
        }
    }

    def parseLine(line:String):List[String] = {
      var res = List[String]()
      var cur = line(0).toString()
      for (i <- 1 until line.length()) {
        if (line(i) == 'a') {
          res = (cur + 'a') :: res
          cur = null
        } else {
          if (cur != null) {
            res = cur :: res
          }
          cur = line(i).toString
        }
      }
      if (cur != null) {
        res = cur :: res
      }
      res
    }
  }
}


You are given three integers $$$x, y$$$ and $$$n$$$. Your task is to find the maximum integer $$$k$$$ such that $$$0 \le k \le n$$$ that $$$k \bmod x = y$$$, where $$$\bmod$$$ is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given $$$x, y$$$ and $$$n$$$ you need to find the maximum possible integer from $$$0$$$ to $$$n$$$ that has the remainder $$$y$$$ modulo $$$x$$$.You have to answer $$$t$$$ independent test cases. It is guaranteed that such $$$k$$$ exists for each test case.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. The next $$$t$$$ lines contain test cases. The only line of the test case contains three integers $$$x, y$$$ and $$$n$$$ ($$$2 \le x \le 10^9;~ 0 \le y < x;~ y \le n \le 10^9$$$). It can be shown that such $$$k$$$ always exists under the given constraints.

For each test case, print the answer — maximum non-negative integer $$$k$$$ such that $$$0 \le k \le n$$$ and $$$k \bmod x = y$$$. It is guaranteed that the answer always exists.

NoteIn the first test case of the example, the answer is $$$12339 = 7 \cdot 1762 + 5$$$ (thus, $$$12339 \bmod 7 = 5$$$). It is obvious that there is no greater integer not exceeding $$$12345$$$ which has the remainder $$$5$$$ modulo $$$7$$$.

//package codeforces.contests._1374

/*
https://codeforces.com/contest/1374/problem/A
 */
object RequiredRemainder {
  def main(args: Array[String]): Unit = {
    for (_ <- 1 to io.StdIn.readInt()) {
      val Array(x, y, n) = io.StdIn.readLine.split(" ").map(_.toInt)

      val maxN = {
        val r = n % x
        val mN = n - r + y
        if (mN > n) mN - x else mN
      }

      println(maxN)
    }
  }
}

import scala.io.StdIn._

object CF653_A extends App {
  val t = readInt()
  for (_ <- 0 until t) {
    val xyn = readLine()
    val tmp = xyn.split(' ').map(_.toInt)
    var (x, y, n) = (tmp(0), tmp(1), tmp(2))

//    println(x, y, n)
//    println(List(1, 2, 3))
//    val (x, y, n) = new Tuple3(: _*)

    println(n - (n - y) % x)
//    println((n - y) / x)
  }
}

import java.io._
import java.util.StringTokenizer

import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new CF653A(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())

    def nextLong(): Long = java.lang.Long.parseLong(next())

    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()

    def nl(): Long = nextLong()

    def nc(): Char = nextChar()

    def ns(): String = next()

    def ns(n: Int): Array[Char] = ns().toCharArray

    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)

    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }

    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }

    def nal(n: Int): Array[Long] = map(n)(_ => nl())

    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n)(_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while (i < N) {
      f(i);
      i += 1
    }
  }

  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while (i >= offset) {
      f(i);
      i -= 1
    }
  }

  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class CF653A(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
    val t = ni()
    REP(t) { _ =>
      val x = ni()
      val y = ni()
      val n = ni()

      val r = n / x
      if(r * x + y <= n) out.println(r * x + y)
      else out.println((r-1)*x + y)
    }
  }
}

object A extends App {
  import java.io._
  import java.nio.file.{Files, Path}
  import java.util.StringTokenizer
  import scala.io.Source
  import scala.io.Codec

  /* Implicit methods */
  implicit def intWithTimes(n: Int) = {
    new {
      def times(f: => Unit): Unit = 1.to(n).foreach { _ => f }
    }
  }
  /* Implicit methods */

//    val in = new CustomScanner(Source.fromResource("input.txt").mkString)
  val in  = new CustomScanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val n = in.nextInt()

    n.times {
      val x = in.nextInt()
      val y = in.nextInt()
      val k = in.nextInt()

      val r = (k - y) / x
      println((r * x) + y)
    }
  }

  try {
    solve()
  } catch {
    case ex: Exception =>
      println(ex)
      sys.exit(9999)
  } finally {
    out.flush()
    out.close()
  }

  /**
    * Util methods
    */

  def printArr[T](in: Array[T]) = {
    in.foreach(print)
    println()
  }

  /**
    * Scala implementation of a faster java.util.Scanner
    * See: http://codeforces.com/blog/entry/7018
    */
  class CustomScanner(reader: LineNumberReader) extends Iterator[String] with AutoCloseable {

    def this(reader: BufferedReader) = this(new LineNumberReader(reader))

    def this(reader: Reader) = this(new BufferedReader(reader))

    def this(inputStream: InputStream)(implicit codec: Codec) = this(new InputStreamReader(inputStream, codec.charSet))

    def this(path: Path)(implicit codec: Codec) = this(Files.newBufferedReader(path, codec.charSet))

    def this(file: File)(implicit codec: Codec) = this(file.toPath)(codec)

    def this(str: String) = this(new StringReader(str))

    private[this] val tokenizers = {
      Iterator
        .continually(reader.readLine())
        .takeWhile(_ != null)
        .map(new StringTokenizer(_))
        .filter(_.hasMoreTokens)
    }

    private[this] var current: Option[StringTokenizer] = None

    @inline private[this] def tokenizer(): Option[StringTokenizer] =
      current.find(_.hasMoreTokens) orElse {
        current = if (tokenizers.hasNext) Some(tokenizers.next()) else None
        current
      }

    /**
      * Unlike Java's scanner which returns till end of current line, this actually returns the next line
      * @see line() if you want the Java behaviour
      */
    def nextLine(): String = {
      current = None // reset
      reader.readLine()
    }

    def lineNumber: Int = reader.getLineNumber

    def line(): String = tokenizer().get.nextToken("\n\r")

    def nextString(): String = next()

    def nextChar(): Char = next().ensuring(_.length == 1).head

    def nextBoolean(): Boolean = next().toBoolean

    def nextByte(radix: Int = 10): Byte = java.lang.Byte.parseByte(next(), radix)

    def nextShort(radix: Int = 10): Short = java.lang.Short.parseShort(next(), radix)

    def nextInt(radix: Int = 10): Int = java.lang.Integer.parseInt(next(), radix)

    def nextLong(radix: Int = 10): Long = java.lang.Long.parseLong(next(), radix)

    def nextBigInt(radix: Int = 10): BigInt = BigInt(next(), radix)

    def nextFloat(): Float = next().toFloat

    def nextDouble(): Double = next().toDouble

    def nextBigDecimal(): BigDecimal = BigDecimal(next())

    override def next(): String = tokenizer().get.nextToken()

    override def hasNext: Boolean = tokenizer().nonEmpty

    override def close(): Unit = reader.close()
  }
}

import scala.io.StdIn.readLine
object Main {
  def main(args: Array[String]) {
    val T = readLine.split(" ")(0).toInt
    for(cas <- 0 until T) {
      val Line = readLine.split(" ").map(
        item => item.toInt
      )
      val x = Line(0)
      val y = Line(1)
      val n = Line(2)
      var r = n / x * x + y
      if(r > n) {
        r -= x
      }
      println(r)
    }
  }
}


Misha was interested in water delivery from childhood. That's why his mother sent him to the annual Innovative Olympiad in Irrigation (IOI). Pupils from all Berland compete there demonstrating their skills in watering. It is extremely expensive to host such an olympiad, so after the first $$$n$$$ olympiads the organizers introduced the following rule of the host city selection.The host cities of the olympiads are selected in the following way. There are $$$m$$$ cities in Berland wishing to host the olympiad, they are numbered from $$$1$$$ to $$$m$$$. The host city of each next olympiad is determined as the city that hosted the olympiad the smallest number of times before. If there are several such cities, the city with the smallest index is selected among them.Misha's mother is interested where the olympiad will be held in some specific years. The only information she knows is the above selection rule and the host cities of the first $$$n$$$ olympiads. Help her and if you succeed, she will ask Misha to avoid flooding your house.

The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \leq n, m, q \leq 500\,000$$$) — the number of olympiads before the rule was introduced, the number of cities in Berland wishing to host the olympiad, and the number of years Misha's mother is interested in, respectively. The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq m$$$), where $$$a_i$$$ denotes the city which hosted the olympiad in the $$$i$$$-th year. Note that before the rule was introduced the host city was chosen arbitrarily. Each of the next $$$q$$$ lines contains an integer $$$k_i$$$ ($$$n + 1 \leq k_i \leq 10^{18}$$$) — the year number Misha's mother is interested in host city in.

Print $$$q$$$ integers. The $$$i$$$-th of them should be the city the olympiad will be hosted in the year $$$k_i$$$.

NoteIn the first example Misha's mother is interested in the first $$$10$$$ years after the rule was introduced. The host cities these years are 4, 3, 4, 2, 3, 4, 1, 2, 3, 4.In the second example the host cities after the new city is introduced are 2, 3, 1, 2, 3, 5, 1, 2, 3, 4, 5, 1.

import java.util.Comparator

object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private val oj = System.getenv("ATCODER_DEBUG") == null
  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  def debugDim(m: Array[Array[Int]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from) { i =>
      f(i)
    }
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }
  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer

  // toIntとか+7とかするならvalにしろ
  @inline private def MOD = 1000000007

  case class City(i: Int, held: Int)
  case class Query(id: Int, year: Long)

  def solve(): Unit = {
    val M, N, Q = ni()
    val C = Array.ofDim[Int](N)
    REP(M) { _ =>
      val i = ni() - 1
      C(i) += 1
    }

    debug(C)

    val queryQueue = new java.util.PriorityQueue[Query](Q, new Comparator[Query] {
      override def compare(o1: Query, o2: Query): Int = {
        java.lang.Long.compare(o1.year, o2.year)
      }
    })
    REP(Q) { i =>
      queryQueue.add(Query(i, nl() - M - 1)) // 0年スタート
    }

    val cityQueue = new java.util.PriorityQueue[City](N, new Comparator[City] {
      override def compare(o1: City, o2: City): Int = {
        if (o1.held == o2.held) Integer.compare(o1.i, o2.i)
        else Integer.compare(o1.held, o2.held)
      }
    })

    val minHeld = C.min
    REP(N) { i =>
      cityQueue.add(City(i, C(i) - minHeld))
    }

    val INF = 1e18.toLong

    val ans = Array.ofDim[Int](Q)
    var year = 0L
    var cityCnt = 0
    var held = 0L
    val bit = new BIT(N, 0)(_+_)
    def expectedHeld(targetYear: Long): Long = {
      if (cityCnt == 0) INF else (targetYear - year) / cityCnt + held
    }

    def findCity(targetYear: Long): Int = {
      val cnt = (targetYear - year) % cityCnt + 1
      debug(s"fincCity($targetYear) cnt:$cnt")
      def test(i: Int): Boolean = {
        cnt <= bit.sum(i + 1)
      }

      findMin(test, 0, N - 1)
    }

    while(!queryQueue.isEmpty) {
      val q = queryQueue.poll()
      debug(s"q:$q")
      // q.year以前に追加されるcityをここで追加する
      while(!cityQueue.isEmpty && expectedHeld(q.year) >= cityQueue.peek().held) {
        val city = cityQueue.poll()
        year += (city.held - held) * cityCnt // 今のcitiesだけでcity.heldまで進める
        held = city.held
        bit.add(city.i, 1)
        cityCnt += 1
        debug(s"city add $city year:$year cityCnt:$cityCnt held:$held")
      }
      ans(q.id) = findCity(q.year) + 1
    }

    ans.foreach(out.println)
  }

  def findMin(f: Int => Boolean, min: Int, max: Int): Int = {
    var l = min - 1
    var h = max
    while(h - l > 1) {
      val x = (h + l) / 2
      if (f(x)) h = x
      else l = x
    }
    h
  }

class BIT(n: Int, zero: Int)(f: (Int, Int) => Int) {
  def this(n: Int) = this(n, 0)(_ + _)

  private val N = n
  private val bit = if (zero != 0) Array.fill[Int](N + 1)(zero) else Array.ofDim[Int](N + 1)

  /**
    * 1 index
    * addとindex違うよ
    * cumsumなんかといっしょ
    */
  def sum(i: Int): Int = {
    assert(i <= n)
    var x = i
    var s: Int = 0
    while(x > 0) {
      s = f(s, bit(x))
      x -= x & -x
    }
    s
  }

  /**
    * 0 index
    */
  def add(i: Int, a: Int): Unit = {
    assert(i < n)
    var x = i + 1
    while(x <= N) {
      bit(x) = f(bit(x), a)
      x += x & -x
    }
  }

  def sumAll: Int = sum(n)

  /**
    * [l, r)
    */
  def query(l: Int, r: Int)(sub: (Int, Int) => Int): Int = {
    assert(r > l)
    sub(sum(r), sum(l))
  }
}
}


3R2 - Standby for ActionOur dear Cafe's owner, JOE Miller, will soon take part in a new game TV-show "1 vs. $$$n$$$"!The game goes in rounds, where in each round the host asks JOE and his opponents a common question. All participants failing to answer are eliminated. The show ends when only JOE remains (we assume that JOE never answers a question wrong!).For each question JOE answers, if there are $$$s$$$ ($$$s > 0$$$) opponents remaining and $$$t$$$ ($$$0 \le t \le s$$$) of them make a mistake on it, JOE receives $$$\displaystyle\frac{t}{s}$$$ dollars, and consequently there will be $$$s - t$$$ opponents left for the next question.JOE wonders what is the maximum possible reward he can receive in the best possible scenario. Yet he has little time before show starts, so can you help him answering it instead?

The first and single line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), denoting the number of JOE's opponents in the show.

Print a number denoting the maximum prize (in dollars) JOE could have. Your answer will be considered correct if it's absolute or relative error won't exceed $$$10^{-4}$$$. In other words, if your answer is $$$a$$$ and the jury answer is $$$b$$$, then it must hold that $$$\frac{|a - b|}{max(1, b)} \le 10^{-4}$$$.

NoteIn the second example, the best scenario would be: one contestant fails at the first question, the other fails at the next one. The total reward will be $$$\displaystyle \frac{1}{2} + \frac{1}{1} = 1.5$$$ dollars.

object Main {
    def main(args: Array[String]): Unit = {
        val n = readInt
        val s = (1 to n).foldLeft(0.0){
            case (d, i) => d + (1.0 / i)
        }
        println(s)
    }
}