using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;class Program
{
	static void Main(string[] args)
	{
        //var exStdIn = new System.IO.StreamReader( "stdin.txt" );System.Console.SetIn( exStdIn );
		
		int n = int.Parse(Console.ReadLine());
		var a = Console.ReadLine().Split().Select(int.Parse).ToArray();
		long sm = 0;
		var cnt = new int[a.Max()+1];
		for (int i = 0; i < n; i++)
		{
			sm+=a[i];
			cnt[a[i]]++;
		}
		int q = int.Parse(Console.ReadLine());
		for (int i = 0; i < q; i++)
		{
			var bc = Console.ReadLine().Split().Select(int.Parse).ToArray();
			int b = bc[0];
			int c = bc[1];
			sm+=(c-b)*cnt[b];
			cnt[c] += cnt[b];
			cnt[b] = 0;
			Console.WriteLine(sm);
		}
	}
}
