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 As = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList().OrderBy(x=>x);

            var A = new Dictionary<int, int>();
            foreach (var a in As)
            {
                if(!A.ContainsKey(a))
                {
                    A.Add(a, 1);
                }
                else
                {
                    A[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();


                if (A.ContainsKey(BS[0]))
                {
                    var cntB = A[BS[0]];
                    A[BS[0]] = 0;
                    if (A.ContainsKey(BS[1]))
                    {
                        A[BS[1]] = cntB;
                    }
                    else
                    {
                        A.Add(BS[1], cntB);
                    }
                    
                }

                var sum = 0;
                foreach (var a in A)
                {
                    sum += a.Key * a.Value;
                }

                Console.WriteLine(sum);
            }
        }
    }
}