We have seen the applications of union, intersection, difference and symmetric difference operations, but these operations do not make any changes or mutations to the set.  

We can use the following operations to create mutations to a set:

.update() or |= 

Update the set by adding elements from an iterable/another set.

>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.update(R)
>>> print H
set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])

.intersection_update() or &=

Update the set by keeping only the elements found in it and an iterable/another set.

>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.intersection_update(R)
>>> print H
set(['a', 'k'])

.difference_update() or -=

Update the set by removing elements found in an iterable/another set.

>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.difference_update(R)
>>> print H
set(['c', 'e', 'H', 'r'])

.symmetric_difference_update() or ^=

Update the set by only keeping the elements found in either set, but not in both.

>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.symmetric_difference_update(R)
>>> print H
set(['c', 'e', 'H', 'n', 'r', 'R'])

TASK

You are given a set $\mbox{A}$ and $N$ number of other sets. These $N$ number of sets have to perform some specific mutation operations on set $\mbox{A}$.

Your task is to execute those operations and print the sum of elements from set $\mbox{A}$.

Input Format

The first line contains the number of elements in set $\mbox{A}$.

The second line contains the space separated list of elements in set $\mbox{A}$.

The third line contains integer $N$, the number of other sets.

The next $2*N$ lines are divided into $N$ parts containing two lines each.

The first line of each part contains the space separated entries of the operation name and the length of the other set.

The second line of each part contains space separated list of elements in the other set.

${\mbox{o}}_{\lt}$ len(set(A)) $<1000$ 

${\mbox{o}}_{\lt}$ len(otherSets) $<100$ 

$0<N<100$

Output Format

Output the sum of elements in set $\mbox{A}$.

Sample Input
 16
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 24 52
 4
 intersection_update 10
 2 3 5 6 8 9 1 4 7 11
 update 2
 55 66
 symmetric_difference_update 5
 22 7 35 62 58
 difference_update 7
 11 22 35 55 58 62 66

Sample Output
38

Explanation

After the first operation, (intersection_update operation), we get:

set $A=set([1,2,3,4,5,6,7,8,9,11])$

After the second operation, (update operation), we get:

set $A=set([1,2,3,4,5,6,7,8,9,11,55,66])$

After the third operation, (symmetric_difference_update operation), we get:

set $A=set([1,2,3,4,5,6,8,9,11,22,35,55,58,62,66])$

After the fourth operation, ( difference_update operation), we get:

set $A=set([1,2,3,4,5,6,8,9])$

The sum of elements in set $\mbox{A}$ after these operations is $38$.

Source: hackerrank | Difficulty: EASY
Tags: Data structures


Solutions:
N = int(input())
A = set(map(int, input().split()))
for _ in range(int(input())):
	(a, b) = input().split()
	s = set(map(int, input().split()))
	eval('A.' + a + '(' + str(s) + ')')
print(sum(A))


n = int(input())
A = set(map(int, input().split()))
i = int(input())
for _ in range(i):
	(op, v) = input().split()
	B = set(map(int, input().split()))
	if op == 'intersection_update':
		A.intersection_update(B)
	if op == 'update':
		A.update(B)
	if op == 'symmetric_difference_update':
		A.symmetric_difference_update(B)
	if op == 'difference_update':
		A.difference_update(B)
print(sum(A))


acnt = int(input())
a = set(map(int, input().strip().split()))
numsets = int(input())
operationsetlist = [(input().strip(), set(map(int, input().strip().split()))) for _ in range(numsets)]
for (operation, s) in operationsetlist:
	(cmd, setlength) = operation.split()
	setlength = int(setlength)
	if cmd == 'update':
		a.update(s)
	elif cmd == 'intersection_update':
		a.intersection_update(s)
	elif cmd == 'symmetric_difference_update':
		a.symmetric_difference_update(s)
	elif cmd == 'difference_update':
		a.difference_update(s)
print(sum(a))


t = input()
s = set(map(int, input().split()))
for _ in range(int(input())):
	getattr(s, list(map(str, input().split()))[0])(set(map(int, input().split())))
print(sum(s))


def read_set():
	return set(map(int, input().split()))
_ = input()
s = read_set()
operations = {'intersection_update': s.intersection_update, 'update': s.update, 'symmetric_difference_update': s.symmetric_difference_update, 'difference_update': s.difference_update}
for _ in range(int(input())):
	(op, _) = input().split()
	operations[op](read_set())
print(sum(s))


