from sys import stdin


def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default
    
n = int(input())
print(n)

A =list(map(int, input().split()))
print(A)

base=sum(A)

Q=int(input())
for i in range(Q):
    b, c = map(int, input().split())
    change_num=A.count(b)
    for i in range(change_num):
        A[A.index(b)]=c
    base+=change_num*(c-b)
    print(base)