#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  while (t--) {
    long long n;
    cin >> n;
    int a;
    long long sum = 0;
    map<int, int> m;
    for (int i = 0; i < n; i++) {
      cin >> a;
      m[a]++;
      sum += a;
    }
    int q;
    cin >> q;
    while (q--) {
      int b, c;
      cin >> b >> c;
      sum = sum - b * m[b] - c * m[c];
      m[c] += m[b];
      sum = sum + (m[c] * c);
      m[b] = 0;
      cout << sum << endl;
    }
  }
  return 0;
}
