#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = long long(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  long long q;
  cin >> q;
  vector<long long> count(100001, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    count[a[i]]++;
    sum += a[i];
  }
  for (int i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    sum -= count[b] * b;
    sum += count[b] * c;
    count[c] += count[b];
    count[b] = 0;
  }
  cout << sum << "\n";
}