n = int(input())
s1 = set(map(int, input().split()))
m = int(input())
for i in range(m):
	(comm, ns) = input().split()
	st = set(map(int, input().split()))
	if comm == 'update':
		s1.update(st)
	elif comm == 'difference_update':
		s1.difference_update(st)
	elif comm == 'intersection_update':
		s1.intersection_update(st)
	else:
		s1.symmetric_difference_update(st)
print(sum(s1))


input()
s = set(map(int, input().split()))
n = int(input())
for i in range(n):
	l = input().split()
	if l[0] == 'update':
		s |= set(map(int, input().split()))
	elif l[0] == 'intersection_update':
		s &= set(map(int, input().split()))
	elif l[0] == 'difference_update':
		s -= set(map(int, input().split()))
	elif l[0] == 'symmetric_difference_update':
		s ^= set(map(int, input().split()))
print(sum(list(s)))


input()
s = set(input().split())
n = int(input())
for i in range(n):
	x = input().split()
	y = set(input().split())
	if x[0] == 'update':
		s.update(y)
	elif x[0] == 'intersection_update':
		s.intersection_update(y)
	elif x[0] == 'difference_update':
		s.difference_update(y)
	elif x[0] == 'symmetric_difference_update':
		s.symmetric_difference_update(y)
d = 0
for i in s:
	d += int(i)
print(d)


def setTest(setA):
	(a, n) = input().split()
	setB = set(map(int, input().split()))
	if a == 'intersection_update':
		setA.intersection_update(setB)
	if a == 'update':
		setA.update(setB)
	if a == 'symmetric_difference_update':
		setA.symmetric_difference_update(setB)
	if a == 'difference_update':
		setA.difference_update(setB)
	return setA
n = input()
setA = set(map(int, input().split()))
for _ in range(int(input())):
	setTest(setA)
print(sum(setA))


n = int(input())
s = set(map(int, input().split()))
for _ in range(int(input())):
	(cmd, n) = input().split()
	sub = set(map(int, input().split()))
	exec('s.' + cmd + '(sub)')
print(sum(s))


n = int(input())
s = set(map(int, input().split()))
ins = int(input())
for _ in range(ins):
	command = input().split()
	if command[0] == 'intersection_update':
		s &= set(map(int, input().split()))
	if command[0] == 'update':
		s |= set(map(int, input().split()))
	if command[0] == 'difference_update':
		s -= set(map(int, input().split()))
	if command[0] == 'symmetric_difference_update':
		s ^= set(map(int, input().split()))
print(sum(s))


_ = input()
S = set(map(int, input().split()))
for _ in range(int(input())):
	(OP, _) = input().split()
	T = set(map(int, input().split()))
	if OP == 'intersection_update':
		S &= T
	if OP == 'update':
		S |= T
	if OP == 'symmetric_difference_update':
		S ^= T
	if OP == 'difference_update':
		S -= T
print(sum(S))


cmds = {'intersection_update': lambda x: a.intersection_update(x), 'update': lambda x: a.update(x), 'symmetric_difference_update': lambda x: a.symmetric_difference_update(x), 'difference_update': lambda x: a.difference_update(x)}
input()
a = set(map(int, input().split()))
n = int(input())
for i in range(n):
	cmd = input().split()[0]
	other = set(map(int, input().split()))
	cmds[cmd](other)
print(sum(a))


num = int(input())
data = set(map(int, input().split()))
operations = int(input())
for oper in range(operations):
	op = input().split()
	if op[0] == 'intersection_update':
		data.intersection_update(set(map(int, input().split())))
	elif op[0] == 'update':
		data.update(set(map(int, input().split())))
	elif op[0] == 'symmetric_difference_update':
		data.symmetric_difference_update(set(map(int, input().split())))
	else:
		data.difference_update(set(map(int, input().split())))
print(sum(data))


(_, s) = (input(), set(input().split()))
for _ in range(int(input())):
	tokens = input().split()
	getattr(s, tokens[0])(set(input().split()))
print(sum(map(int, s)))


_ = input()
x = set(map(int, input().split()))
i = int(input())
for a in range(i):
	(n, _) = input().split()
	other = set(map(int, input().split()))
	if n == 'intersection_update':
		x.intersection_update(other)
	elif n == 'update':
		x.update(other)
	elif n == 'difference_update':
		x.difference_update(other)
	elif n == 'symmetric_difference_update':
		x.symmetric_difference_update(other)
