#include <bits/stdc++.h>
using namespace std;
int freq[100010];
long long sum = 0;
int main() {
  fill(freq, freq + 100010, 0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ++freq[a];
    sum += a;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum = sum - freq[b] * b + freq[b] * c;
    cout << sum << endl;
    freq[c] += freq[b];
    freq[b] = 0;
  }
  return 0;
}
