import bisect
import collections
import copy
import heapq
import itertools
import math
import numpy
import string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


N = I()
A = LS()
Q = I()
BC_ARR = [LS() for _ in range(Q)]

for s in range(Q):
    str1 = ",".join(A)
    A = str1.replace(BC_ARR[s][0], BC_ARR[s][1]).split(",")
    print(
        numpy.sum(list(map(int, A))))
