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

def inverse_unbounded_knapsack(items, target_value)
	table = [0]
	for i in 1..target_value
		table[i] = Float::INFINITY
		items.each{|c, v|
			next if v > i
			x = table[i-v] + c
			table[i] = x if x < table[i]
		}
	end
	(target_value-1).downto(0){|i|
		table[i] = table[i+1] if table[i] > table[i+1]
	}
	table
end

loop {
	n = gets.to_i
	break if n == 0
	enemies = gets.split.map &:to_i
	m = gets.to_i
	single_spells = []
	all_spells = []
	(1..m).each{
		_, cost, type, damage = gets.chomp.split
		if type == 'Single'
			single_spells << [cost, damage].map(&:to_i)
		else
			all_spells << [cost, damage].map(&:to_i)
		end
	}
	
	single_cost = unbounded_knapsack(single_spells, enemies.max)
	all_cost = unbounded_knapsack(all_spells, enemies.max)
	
	min = Float::INFINITY
	(0..enemies.max).each{|limit|
		cost = 0
		enemies.each{|hp|
			if hp > limit
				cost += single_cost.find{|v, c| v >= hp - limit}[1]
			end
		}
		cost += all_cost.find{|v, c| v >= limit}[1]
		min = cost if cost < min
	}
	p min
}