N = int(input())
A = map(int, input().split())
Q = int(input())

change_pair = []
for i in range(Q):
    b, c = map(int, input().split())
    change_pair.append((b, c))

# Aの出現値毎に回数をカウント
counts = {}
for a in A:
    if a in counts:
        counts[a] += 1
    else:
        counts[a] = 1

# 置き換え毎に回数を移動
for b, c in change_pair:
    if b in counts:
        count = counts[b]
        counts[b] = 0
    else:
        count = 0
    if c in counts:
        counts[c] += count
    else:
        counts[c] = count

    # 総和を求める
    total = 0
    for a, count in counts.items():
        total += a * count
    print(total)
