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

for i in range(Q):
    B, C = map(str, input().split())
    A = list(map(lambda x: int(str(x).replace(B, C)), A))
    print(sum(A))

