t = gets.to_i t.times do c = gets.to_i cs = c.to_s i = 3*(c/3+1) j = 10 while true x = i - c if (cs + x.to_s).to_i % (c + x) == 0 puts x break end if (cs + j.to_s).to_i % (c + j) == 0 puts j break end i += 3 j += 10 end end