A = list(map(int, input().split()))
Q = int(input())
ans = []
for i in range(Q):
	b, c = list(map(int, input().split()))
	for j in range(N):
		if A[j] == b:
			A[j] = c

	ans += [sum(A)]
[print(a) for a in ans]