#include <iostream>
using namespace std;
static int count[100001];
int main()
{
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    count[A]++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    count[C] += count[B];
    count[B] = 0;
  }
  long long ans = 0;
  for (int i = 1; i <= 100000; i++) {
    ans += (long long)count[i] * i;
  }
  cout << ans << endl;
  return 0;
}
