def unbounded_knapsack(items, target_value)
	table = [0]
	i = 0
	hash = {0 => 0}
	loop {
		i += 1
		table[i] = 0
		items.each{|c, v|
			next if c > i
			x = table[i-c] + v
			table[i] = x if x > table[i]
		}
		hash[table[i]] ||= i
		return hash.to_a if table[i] >= target_value
	}
end

loop {
	n = gets.to_i
	break if n == 0
	enemies = (1..n).map{gets.to_i}.sort
	m = gets.to_i
	single_spells = []
	all_spells = []
	zero = false
	(1..m).each{
		_, cost, type, damage = gets.chomp.split
		if cost == ?0
			if damage == ?0
				next
			else
				zero = true
			end
		end
		next if damage == ?0
		
		if type == 'Single'
			single_spells << [cost, damage].map(&:to_i)
		else
			all_spells << [cost, damage].map(&:to_i)
		end
	}

	if zero
		puts 0
		next
	end

	single_cost = (single_spells.empty? ? [] : unbounded_knapsack(single_spells, enemies.max))
	all_cost = (all_spells.empty? ? [] : unbounded_knapsack(all_spells, enemies.max))

	if all_cost.empty?
		cost = 0
		enemies.each{|hp|
			cost += single_cost.find{|v, c| v >= hp}[1]
		}
		puts cost
	elsif single_cost.empty?
		hp = enemies.max
		puts all_cost.find{|v, c| v >= hp}[1]
	else
		min = Float::INFINITY
		(0..enemies.max).each{|limit|
			all_cost.shift until all_cost[0][0] >= limit
			cost = all_cost[0][1]
			break if cost >= min
			
			l = 0
			enemies.each{|hp|
				if hp > limit
					l += 1 until single_cost[l][0] >= hp - limit
					cost += single_cost[l][1]
					break if cost >= min
				end
			}
			min = cost if cost < min
		}
		p min
	end
}