#include <bits/stdc++.h>
using namespace std;
int N, Q;
int main() {
  cin >> N;
  vector<int> A(N);
  unordered_map<int, int64_t> mp;
  for (auto &x : A) cin >> x, mp[x]++;
  int64_t sum = accumulate(A.begin(), A.end(), 0);
  cin >> Q;
  while (Q--) {
    int a, b;
    cin >> a >> b;
    sum -= a * mp[a];
    sum += b * mp[a];
    mp[b] += mp[a];
    mp[a] = 0;
    cout << sum << "\n";
  }
}
