using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace _171_d
{
    internal class Replacing
    {
        private static void Main(string[] args)
        {
            var a = new Dictionary<int, long>();
            var n = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());

            // 事前に空の辞書を作成する
            for (var i = 1; i <= 100000; i++)
            {
                a.Add(i, 0);
            }

            var sb = new StringBuilder();
            while (true)
            {
                var ch = Convert.ToChar(Console.Read());
                sb.Append(ch);
                if (ch == '\n') break;
            }
            var array = sb.ToString().Split(" ").Select(int.Parse).ToArray();
            foreach (var data in array)
            {
                a[data] += data;
            }

            var q = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());

            for (var i = 0; i < q; i++)
            {
                var bcLine = Console.ReadLine()?.Split(" ").Select(int.Parse).ToArray();
                if (bcLine == null) return;
                var b = bcLine[0];
                var c = bcLine[1];

                if (b != c)
                {
                    var value = a[b] / b * c;
                    a[c] += value;
                    a[b] = 0;

                }
                Console.WriteLine(a.Values.Sum());
            }
        }
    }
}
