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