loop do
    n = gets.to_i
    break if n == 0
    a = (1..n).map { gets.split.map(&:to_i) }
    m = gets.to_i
    a += (1..m).map { gets.split.map(&:to_i) }

    a.sort!.uniq!

    hash = Hash.new(Float::INFINITY) # tab[k]: min r s.t num = k
    hash[[0, 0]] = 0
    a.each do |h, r|
        new_hash = Hash.new(Float::INFINITY)
        hash.each do |key, min_r|
            height, num = key #height <= h
            new_hash[key] = min_r if min_r < new_hash[key]
            if height < h and min_r < r
                new_hash[[h, num+1]] = r if r < new_hash[[h, num+1]]
            end
        end
        hash = new_hash
    end
    p hash.keys.map(&:last).max
end

