N = gets.to_i.times.map{ gets.split.map(&:to_i) } gets.to_i.times.map{gets.split.map(&:to_i)} .group_by{|x,y| N.count{|a,b| x >=a && y <= b } } .then do |h| m = h.keys.max puts m == 0 ? 0 : h[m].size end