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