using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
namespace Atcoder171
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
 
            var Anum = new int[100001];
            var sum = 0;
            var As = Console.ReadLine().Split(' ').Select(x => int.Parse(x));
 
            foreach (var a in As)
            {
                Anum[a]++;
                sum += a;
            }
          
            var Q = int.Parse(Console.ReadLine());
 
            for (var i = 0; i < Q; i++)
            {
 
                var BS = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
 
                sum += (Anum[BS[0]]  * BS[1] - Anum[BS[0]] * BS[0]);
                Console.WriteLine(sum);
              
                Anum[BS[1]] += Anum[BS[0]];
                Anum[BS[0]] = 0;
 
                
            }
        }
    }
}