import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)
N = ii()
A = [el for el in mips()]
Q = ii()
check = dict()
for i in range(1,max(A)+1):
    check[i] = 0
for el in A:
    check[el] += 1
for i in range(Q):
    ans = sum(A)
    B,C = mips()
    if B in check:
        ans -= check[B] * B
        if C in check:
            check[C] += check[B]
        else:
            check[C] = check[B]
        check[B] = 0
        ans += check[C] * C
    print(ans)