loop do
    t = gets.to_i
    break if t == 0
    cycle = gets.split.map(&:to_i)

    schedule = Hash.new(Float::INFINITY)
    n = gets.to_i
    n.times do
        d, t = gets.split.map(&:to_i)
        schedule[d] = t if t < schedule[d]
    end

    table = [nil] * cycle.size
    table[-1] = 0
    (1..schedule.keys.max).each do |d|
        m = table.compact.min + 1
        table.unshift(table.pop)
        (1..cycle.size-1).each do |i|
            table[i] = nil if cycle[i] > schedule[d]
        end
        table[0] = m if !table[0] || m < table[0]
    end

    p table.compact.min
end