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


namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var s = ReadLine().Split(' ');
            long ans = 0;
            int n = int.Parse(s[0]);

            int[] a = new int[100000];
            var s2 = ReadLine().Split(' ');
            int num;
            for (int i = 0; i < n; i++)
            {
                num = int.Parse(s2[i]);
                a[num]++;
            }
            s = ReadLine().Split(' ');
            int q = int.Parse(s[0]);
            var s3 = ReadLine().Split(' ');
            int b, c;
            for (int i = 0; i < q; i++)
            {
                b = int.Parse(s[0]);
                c = int.Parse(s[1]);
                a[c] += a[b];
                a[b] = 0;
            }

            for (int i = 0; i < 100000; i++)
            {
                ans += a[i] * i;
            }


            WriteLine(ans);
            Out.Flush();
        }

    }

    public static class MyMath
    {
        public static long Factorical(long n)
        { return n <= 1 ? 1L : n * Factorical(n - 1); }
        public static long Permutation(long n, long m)
        { return m == 0 ? 1L : (m == n) ? Permutation(n, m - 1) : n * Permutation(n - 1, m - 1); }
        public static long Combination(long n, long m)
        { return n - m > m ? Permutation(n, m) / Factorical(m) : Permutation(n, n - m) / Factorical(n - m); }
        public static long Gcd(long n, long m)
        { return n % m == 0 ? m : Gcd(m, n % m); }
        public static long Lcm(long n, long m)
        { return n * m / Gcd(n, m); }
        public static long Pow(long n, long m)
        { return (m == 1 || m == 0) ? 1L : n * Pow(n, m - 1); }
    }


}