#include <bits/stdc++.h>
using namespace std;
int n, q, tong[233333];
long long sum;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1, x; i <= n; i++) cin >> x, tong[x]++, sum += x;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum = sum - b * tong[b] + c * tong[b];
    tong[c] += tong[b];
    tong[b] = 0;
    cout << sum << endl;
  }
}
