n = int(input())
target = list(map(int, input().split()))
dct = dict()
for t in target:
    if t in dct:
        dct[t] += 1
    else:
        dct[t] = 1
lst = [list(map(int, input().split())) for i in range(int(input()))]
for l in lst:
    if l[0] in dct and not l[1] in dct:
        dct[l[1]] = dct[l[0]]
        dct[l[0]] = 0
    elif l[0] in dct:
        dct[l[1]] += dct[l[0]]
        dct[l[0]] = 0
    a = []
    for k, v in dct.items():
        a.append(k*v)
    print(sum(a))