N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())

aDict = dict()
for aElement in A:
    aDict.setdefault(aElement, 0)
    aDict[aElement] += 1

for _ in range(Q):
    b, c = map(int, input().split())
    aDict.setdefault(c, 0)
    aDict[c] += aDict.pop(b, 0)
    print(sum([k * v for k, v in aDict.items()]))
