n = int(input())

a = input()
#print(a)
q = int(input())

b = []
for _ in range(q):
    b.append(list(input().split(" ")))
#print(b)

d = a.split(' ')
l = len(d)
for i in range(q):
    c = a.replace(b[i][0],b[i][1],100000)
    a = c
    d = a.split(' ')
    s = 0
    for j in range(l):
        s += int(d[j]) 
    print(s)