print(sum(x))


input()
s = set(map(int, input().split()))
for _ in range(int(input())):
	ins = input().split()
	tmp = set(map(int, input().split()))
	if ins[0] == 'intersection_update':
		s.intersection_update(tmp)
	elif ins[0] == 'update':
		s.update(tmp)
	elif ins[0] == 'symmetric_difference_update':
		s.symmetric_difference_update(tmp)
	elif ins[0] == 'difference_update':
		s.difference_update(tmp)
print(sum(s))


n = input()
a = set(map(int, input().split()))
n = int(input())
for x in range(n):
	tmp = input().split()[0]
	if tmp == 'update':
		a.update([int(y) for y in input().split()])
	elif tmp == 'symmetric_difference_update':
		a.symmetric_difference_update([int(y) for y in input().split()])
	elif tmp == 'difference_update':
		a.difference_update([int(y) for y in input().split()])
	elif tmp == 'intersection_update':
		a.intersection_update([int(y) for y in input().split()])
print(sum(a))


_ = input()
A = set(map(int, input().split()))
for i in range(int(input())):
	cmd = input().split()[0]
	other_set = set(map(int, input().split()))
	eval('A.' + cmd + '(other_set)')
print(sum(A))


n = int(input())
st = set(map(int, input().split()))
for i in range(0, int(input())):
	line = input().split()
	opt = line[0]
	num = line[1]
	otherSet = set(map(int, input().split()))
	if opt == 'update':
		st.update(otherSet)
	elif opt == 'intersection_update':
		st.intersection_update(otherSet)
	elif opt == 'difference_update':
		st.difference_update(otherSet)
	elif opt == 'symmetric_difference_update':
		st.symmetric_difference_update(otherSet)
sum = 0
for e in st:
	sum += e
print(sum)


size_a = int(input())
set_a = set(map(int, input().split()))
no_of_cmd = int(input())
for i in range(0, no_of_cmd):
	command = input().split()
	set_b = set(map(int, input().split()))
	if command[0] == 'intersection_update':
		set_a.intersection_update(set_b)
	elif command[0] == 'update':
		set_a.update(set_b)
	elif command[0] == 'symmetric_difference_update':
		set_a.symmetric_difference_update(set_b)
	elif command[0] == 'difference_update':
		set_a.difference_update(set_b)
print(sum(set_a))


n = input()
A = set(map(int, input().split()))
m = int(input())
for i in range(m):
	cmd = input().split()
	if cmd[0] == 'intersection_update':
		B = set(map(int, input().split()))
		A &= B
	elif cmd[0] == 'update':
		B = set(map(int, input().split()))
		A |= B
	elif cmd[0] == 'symmetric_difference_update':
		B = set(map(int, input().split()))
		A ^= B
	elif cmd[0] == 'difference_update':
		B = set(map(int, input().split()))
		A -= B
	else:
		print('unkown command')
print(sum(A))


_ = input()
a = set(map(int, input().split()))
n = int(input())
for _ in range(n):
	(comm, __) = input().split()
	b = set(map(int, input().split()))
	getattr(a, comm)(b)
print(sum(a))


n1 = int(input())
init = set(map(int, input().split()))
n = int(input())
for i in range(n):
	cmd = input().split()
	vals = set(map(int, input().split()))
	if cmd[0] == 'intersection_update':
		init.intersection_update(vals)
	elif cmd[0] == 'symmetric_difference_update':
		init.symmetric_difference_update(vals)
	elif cmd[0] == 'update':
		init.update(vals)
	else:
		init.difference_update(vals)
print(sum(init))


n = int(input())
s = set(map(int, input().split()))
N = int(input())
for _ in range(N):
	arr = input().split()
	name = arr[0]
	l = int(arr[1])
	s2 = set(map(int, input().split()))
	getattr(s, name)(s2)
print(sum(s))


n = input()
A = set(input().split())
for _ in range(int(input())):
	(command, n) = input().split()
	other = set(input().split())
	getattr(A, command)(other)
print(sum(map(int, A)))


n = input()
a = set(map(int, input().split()))
m = int(input())
for i in range(m):
	att = list(map(str, input().split()))[0]
	getattr(a, att)(map(int, input().split()))
print(sum(a))


n = int(input())
s = set(input().split())
m = int(input())
for i in range(m):
	(st, k) = input().split()
	p = set(input().split())
	if st == 'intersection_update':
		s.intersection_update(p)
	elif st == 'update':
		s.update(p)
	elif st == 'difference_update':
		s.difference_update(p)
	else:
		s.symmetric_difference_update(p)
