# Middle-Square Method

def middle_square(s, n)
  ss=(s**2).to_s
  ss='0'*(n*2-ss.length)+ss
  ss[n/2..n+1].to_i
end

gets.to_i.times { |i|
  puts "Case #{i+1}:"
  s=gets.to_i
  10.times {
    s=middle_square(s, 4)
    puts s
  }
}