import sys
import numpy as np
input = sys.stdin.readline


N = int(input())
A_list = input().split()
Q = int(input())
num_list = [list(input().split()) for _ in range(Q)]

A_cha = ",".join(A_list)
for i in num_list:
    B = i[0]
    C = i[1]
    A_cha = A_cha.replace(B, C)
    A_result = sum([int(A) for A in A_cha.split(",")])
    print(A_result)