#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<int> count(10001, 0);
  vector<int> v(n);
  long long sum = 0;
  for (auto i : v) {
    cin >> i;
    sum += i;
    count[i]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    count[c] += count[b];
    sum += (c - b) * count[b];
    count[b] = 0;
    cout << sum << endl;
  }
  return 0;
}
