a = [] gets.to_i.times do a << gets.split.map(&:to_i) end m = [] gets.to_i.times do m << gets.split.map(&:to_i) end count = Array.new(m.size, 0) a.each do |i| m.each_with_index do |j,k| count[k] += 1 if j[0] <= i[0] && j[1] >= i[1] end end if count.all?{|i| i == 0} p 0 else max = count.max count.each_with_index do |i,j| p j+1 if i == max end end