n = int(input())
list = input().split()
for a in range(int(input())):
    b, c = input().split()
    list = map((lambda x: replace(b, c)), list)
    list_int = map((lambda x: int(x)), list)
    print(sum(list_int))