N = int(input())
suretu = list(input().split())
Q = int(input())
ans = []
for i in range(Q):
    x, y = input().split()
    l_replace = [s.replace(x, y) for s in suretu]
    suretu = l_replace
    print(sum(map(int, l_replace)))