#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long S = 0;
  unordered_map<long long, long long> cnt;
  long long A;
  for (int i = 0; i < N; i++) {
    cin >> A;
    S += A;
    cnt[A]++;
  }
  int Q;
  cin >> Q;
  long long B, C;
  for (int i = 0; i < Q; i++) {
    cin >> B >> C;
    long long cnt_B = cnt[B];
    long long cnt_C = cnt[C];
    S -= B * cnt_B;
    S += C * cnt_B;
    cnt[B] = 0;
    cnt[C] = cnt_B + cnt_C;
  }
  cout << S << endl;
  return 0;
}
