#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, q = 0, sum = 0;
  cin >> n;
  map<ll, ll> a;
  for (int i = 0, i_len = (n); i < i_len; ++i) {
    ll index;
    cin >> index;
    sum += index;
    a[index]++;
  }
  cin >> q;
  for (int i = 0, i_len = (q); i < i_len; ++i) {
    ll b, c;
    cin >> b >> c;
    sum -= b * a[b];
    sum += c * a[b];
    a[c] += a[b];
    a[b] -= a[b];
  }
  cout << sum << endl;
  return 0;
}
