N=int(input())
number=list(map(int,input().split()))
Q=int(input())
for i in range(Q):
    B,C=map(int,input().split())
    if B in number:
        for i in range(N):
            if number[i]==B:
                number[i]=C
    print(sum(number))