#!/usr/bin/env python3
from collections import Counter
N = int(input()) 
A = Counter(map(int, input().split()))
Q = int(input())
for i in range(Q):
    sum = 0
    b, c = map(int, input().split())
    if A[b] != 0:
        A[c] += A[b]
        A[b] = 0
    for n in A:
        sum += A[n] * n
    print(sum)