l = list(s)
sum = 0
for i in l:
	sum = sum + int(i)
print(sum)


n = int(input())
s1 = set(map(int, input().split()))
for i in range(0, int(input())):
	args = input().strip().split(' ')
	s2 = set(map(int, input().split()))
	getattr(s1, args[0])(s2)
print(sum(list(s1)))


input()
a = set(map(int, input().split()))
n = int(input())
for _ in range(n):
	operation = input().split()[0]
	b = set(map(int, input().split()))
	eval('a.' + operation + '(b)')
print(sum(a))


n = int(input())
sets = set(input().strip().split())
m = int(input())
for _ in range(0, m):
	(cmd, num_items_str) = input().strip().split()
	cur_sets = set(input().strip().split())
	if cmd == 'intersection_update':
		sets &= cur_sets
	elif cmd == 'update':
		sets |= cur_sets
	elif cmd == 'symmetric_difference_update':
		sets ^= cur_sets
	else:
		sets -= cur_sets
print(sum(map(int, sets)))


i = int(input())
s = set(map(int, input().split()))
k = int(input())
for i in range(k):
	L = list(input().split())
	if L[0] == 'intersection_update':
		K = set(map(int, input().split()))
		s.intersection_update(K)
	if L[0] == 'difference_update':
		K = set(map(int, input().split()))
		s.difference_update(K)
	if L[0] == 'update':
		K = set(map(int, input().split()))
		s.update(K)
	if L[0] == 'symmetric_difference_update':
		K = set(map(int, input().split()))
		s.symmetric_difference_update(K)
print(sum(s))


input()
a = set(map(int, input().split()))
for i in range(int(input())):
	exec('a.{}(set(map(int,"{}".split())))'.format(input().split()[0], input().strip()))
print(sum(a))


n = int(input())
a = set(map(int, input().split()))
N = int(input())
for i in range(N):
	cmd = input().split()
	opt = cmd[0]
	s = set(map(int, input().split()))
	if opt == 'update':
		a |= s
	elif opt == 'intersection_update':
		a &= s
	elif opt == 'difference_update':
		a -= s
	elif opt == 'symmetric_difference_update':
		a ^= s
print(sum(a))


n = int(input())
a = set([int(i) for i in input().split()])
N = int(input())
for j in range(N):
	op = input().split()[0]
	b = set([int(i) for i in input().split()])
	if op == 'update':
		a |= b
	elif op == 'intersection_update':
		a &= b
	elif op == 'symmetric_difference_update':
		a ^= b
	else:
		a -= b
print(sum(a))


def read_int():
	return int(input())

def read_int_words():
	return map(int, input().split())
_ = input()
aS = set(read_int_words())
n = read_int()
for i in range(n):
	(c, _) = input().split()
	s = set(read_int_words())
	if c == 'intersection_update':
		aS &= s
	elif c == 'symmetric_difference_update':
		aS ^= s
	elif c == 'update':
		aS |= s
	elif c == 'difference_update':
		aS -= s
	else:
		pass
print(sum(aS))


nb = int(input())
A = set(map(int, input().split()))
nb = int(input())
for i in range(nb):
	(op, cnt) = input().split()
	B = set(map(int, input().split()))
	if 'update' == op:
		A.update(B)
	elif 'symmetric_difference_update' == op:
		A.symmetric_difference_update(B)
	elif 'intersection_update' == op:
		A.intersection_update(B)
	else:
		A.difference_update(B)
print(sum(A))


a = int(input())
k = {int(i) for i in input().split()}
n = int(input())
for i in range(n):
	tmp = input().split()
	tmp2 = {int(i) for i in input().split()}
	if tmp[0] == 'update':
		k.update(tmp2)
	elif tmp[0] == 'intersection_update':
		k.intersection_update(tmp2)
	elif tmp[0] == 'symmetric_difference_update':
		k.symmetric_difference_update(tmp2)
	elif tmp[0] == 'difference_update':
		k.difference_update(tmp2)
print(sum(k))


n = int(input())
a = set(map(int, input().split()[:n]))
n_sets = int(input())
for i in range(n_sets):
	(c, m) = input().split()
	m = int(m)
	b = set(map(int, input().split()[:m]))
	if c == 'difference_update':
		a -= b
	elif c == 'intersection_update':
		a &= b
	elif c == 'symmetric_difference_update':
		a ^= b
	elif c == 'update':
		a |= b
