# coding: utf-8
# submission # - User: herp_sy
# https://atcoder.jp/contests/
#
# lang: Python3 (3.8.2)

import math
import statistics
import numpy as np
import queue
import itertools
import functools 

n = int(input())
a = list(int(i) for i in input().split())
psum, ncnt = sum(a), {}

for k in range(min(a), max(a) + 1):
	ncnt[k] = a.count(k)

q = int(input())
for _ in range(q):
	b, c = map(int, input().split())
	if b in ncnt:
		psum += ((c - b) * ncnt[b])
		if c in ncnt:
		  ncnt[c] += ncnt[b]
		else:
		  ncnt[c] = ncnt[b]
		ncnt[b] = 0
	print(psum)
