#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int main() {
  int n;
  cin >> n;
  a.assign(n, 0);
  int sum = 0;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    sum = sum + a[i];
  }
  int q;
  cin >> q;
  int x, y;
  while (q--) {
    cin >> x >> y;
    if (m[x]) {
      if (y > x)
        sum += m[x] * (y - x);
      else
        sum -= m[x] * (x - y);
      int temp = m[x];
      m.erase(x);
      m[y] += temp;
    }
    cout << sum << endl;
  }
}
