class MiddleSquare  # 8digit

  def initialize(seed)
    @seed = seed
    @n=4
  end

  def random
    t= (@seed**2).to_s
    r=("00000000"+t.slice(0..-(@n/2+1))).slice(-@n..-1).to_i
    @seed = r
    return r
  end

end

def main
  if true then
    fh=STDIN
  else
    fh=open("in1_0137.txt")
  end
  n=fh.gets.chomp.to_i
  0.upto(n-1){|i|
    s=fh.gets.chomp.to_i
    ans=[]
    r=MiddleSquare.new(s)
    10.times{|j|
      ans.push(r.random)
    }
    puts "Case #{i+1}:"
    puts ans
  }
end

if $0==__FILE__ then
  main
end