#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x;
  cin >> n;
  map<int, int> d;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    d[x]++;
    sum = sum + x;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int a = d[b];
    sum = sum - b * a + c * a;
    d[c] += a;
    d[b] = 0;
    cout << sum << "\n";
  }
  return 0;
}
