#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const ll inf = 1001001001;
const ll e9 = 1000000000;
const ll mod = 1000000007;
int main() {
  int n, q;
  cin >> n;
  vector<int> a(n), cnt(100001);
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    cnt[a]++;
    sum += a;
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    sum += cnt[b] * (c - b);
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  cout << sum << endl;
}
