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