n = int(input())
r = list(map(int,input().split(" ")))
l = []
ar = {}
s = sum(r)
for a in r:
    if a not in l:
        ar[a] = 1
        l.append(a)
    else:
        ar[a] += 1
q = int(input())
for i in range(q):
    x,y = map(int,input().split(" "))
    if x in l:
        s += (y - x) * ar[x]
        if y in l:
            ar[y] += ar[x]
        else:
            ar[y] = ar[x]
            l.append(y)
        ar[x] = 0
    print(s)