def main():

    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())
    bc =[list(map(int, input().split())) for _ in range(q)]
    temp = [0] * (10**5 + 1)
    
    for i in a:
        temp[i] += i
    
    for i in range(q):
        b,c = bc[i][0],bc[i][1]
        temp[c] += (temp[b]//b) * c
        temp[b] = 0
        print(sum(temp))
 
if __name__ == "__main__":
    main()