print(sum(a))


n = int(input())
s = set(map(int, input().split()))
t = int(input())
for item in range(t):
	op = list(input().split())
	temp = set(map(int, input().split()))
	if op[0] == 'update':
		s.update(temp)
	elif op[0] == 'intersection_update':
		s.intersection_update(temp)
	elif op[0] == 'symmetric_difference_update':
		s.symmetric_difference_update(temp)
	elif op[0] == 'difference_update':
		s.difference_update(temp)
print(sum(s))


n = int(input())
A = set(map(int, input().split()))
N = int(input())
for i in range(N):
	(op, ln) = input().split()
	B = set(map(int, input().split()))
	if op == 'update':
		A.update(B)
	elif op == 'intersection_update':
		A.intersection_update(B)
	elif op == 'difference_update':
		A.difference_update(B)
	elif op == 'symmetric_difference_update':
		A.symmetric_difference_update(B)
print(sum(A))


an = int(input())
a = set(map(int, input().split()))
n = int(input())
for i in range(n):
	com = input().split()
	b = set(map(int, input().split()))
	if com[0] == 'update':
		a.update(b)
	elif com[0] == 'intersection_update':
		a.intersection_update(b)
	elif com[0] == 'difference_update':
		a.difference_update(b)
	else:
		a.symmetric_difference_update(b)
print(sum(a))


(e, s, c) = (int(input()), set(map(int, input().split())), int(input()))
for i in range(c):
	(command, l) = input().split()
	cmd = getattr(s, command)
	A = set(map(int, input().split()))
	cmd(A)
print(sum(s))


input()
s = set(map(int, input().split()))
for _ in range(int(input())):
	cmd = input().split()[0]
	t = set(map(int, input().split()))
	exec('s.%s(t)' % cmd)
print(sum(s))


nA = int(input())
A = set(map(int, input().split()))
N = int(input())
for i in range(N):
	command = input().split()
	B = set(map(int, input().split()))
	if command[0] == 'update':
		A |= B
	elif command[0] == 'intersection_update':
		A &= B
	elif command[0] == 'difference_update':
		A -= B
	else:
		A ^= B
print(sum(A))


a_num = input()
a_set = set(input().split())
t = int(input())
while t > 0:
	t -= 1
	op = input()
	if 'intersection_update' in op:
		a_set &= set(input().strip().split())
	elif 'symmetric_difference_update' in op:
		a_set ^= set(input().strip().split())
	elif 'difference_update' in op:
		a_set -= set(input().strip().split())
	elif 'update':
		a_set |= set(input().strip().split())
	else:
		continue
print(sum([int(i) for i in a_set]))


nA = int(input())
A = set(map(int, input().split()))
N = int(input())
for i in range(N):
	c = list(input().split())
	l = list(map(int, input().split()))
	if c[0] == 'intersection_update':
		A.intersection_update(l)
	elif c[0] == 'update':
		A.update(l)
	elif c[0] == 'symmetric_difference_update':
		A.symmetric_difference_update(l)
	elif c[0] == 'difference_update':
		A.difference_update(l)
print(sum(A))


input()
s = set(input().split())
I = [[input().split()[0], set(input().split())] for _ in range(int(input()))]
for x in I:
	y = x[1]
	eval('s.' + x[0] + '(y)')
print(sum({int(x) for x in s}))


input()
A = set(map(int, input().split()))
for i in range(int(input())):
	cmd = input().split()
	B = set(map(int, input().split()))
	if cmd[0] == 'update':
		A |= B
	elif cmd[0] == 'intersection_update':
		A &= B
	elif cmd[0] == 'difference_update':
		A -= B
	elif cmd[0] == 'symmetric_difference_update':
		A ^= B
print(sum(A))


__author__ = 'namii'
input()
A = set(input().split())
N = int(input())
for _ in range(N):
	command = input().split()[0]
	new_set = set(input().split())
	if command == 'intersection_update':
		A.intersection_update(new_set)
	elif command == 'symmetric_difference_update':
		A.symmetric_difference_update(new_set)
	elif command == 'difference_update':
		A.difference_update(new_set)
	elif command == 'update':
		A.update(new_set)
print(sum(map(int, set(A))))


