N = int(input())
A = list(map(str, input().split()))
Q = int(input())

for i in range(Q):
    B, C = map(str, input().split())
    if B not in A:
        continue
    else:
        Ast = ",".join(A)
        Ast_new = Ast.replace(B, C)
        A = Ast_new.split(',')
        INT = list(map(int, A))                
        print(sum(INT))