N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q

for i in range(Q):
    B[i], C[i] = map(str, input().split())

for i in range(Q):
    A = list(map(str, A))
    if B[i] in A:
        A = [a.replace(B[i], C[i]) for a in A]
    
    A = list(map(int, A))
    print(sum(A))