#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  int result;
  int max;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    result += A[i];
  }
  max = *max_element(A.begin(), A.end());
  vector<int> AA(max);
  for (int i = 0; i < N; i++) {
    AA[A[i]]++;
  }
  int Q;
  cin >> Q;
  int b, c;
  int temp;
  Graph G(Q);
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    temp = AA[b];
    AA[b] = 0;
    AA[c] += temp;
  }
  for (int i = 0; i < (int)AA.size(); i++) {
    result += i * AA[i];
  }
  cout << result << endl;
  return 0;
}
