#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, q, b, c;
  cin >> n;
  long long a[n], s = 0;
  map<long long, long long> mp;
  for (long long i = 0; i < n; i++) cin >> a[i], mp[a[i]]++, s += a[i];
  cin >> q;
  for (long long i = 0; i < q; i++) {
    cin >> b >> c;
    if (mp[c] == 0)
      s = s - b * mp[b] + mp[b] * c, mp[c] = mp[b];
    else
      s = s - b * mp[b] + mp[b] * c, mp[c] += mp[b];
    cout << s << '\n';
  }
}
int main() { solve(); }
