n = int(input())
a = list(map(int, input().split()))
q = int(input())

m = {}
for i in a:
    if i in m.keys():
        m[i] = m[i] + 1
    else:
        m[i] = 1

for i in range(q):
    b, c = map(int,input().split())
    m[c] = m[c] + m[b]
    m.pop(b)
    ans = 0
    for k in m:
        ans += m[k] * k
    print(ans)
