x = gets.chomp.to_i
result = []
x.times do
  ary = []
  tmp = 0
  s = gets.chomp.to_i
  
  10.times do
  s = s * s
  tmp = sprintf("%08d",s)
  tmp = tmp[2,4].to_i
  ary << tmp
  s = tmp
  end
result << ary
end

result.each_with_index do |v,i|
 puts "Case #{i+1}:"
 puts v
 end
