N = int(input())
A = list(map(str,input().split()))
A = ','.join(A)
Q = int(input())
lst = []
for _ in range(Q):
    a,b = map(int,input().split())
    lst.append([a,b])

#print(A)
'''
for i in range(Q):
    A = [lst[i][1] if j == lst[i][0] else j for j in A]
    print(sum(A))
'''
def digitSum(n):
    s = str(n)
    array = list(map(int, s))
    return sum(array)

for i in range(Q):
    ans = []
    A = A.replace(str(lst[i][0]),str(lst[i][1]))
    #A = int(A)
    #print(A)
    ans =A.split(',')
    ans = list(map(int, A.split(',')))
    #print(ans)
    print(sum(ans))
    #print(digitSum(A))
    A = str(A)
    #print(A)
