N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    cnt = 0
    for j in range(len(numbers)):
        if numbers[j] == B : 
            cnt += 1
            numbers[j] = C
    sums += cnt * (C - B)
    print(sums)
