n = int(input())
a = list(map(int,input().split()))
q = int(input())
d = dict()
for i in a :
    d[i] = d.get(i,0) + 1
s = sum(a)
for i in range(q) :
    b,c = map(int,input().split())
    if b in d.keys() :
        s += (c*d[b] - b*d[b])
        if c in d.keys() :
            d[c] += 1
        else :
            d[c] = 1
        del d[b]
    print(s)