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