N = int(input())
A = input().split()
Q = int(input())
b_c = [input().split() for i in range(Q)]

for i in range(Q):
    sum = 0
    A = [j.replace(b_c[i][0], b_c[i][1]) for j in A]
    for j in range(len(A)):
        sum += int(A[j])
    print(sum)