#include <bits/stdc++.h>
using namespace std;
void data() {}
const int N = 1e6 + 200;
int kol[N];
int main() {
  data();
  int n;
  cin >> n;
  int a[n + 1];
  long long s = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s += a[i];
    kol[a[i]]++;
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int b, c;
    cin >> b >> c;
    s -= kol[b] * b;
    s += kol[b] * c;
    kol[c] += kol[b];
    kol[b] = 0;
    cout << s << '\n';
  }
}
