#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int A[n + 5];
  long long int sum = 0;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
    m[A[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    long long int ans = sum - m[b] * b + c * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << ans << endl;
    sum = ans;
  }
}
