N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = 0
dic = {}

for i in A:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

for _ in range(Q):
    B, C = map(int, input().split())
    if B in dic.keys():
        tmp = dic[B]
        dic[B] = 0
        if C in dic.keys():
            dic[C] += tmp
        else:
            dic[C] = tmp
    else:
        continue

    for val, num in dic.items():
        ans += val * num

    print(ans)
    ans = 0

