#create date: 2020-06-29 09:42

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    alist = [0] * (10**5 + 1)
    for ai in a:
        alist[ai] += 1
    q = ni()
    for i in range(q):
        b, c = na()
        alist[c] += alist[b]
        alist[b] = 0
        ans = 0
        
        for i, num in enumerate(alist):
            ans += i*num
        print(ans)

if __name__ == "__main__":
    main()