#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const double EPS = 1e-8;
long long pow(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1) ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}
template <typename T, typename U>
bool chmax(T& max, const U& x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}
template <typename T, typename U>
bool chmin(T& min, const U& x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}
int main() {
  vector<long long> a(100001, 0);
  long long n, q, ans = 0;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long tmp;
    cin >> tmp;
    a[tmp]++;
  }
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long b, c, tmp;
    cin >> b >> c;
    tmp = a[b];
    a[b] = 0;
    a[c] += tmp;
  }
  for (long long i = 0; i < 100001; i++) {
    ans += (i * a[i]);
  }
  cout << ans << endl;
}
