if __name__=='__main__':
    N = int(input())
    A = input().split(" ")
    #A_i = [int(s) for s in A]
    Q=int(input())
    str_list=[]
    S=[]
    for i in range(Q):
        str_list.append(list(input().split()))

    for j in range(Q):
        if str_list[j][0] in A:
            A=[s.replace(str_list[j][0],str_list[j][1]) for s in A]

        A_i = [int(s) for s in A]
        print(sum(A_i))