N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
B = list()
C = list()
for _ in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

aDict = dict()
for aElement in A:
    if aElement in aDict:
        aDict[aElement] += 1
    else:
        aDict[aElement] = 1

for b, c in zip(B, C):
    if b in aDict:
        if c in aDict:
            aDict[c] += aDict.pop(b)
        else:
            aDict[c] = aDict.pop(b)
    print(sum([k * v for k, v in aDict.items()]))
