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