N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
s = 0
for q in range(Q):
    count = 0
    B,C = [int(x) for x in input().split()]
    for i in range(len(A)):
        if A[i]==B:
            A[i]=C
            count+=1
    if q>0:
        s = s+count*(C-B)
    else:
        s = sum(A)
    print(s)