N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [list(map(int,input().split())) for i in range(Q)]

st = ""
res = 0

for i in A:
    i = str(i)
    st += i
for i in range(Q):
    a = str(B[i][0])
    b = str(B[i][1])
    st = st.replace(a, b)
    for i in range(N):
        res += int(st[i])
    print(res)
    res = 0

