#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
int a[100000];
signed main() {
  int n;
  cin >> n;
  unordered_map<int, int> pq;
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
    ++pq[a[i]];
  }
  int k;
  scanf("%d", &k);
  for (int i = 0; i < k; ++i) {
    int l, m;
    cin >> l >> m;
    sum -= (l * pq[l] + m * pq[m]);
    pq[m] = pq[m] + pq[l];
    pq[l] = 0;
    sum += pq[m] * m;
    cout << sum << "\n";
  }
  return 0;
}
