N = int(input())
A = list(map(str, input().split()))
A.sort()
num = int(input())
change_list = [list(map(str, input().split())) for _ in range(num)]

for change in change_list:
    A = [s.replace(change[0], change[1]) for s in A]
    print(sum(list(map(int, A))))
