#!/usr/bin/env python3
import sys
import collections

N = int(input())
A = list(map(int, input().split()))
Q = int(input())

d = collections.Counter(A)

for i in range(Q):
    B,C = list(map(int, input().split()))
    if B in d:
        val = d[B]
        if C in d:
            d[C] = d[C] + d[B]
        else:
            d[C] = d[B]
        d.pop(B)

    ans = 0
    for k, v in d.items():
        ans += k * v
    
    print(ans)