# 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

i=0
while (n=gets.to_i)!=0
  n.times {
    puts "Case #{i+=1}:"
    s=gets.to_i
    10.times {
      s=middle_square(s, 4)
      puts s
    }
  }
end