import numpy as np

def Replacing():
    N = int(input())
    A = np.array(list(map(int,input().split())))
    Q = int(input())
    for i in range(Q):
        b, c = map(int,input().split())
        A = np.where(A == b, c, A)
        print(A.sum())
        
Replacing()