class Set
	def initialize(a, dict)
		@dict = dict
		@bits = 0
		a.each do |x|
			@bits |= (1 << @dict[x])
		end
	end

	def !@
		s = dup
		s.bits = (@bits ^ ((1 << @dict.size) - 1))
		s
	end

	def |(other)
		s = dup
		s.bits = (@bits | other.bits)
		s
	end

	def &(other)
		s = dup
		s.bits = (@bits & other.bits)
		s
	end

	def ^(other)
		s = dup
		s.bits = (@bits ^ other.bits)
		s
	end

	def -(other)
		s = dup
		s.bits -= (@bits & other.bits)
		s
	end

	def to_a
		(0..@dict.size-1).map{|i| [@dict.invert[i]] * @bits[i]}.flatten
	end

	def to_s
		to_a.to_s
	end

	attr_accessor :bits
end

loop do
	set = {}
	elem = {}
	a = b = c = d = e = nil
	loop do
		name, _ = gets.chomp.split rescue exit
		break if name == ?R
		a = gets.split.map &:to_i
		a.each{|x| elem[x] ||= elem.size}
		set[name.downcase] = a
	end
	set.each do |name, arr|
		eval "#{name} = Set.new(arr, elem)"	
	end
	expr = gets.chomp.tr("ABCDEuidsc", 'abcde|&\-^!')
	puts eval(expr).to_a.sort * ' '
end