using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace D
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            int N = int.Parse(line);
            line = Console.ReadLine();
            string[] strs = line.Split(' ');
            var dict = new Dictionary<int, int>();
            for(int i = 0; i < N; i++)
            {
                var tmp = int.Parse(strs[i]);
                if (!dict.ContainsKey(tmp))
                {
                    dict.Add(tmp, 1);
                }
                else dict[tmp]++;
            }
            line = Console.ReadLine();
            int Q = int.Parse(line);
            for (int i = 0; i < Q; i++)
            {
                line = Console.ReadLine();
                strs = line.Split(' ');
                var tmp1 = int.Parse(strs[0]);
                var tmp2 = int.Parse(strs[1]);
                if (dict.ContainsKey(tmp1))
                {
                    if (dict.ContainsKey(tmp2))
                    {
                        dict[tmp2] += dict[tmp1];
                    }
                    else
                    {
                        dict.Add(tmp2, dict[tmp1]);
                    }
                    dict.Remove(tmp1);
                }
                long result = 0;
                foreach (var kvp in dict)
                {
                    result += kvp.Key * (long)kvp.Value;
                }
                Console.WriteLine(result);
            }
        }
    }
}
