import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
from numba import njit
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()
# A = np.array(_A,dtype='int')
Q = I()
BC = [LI() for _ in range(Q)]
# print(BC)

def convert(n):
    return d[n]

d = {1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9}
for i in range(Q):
    for key, value in d.items():
        if BC[i][0] == value:
            d[key]=BC[i][1]
    ans = np.array(list(map(convert,A)),dtype='int')
    print(ans.sum())


# ans = list(map(convert,A))
# for item in ans:
#     print(item)
# print('\n'.join(str(ans)))

#A,B = zip(*AB)
#Ap = np.array(A)
#C = np.zeros(N + 1)

# if ans:
#     print('Yes')
# else:
#     print('No')