import sys
import numpy as np
import math  # gcdもあるよ
import collections
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n = int(sys.stdin.readline())
    a = list(map(int,sys.stdin.readline().split()))
    q = int(sys.stdin.readline())
    queue = [list(map(int,sys.stdin.readline().split())) for _ in range(q)]
    count = collections.Counter(a)
    print(count)
    print(list(count))
    for b, c in queue:
        if  b in count:
            if c in count:
                count[c] += count[b]
                del count[b]
            else:
                count[c] = count[b]
                del count[b]
        ans = 0
        for k in count:
            ans += k * count[k]
        print(ans) 
    
    
    return 0

if __name__ == "__main__":
    main()