
number_cards, number_questions = gets.split.map do |tmp|
  tmp.to_i
end
cards = gets.split.map do |tmp|
  tmp.to_i
end
cards.sort!
number_questions.times.with_index(1) do |y, n|
  # puts "??????#{n}??????"
  q = gets.to_i
  hoge = nil
  # p hoge
  counter = 1
  amari = []
  q.times.with_index(1) do |y, counter|
    q.times.with_index(1) do |z, i|
      # puts "cards?????????#{cards}"
      # puts "-----?¨????#{counter}??????"
      # puts "??????#{q*counter - i}"
      p cards.include?(q*counter - i)
      hoge = cards[cards.index(q*counter - i)] if cards.include?(q*counter - i)
      # puts "hoge:#{hoge} = q*counter - i"
      break unless hoge.nil?
    end
    amari << hoge % q
    break if hoge == q-1
  end

  # puts "??????#{amari.last}"
  puts amari.last

end