#include <bits/stdc++.h>
using ll = long long;
int main() {
  ll sum = 0;
  std::map<ll, ll> ma;
  ll n, a, b, c, q, x;
  std::cin >> n;
  for (ll i = 0; i < n; i++) {
    std::cin >> x;
    ma[x]++;
  }
  std::cin >> q;
  for (ll t = 0; t < q; t++) {
    std::cin >> b >> c;
    ll k = ma[b];
    ma[b] = 0;
    ma[c] = k;
  }
  ll ans = 0;
  for (auto &v : ma) {
    ans += (v.first) * (v.second);
  }
  std::cout << ans << std::endl;
}
