import collections

N = int(input())
a = list(map(int,input().split()))
Q = int(input())

c = collections.Counter(a)

for i in range(Q):
    arr = list(map(int,input().split()))
    B = arr[0]
    C = arr[1]

    if(C not in c):
        c[C] = 0

    c[C] += c[B]
    del c[B]

    sum = 0
    for key, cnt in c.items():
        sum += key*cnt

    print(sum)