n = int(input())
A = list(map(int, input().split()))
D = {}
res = 0
for a in A:
    res += a
    if a in D.keys():
        D[a] += 1
    else:
        D[a] = 1

q = int(input())


for cq in range(q):
    b, c = map(int, input().split())
    if b in D.keys():
        res += (c - b) * D[b]
        if c in D.keys():
            D[c] += D[b]
        else:
            D[c] = D[b]
    print(res)
