t = gets.to_i t.times do c = gets.to_i cs = c.to_s k = cs.size i = ('1' * k).to_i j = i while true if i.to_s.size > k k += 1 i = ('1' * k).to_i j = i end x = 1 x = i - c if i > c if (cs + x.to_s).to_i % (c + x) == 0 puts x break end i += j end end