#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long a[n];
  long long i, j, k;
  unordered_map<long long, long long> m;
  long long sum = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum = sum + a[i];
  }
  long long t;
  cin >> t;
  while (t-- > 0) {
    long long x, y;
    cin >> x >> y;
    sum = sum - m[x] * x + (m[x]) * y;
    m[y] = m[x] + m[y];
    m[x] = 0;
  }
}
