N = int(input()) 
A = list(map(int, input().split()))
Q = int(input()) 
for i in range(Q):
    b, c = map(str, input().split())
    if int(b) in A:
        A = [int(str(a).replace(b, c)) for a in A]
    print(sum(A))