#include <bits/stdc++.h>
using namespace std;
vector<long long> freq(100001);
int main() {
  int n, flag = 0, x, value = 0, sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    sum += x;
    freq[x]++;
  }
  int t;
  cin >> t;
  while (t--) {
    int a, b;
    cin >> a >> b;
    sum += (b * freq[a] - a * freq[a]);
    freq[b] += freq[a];
    freq[a] = 0;
    cout << sum << endl;
  }
}
