read = lambda: list(map(int, input().split()))

n = int(input())
a = read()
q = int(input())
s = sum(a)
data = {}
for i in a:
    if i in data:
        data[i] += 1
    else:
        data[i] = 1

for _ in range(q):
    b, c = read()
    if b in data:
        s += (c - b) * data[b]
    else:
        data[b] = 0
    if c in data:
        data[c] += data[b]
        data[b] = 0
    else:
        data[c] = data[b]
    print(s)