import collections

N = int(input())

A = list(map(int,input().split()))

Q = int(input())

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

# print(N,A,Q,B,C)


a = []

a = dict(collections.Counter(A))

# print(a)


# d1 = {'k1': 1, 'k2': 2}
# d2 = {'k1': 100, 'k3': 3, 'k4': 4}

# d1.update(d2)
# print(d1)
# # {'k1': 100, 'k2': 2, 'k3': 3, 'k4': 4}

# これを用いれば良い。

for i in range(Q):
    # 変更後
    after = {B[i]: 0, C[i]: a.setdefault(B[i], 0) + a.setdefault(C[i], 0)}
    # print(a, after)
    a.update(after)

    s = 0
    for k in a:
        s += k * a[k]
    print(s)

# print(a)

