using System;
using static System.Console;
using static System.Math;
using System.Numerics;
using System.Collections.Generic;
using System.Linq;


namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var n = int.Parse(ReadLine());
            var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var kosuu = new int[100000];
            var ans = 0;
            for (int i = 0; i < n; i++)
            {
                ans += a[i];
                kosuu[a[i]]++;
            }
            var q = int.Parse(ReadLine());

            for (int i = 0; i < q; i++)
            {
                var s = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                ans += a[s[0]] * (s[1] - s[0]);
                a[s[1]] += a[s[0]];
                a[s[0]] = 0;
                WriteLine(ans);
                Out.Flush();
            }
        }
    }
}