#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  unordered_map<int, int> h;
  int cs = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    h[a]++;
    cs += a;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    cs -= b * h[b];
    cs += c * h[b];
    h[c] += h[b];
    h[b] = 0;
    cout << cs << "\n";
  }
}
