#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;
int main() {
  int n;
  cin >> n;
  ll cnt = 0;
  map<int, int> mp;
  for (int i = 0; i < (n); ++i) {
    int a;
    cin >> a;
    mp[a]++;
    cnt += a;
  }
  int q;
  cin >> q;
  for (int i = 0; i < (q); ++i) {
    int b, c;
    cin >> b >> c;
    cnt += (c - b) * mp[b];
    mp[c] += mp[b];
    mp[b] = 0;
  }
  cout << cnt << endl;
  return 0;
}
