using System;
using System.Collections.Generic;

public class Program
{
	public static Int64 sum(Dictionary<String, Int64> nums)
	{
		Int64 sum = 0;
		foreach (KeyValuePair<String, Int64> item in nums)
		{
			sum += item.Value * Convert.ToInt64(item.Key);
		}

		return sum;
	}

	public static void Main(String[] args)
	{
		var numbers = new Dictionary<String, Int64>();
		Int64 numberOfElements = Convert.ToInt64(Console.ReadLine());
		foreach (String x in Console.ReadLine().Split(' '))
		{
			if (numbers.ContainsKey(x))
			{
				numbers[x] += 1;
			}
			else
			{
				numbers.Add(x, 1);
			}
		}

		Int64 numOfOperations = Convert.ToInt64(Console.ReadLine());
		for (Int64 i = 0; i < numOfOperations; ++i)
		{
			String[] x = Console.ReadLine().Split(' ');
			if (numbers.ContainsKey(x[0]))
			{
				Console.WriteLine((Convert.ToInt64(x[1]) - Convert.ToInt64(x[0])) * numbers[x[0]] + sum(numbers));
				if (numbers.ContainsKey(x[1]))
					numbers[x[1]] += numbers[x[0]];
				else
					numbers[x[1]] = numbers[x[0]];
				numbers[x[0]] = 0;
			}
			else
				Console.WriteLine(sum(numbers));
		}
	}
}