import sys
NList = int(input())
NumList = list(map(int,sys.stdin.readline().rstrip().split()))

AltNum = int(input())
resultList = []
for i in range(AltNum):
    a,b = map(int,sys.stdin.readline().rstrip().split())
    for j in range(len(NumList)):
        if(NumList[j] == a):
            NumList[j] = b
    print(sum(NumList))