#include <bits/stdc++.h>
static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;
template <class T>
constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  map<int, int> m;
  for (auto &&i : v) scanf("%d", &i), m[i]++;
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    m[c] += m[b];
    m[b] = 0;
  }
  ll ans = 0;
  for (auto &&i : m) {
    ans += (ll)i.first * i.second;
  }
  cout << ans << "\n";
  return 0;
}
