n = int(input())
a = list(input().split())
# aa = str("".join(a))
q = int(input())
bc = []
for i in range(q):
    bc.append(list(map(int,input().split())))

for i in range(q):
    mae = str(bc[i][0])
    ato = str(bc[i][1])
    for i,k in enumerate(a):
        if mae == k:    
            a[i] = a[i].replace(mae,ato)
    s = 0
    for j in range(len(a)):
        s += int(a[j])
    print(s)