N1 = int(input())
A = set(input().split())
N2 = int(input())
for i in range(N2):
	c = input().split()
	s = set(input().split())
	if c[0] == 'intersection_update':
		A.intersection_update(s)
	elif c[0] == 'update':
		A.update(s)
	elif c[0] == 'symmetric_difference_update':
		A.symmetric_difference_update(s)
	elif c[0] == 'difference_update':
		A.difference_update(s)
print(sum(list(map(int, A))))


input()
st = set(input().split())
cases = int(input())
for i in range(cases):
	inp = input().split()
	if inp[0] == 'intersection_update':
		temp = set(input().split())
		st.intersection_update(temp)
	elif inp[0] == 'update':
		temp = set(input().split())
		st.update(temp)
	elif inp[0] == 'symmetric_difference_update':
		temp = set(input().split())
		st.symmetric_difference_update(temp)
	elif inp[0] == 'difference_update':
		temp = set(input().split())
		st.difference_update(temp)
output = set([int(i) for i in st])
print(sum(output))


n = int(input())
s = set(map(int, input().split()))
m = int(input())
for i in range(m):
	getattr(s, input().split()[0])(set(map(int, input().split())))
print(sum(s))


n = int(input())
a = set(map(int, input().split()))
num_tasks = int(input())
for i in range(num_tasks):
	(task, num) = input().split()
	num = int(num)
	b = set(map(int, input().split()))
	getattr(a, task)(b)
print(sum(a))


noofel = int(input())
setel = set(input().split())
noofop = int(input())
for _ in range(noofop):
	(opern, setsize) = input().split()
	newset = set(input().split())
	if opern == 'intersection_update':
		setel &= newset
	elif opern == 'update':
		setel |= newset
	elif opern == 'difference_update':
		setel -= newset
	elif opern == 'symmetric_difference_update':
		setel ^= newset

def sum(s):
	su = 0
	for i in s:
		su += int(i)
	return su
print(sum(setel))


m = int(input())
a = set(map(int, input().split()))
com_no = int(input())
for i in range(com_no):
	com = input().split()
	other = set(map(int, input().split()))
	if com[0] == 'intersection_update':
		a &= other
	if com[0] == 'update':
		a |= other
	if com[0] == 'difference_update':
		a -= other
	if com[0] == 'symmetric_difference_update':
		a ^= other
print(sum(a))


n = int(input())
a = set(map(int, input().split()))
N = int(input())
for _ in range(N):
	(op, M) = input().split()
	b = set(map(int, input().split()))
	if op == 'update':
		a |= b
	elif op == 'intersection_update':
		a &= b
	elif op == 'difference_update':
		a -= b
	elif op == 'symmetric_difference_update':
		a ^= b
	else:
		print('wtf')
print(sum(a))


nA = int(input())
a = set(map(int, input().split()))
n = int(input())
for _ in range(n):
	nb = input().split()
	b = list(map(int, input().split()))
	b = set(b)
	if nb[0] == 'intersection_update':
		a.intersection_update(b)
	elif nb[0] == 'update':
		a.update(b)
	elif nb[0] == 'symmetric_difference_update':
		a.symmetric_difference_update(b)
	elif nb[0] == 'difference_update':
		a.difference_update(b)
print(sum(a))


n_a = int(input())
A = set(map(int, input().split()))
N = int(input())
for i in range(N):
	command = input().split()
	if command[0] == 'update':
		A.update(set(map(int, input().split())))
	elif command[0] == 'intersection_update':
		A.intersection_update(set(map(int, input().split())))
	elif command[0] == 'difference_update':
		A.difference_update(set(map(int, input().split())))
	elif command[0] == 'symmetric_difference_update':
		A.symmetric_difference_update(set(map(int, input().split())))
print(sum(A))


n = int(input())
s = set(map(int, input().split()))
for i in range(0, int(input())):
	read = input().split()
	try:
		getattr(s, read[0])(set(map(int, input().split())))
	except:
		continue
print('{0}'.format(sum(s)))


n = int(input())
s1 = set(map(int, input().split()))
m = int(input())
l = []
for _ in range(m):
	a = list(input().split())
	sq = set(map(int, input().split()))
	a.append(sq)
	l.append(a)
for i in l:
	if i[0] == 'intersection_update':
		i[1] = int(i[1])
		s1.intersection_update(i[2])
	if i[0] == 'update':
		i[1] = int(i[1])
		s1.update(i[2])
	if i[0] == 'symmetric_difference_update':
		s1.symmetric_difference_update(i[2])
	if i[0] == 'difference_update':
		s1.difference_update(i[2])
