n = int(input())
a = list(input().split())
q = int(input())
for i in range(q):
    b, c = input().split()
    replace_a = [int(i.replace(b, c)) if int(i) == int(b) else int(i) for i in a]
    print(sum(replace_a))
    a = [str(j) for j in replace_a]
