#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
long long a, b, v, w, t;
int main() {
  int n;
  cin >> n;
  vector<int> x(100001, 0);
  for (int i = 0; i < (n); i++) {
    int tmp;
    cin >> tmp;
    x[tmp]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < (q); i++) {
    int b, c;
    cin >> b >> c;
    int tmp = x[b];
    x[b] = 0;
    x[c] += tmp;
  }
  long long ans = 0;
  for (int i = 0; i < (100001); i++) ans += i * x[i];
  cout << ans << endl;
  return 0;
}
