a = int(input())
sequence = list(map(str, input().split(" ")))
q = int(input())
dictionary = {}
for i in range(q):
    pair = list(map(int, input().split(" ")))
    dictionary[pair[0]] = pair[1]

for key in dictionary:
    Sum = 0
    sequence = "".join((sequence))

    sequence = sequence.replace(str(key),str(dictionary[key]))

    sequence = list(sequence)

    for i in range(len(sequence)):
        Sum += int(sequence[i])
    print(Sum)
