def msm(s)
  seed = s
  Proc.new do
    sq = (seed*seed).to_s
    sq = "0" + sq until 8 <= sq.size
    seed = sq[2,4].to_i
  end
end

n = gets.to_i
n.times do |i|
  s = gets.to_i
  _msm = msm(s)
  puts "Case #{i+1}:"
  10.times { puts _msm.call }
end