t = map(int, s1)
print(sum(t))


input()
a = set([int(x) for x in input().split()])
T = int(input())
for _ in range(T):
	(c, m) = input().split()
	b = set([int(x) for x in input().split()])
	if c[0] == 'i':
		a &= b
	elif c[0] == 'u':
		a |= b
	elif c[0] == 'd':
		a -= b
	else:
		a ^= b
print(sum(a))


m = int(input())
a = set(map(int, input().strip().split()))
c = int(input())
for i in range(c):
	command = input().strip().split()
	s = set(map(int, input().strip().split()))
	if command[0].startswith('i'):
		a.intersection_update(s)
	if command[0].startswith('u'):
		a.update(s)
	if command[0].startswith('s'):
		a.symmetric_difference_update(s)
	if command[0].startswith('d'):
		a.difference_update(s)
print(sum(a))


numA = int(input())
A = set(map(int, input().split()))
numtests = int(input())
for t in range(numtests):
	ops = input().split()
	nset = set(map(int, input().split()))
	if ops[0] == 'intersection_update':
		A.intersection_update(nset)
	if ops[0] == 'update':
		A.update(nset)
	if ops[0] == 'symmetric_difference_update':
		A.symmetric_difference_update(nset)
	if ops[0] == 'difference_update':
		A.difference_update(nset)
print(sum(A))


n = int(input())
a = set(map(int, input().split()))
N = int(input())
for i in range(N):
	op = input().split()
	temp = set(map(int, input().split()))
	if op[0] == 'intersection_update':
		a &= temp
	elif op[0] == 'update':
		a |= temp
	elif op[0] == 'symmetric_difference_update':
		a ^= temp
	elif op[0] == 'difference_update':
		a -= temp
print(sum(a))


input()
A = set((int(i) for i in input().split()))
N = int(input())
for _ in range(N):
	a = input().split()
	b = set((int(i) for i in input().split()))
	if a[0][0] == 'i':
		A &= b
	elif a[0][0] == 's':
		A ^= b
	elif a[0][0] == 'd':
		A -= b
	elif a[0][0] == 'u':
		A |= b
print(sum(A))


n = input()
S = set(map(int, input().split()))
for _ in range(int(input())):
	a = input().split()
	b = set(map(int, input().split()))
	if a[0] == 'update':
		S.update(b)
	elif a[0] == 'difference_update':
		S.difference_update(b)
	elif a[0] == 'intersection_update':
		S.intersection_update(b)
	else:
		S.symmetric_difference_update(b)
print(sum(S))


m = int(input())
a = set(map(int, input().split()))
n = int(input())
for _ in range(n):
	op = input().split()[0]
	new = set(map(int, input().split()))
	if op == 'update':
		a |= new
	elif op == 'intersection_update':
		a &= new
	elif op == 'difference_update':
		a -= new
	elif op == 'symmetric_difference_update':
		a ^= new
print(sum(a))


input()
a_set = set([int(x) for x in input().split()])
n = int(input())
for _ in range(n):
	command = input()
	other_set = set([int(x) for x in input().split()])
	if 'intersection_update' in command:
		a_set.intersection_update(other_set)
	elif 'symmetric_difference_update' in command:
		a_set.symmetric_difference_update(other_set)
	elif 'difference_update' in command:
		a_set.difference_update(other_set)
	elif 'update' in command:
		a_set.update(other_set)
print(sum(a_set))


a = input()
b = set(map(int, input().split()))
c = int(input())
for i in range(c):
	(d, e) = input().split()
	f = set(map(int, input().split()))
	getattr(b, d)(f)
sum = 0
for i in b:
	sum += i
print(sum)


nn = int(input())
s = set(list(map(int, input().split())))
for i in range(int(input())):
	ss1 = input().split()
	ss2 = set(list(map(int, input().split())))
	if ss1[0] == 'intersection_update':
		s.intersection_update(ss2)
	elif ss1[0] == 'update':
		s.update(ss2)
	elif ss1[0] == 'symmetric_difference_update':
		s.symmetric_difference_update(ss2)
	elif ss1[0] == 'difference_update':
		s.difference_update(ss2)
print(sum(s))


L = []
input()
a = set([int(x) for x in input().split()])
for ___ in range(int(input())):
	(op, __) = input().split()
	b = set([int(x) for x in input().split()])
	op = getattr(a, op)
	op(b)
print(sum(a))


