n = int(input())
_a = list(map(int, input().split()))
a = {}
for i in range(n):
    if _a[i] in a:
        a[_a[i]] += 1
    else:
        a[_a[i]] = 1
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    if b in a:
        if c not in a:
            a[c] = 0
        tmp = a[b]
        a[c] += tmp
        a[b] = 0
        sum_ = 0
        for k in a:
            sum_ += k * a[k]
    print(sum_)