import scala.io.StdIn.readInt

object Main extends App {
  val n = readInt
  for(i <- 1 to n) {
    var m = readInt

    println("Case " + i + ":")
    for(j <- 1 to 10){
      m = "%08d".format(m*m).substring(2,6).toInt
      println(m)
    }
  }
}