input()
S = set(map(int, input().split()))
N = int(input())
for i in range(0, N):
	(op, n) = input().split()
	A = set(map(int, input().split()))
	if op == 'intersection_update':
		S &= A
	elif op == 'update':
		S |= A
	elif op == 'symmetric_difference_update':
		S ^= A
	else:
		S -= A
print(sum(S))


ints = lambda s: set(map(int, s.split()))
_ = int(input())
a = ints(input())
n = int(input())
for i in range(n):
	(cmd, _) = input().split()
	s = ints(input())
	if cmd == 'update':
		a |= s
	elif cmd == 'intersection_update':
		a &= s
	elif cmd == 'difference_update':
		a -= s
	elif cmd == 'symmetric_difference_update':
		a ^= s
print(sum(a))


input()
a = set(map(int, input().split()))
for i in range(int(input())):
	b = input().split()
	z = set(map(int, input().split()))
	eval('a.' + b[0] + '(z)')
print(sum(a))


int(input())
a = set((int(x) for x in input().split()))
n = int(input())
for _ in range(n):
	c = input().split()[0]
	s = set((int(x) for x in input().split()))
	if c == 'update':
		a |= s
	elif c == 'difference_update':
		a -= s
	elif c == 'intersection_update':
		a &= s
	else:
		a ^= s
print(sum(a))


num_A = int(input())
A = set(map(int, input().split()))
N = int(input())
for i in range(N):
	task = input().split()
	X = set(map(int, input().split()))
	if task[0] == 'intersection_update':
		A.intersection_update(X)
	if task[0] == 'update':
		A.update(X)
	if task[0] == 'symmetric_difference_update':
		A.symmetric_difference_update(X)
	if task[0] == 'difference_update':
		A.difference_update(X)
print(sum(A))


def __starting_point():
	n = int(input())
	s = set(map(int, input().split()))
	num_cmds = int(input())
	for _i in range(num_cmds):
		cmd = list(input().strip().split(' '))
		if cmd[0] == 'intersection_update':
			get_set = set(map(int, input().split(' ')))
			s &= get_set
		elif cmd[0] == 'update':
			get_set = set(map(int, input().split(' ')))
			s |= get_set
		elif cmd[0] == 'difference_update':
			get_set = set(map(int, input().split(' ')))
			s -= get_set
		elif cmd[0] == 'symmetric_difference_update':
			get_set = set(map(int, input().split(' ')))
			s ^= get_set
	print(sum(s))
__starting_point()


input()
s = set([int(x) for x in input().split()])
for i in range(int(input())):
	lis = [x for x in input().split()]
	nums = set([int(x) for x in input().split()])
	if lis[0] == 'update':
		s |= nums
	elif lis[0] == 'intersection_update':
		s &= nums
	elif lis[0] == 'symmetric_difference_update':
		s ^= nums
	elif lis[0] == 'difference_update':
		s -= nums
print(sum(s))


setlen = int(input())
setnums = set(map(int, input().split()[:setlen]))
for i in range(int(input())):
	(command, newsetlen) = input().split()
	newset = set(map(int, input().split()[:int(newsetlen)]))
	getattr(setnums, command)(newset)
print(sum(setnums))


input()
A = set(map(int, input().strip().split()))
for _ in range(int(input())):
	(cmd, _) = input().strip().split()
	S = set(map(int, input().strip().split()))
	cmd = 'A.' + cmd + '(S)'
	eval(cmd)
print(sum(A))


input()
A = set(map(int, input().split()))
for _ in range(int(input())):
	(operation_name, l) = input().split()
	B = set(map(int, input().split()))
	if operation_name == 'intersection_update':
		A &= B
	elif operation_name == 'update':
		A |= B
	elif operation_name == 'symmetric_difference_update':
		A ^= B
	elif operation_name == 'difference_update':
		A -= B
print(sum(A))


input()
A = set(map(int, input().split()))
N = int(input())
for _ in range(N):
	(operation, n) = input().split()
	B = set(map(int, input().split()))
	if operation == 'intersection_update':
		A &= B
	elif operation == 'update':
		A |= B
	elif operation == 'symmetric_difference_update':
		A ^= B
	elif operation == 'difference_update':
		A -= B
total = 0
for ele in A:
	total += ele
print(total)


N = input()
S = set(map(int, input().split()))
for _ in range(int(input())):
	eval('S.' + input().split()[0] + '([' + ','.join(input().split()) + '])')
print(sum(S))