import collections as cl
#=input()
n=int(input())
#=map(int,input().split())
a=list(map(int,input().split()))
q=int(input())
bc=[list(map(int,input().split())) for _ in range(q)]

d = cl.Counter(a)
v, c = zip(*d.most_common())
v = list(v)
c = list(c)
sum = 0
for m in range(n):
    sum += a[m]
for i in range(q):
    w = bc[i][0]
    e = bc[i][1]
    if w in v:
        sum += (e-w) * c[v.index(w)]
        if e in v:
            c[v.index(e)] += c[v.index(w)]
            c[v.index(w)] = 0
        else:
            v[v.index(w)] = e
    print(sum)