#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()

template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) { return std::vector<T>(n, std::forward<T>(val)); }
template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}
template<class Cond> struct chain {
  Cond cond; chain(Cond cond) : cond(cond) {}
  template<class T> bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; }
};
template<class Cond> chain<Cond> make_chain(Cond cond) { return chain<Cond>(cond); }

int main() {
  i64 N;
  cin >> N;
  vector<i64> A(N);
  map<i64, i64> mp;
  rep(i,0,N) {
    cin >> A[i];
    mp[A[i]]++;
  }
  i64 Q;
  cin >> Q;
  while(Q--) {
    i64 b, c;
    cin >> b >> c;
    i64 cnt = mp[b];
    mp[b] = 0;
    mp[c] += cnt;
  }
  i64 ans = 0;
  for(auto p: mp) {
    ans += p.first * p.second;
  }
  cout << ans << endl;
}

