#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<long long> num(100004, 0);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    num[a]++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    num[c] += num[b];
    num[b] = 0;
  }
  long long ans = 0;
  for (int i = 0; i < 100004; i++) {
    long long j = (long long)i;
    ans += j * num[j];
  }
  cout << ans << endl;
}
