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