#include <bits/stdc++.h>
using namespace std;
int root(vector<int> map, int x) {
  while (map[x] != x) {
    x = map[x];
  }
  return x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> map(1000000);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    map[v[i]]++;
    sum += v[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    sum = sum - map[x] * x + map[x] * y;
    map[y] += map[x];
    map[x] = 0;
    cout << sum << "\n";
  }
}
