#include <bits/stdc++.h>
using namespace std;
typedef long long LL;


int main() {
  int N;
  cin >> N;
  map<int, int> p;
  for(int i=0;i<N;i++){
    int n;
    cin >> n;
    if (p.count(n))
      p[n] += 1;
    else
      p[n] = 1;
  }
  
  int Q;
  cin >> Q;
  int sum =0;
  for(int i=0;i<Q;i++){
    int a,b;
    cin >> a >> b;
    if (p.count(a)){
      p[b] += p[a];
      p[a] = 0;
    }
    
  for (auto q : p) {
    auto k = q.first;
    auto v = q.second;
    sum += k*v;
  }
    
  cout << sum <<endl;
}