#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  int count[100001] = {0};
  int a;
  for (int i = 0; i < N; i++) {
    cin >> a;
    count[a]++;
  }
  int Q;
  cin >> Q;
  int b, c, tmp;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    count[c] += count[b];
    count[b] = 0;
    long long tot = 0;
    for (int j = 0; j < 100001; j++) {
      tot += count[j] * j;
    }
    cout << tot << endl;
  }
  return 0;
}
