n=int(input())
A=input()
A=A.replace(' ','')
q=int(input())
su=[]
for i in range(q):
    s=0
    b,c=map(str,input().split())
    A=A.replace(b,c)
    for i in range(len(A)):
        s+=int(A[i])
    su.append(s)
for i in su:
    print(i)