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