N = int(input())
suretu = list(input().split())
Q = int(input())
ans = []
for i in range(Q):
    kei = 0
    x, y = input().split()
    l_replace = [s.replace(x, y) for s in suretu]
    for k in l_replace:
        kei += int(k)
    ans.append(kei)
for i in ans:
    print(i)