import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(input())
    a = list(map(int, input().split()))
    l = [0]*10**5
    for i in a:
        l[i-1] += i
    q = int(input())
    for i in range(q):
        b,c = map(int, input().split())
        l[c-1] += l[b-1]//b*c
        l[b-1] = 0
        print(sum(l))



if __name__=="__main__":
    main()
