#include <bits/stdc++.h>
using namespace std;
int seive[10000];
int main() {
  int t;
  cin >> t;
  long long int sum = 0;
  for (int i = 0; i < t; i++) {
    long long int k;
    cin >> k;
    sum += k;
    seive[k]++;
  }
  int k;
  cin >> k;
  while (k--) {
    long long int a, b;
    cin >> a >> b;
    sum += ((long long int)seive[a] * (b - a));
    seive[b] += seive[a];
    seive[a] = 0;
    cout << sum << '\n';
  }
}
