#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const long long inf = 2e9, MOD = 1e9 + 7, MAX = 1e4 + 1;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  long long a[n], res = 0;
  map<int, int> mp;
  for (int i = 0; i < n; i++) cin >> a[i], mp[a[i]]++, res += a[i];
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    res -= mp[b] * b;
    res += mp[b] * c;
    mp[c] += mp[b];
    mp[b] = 0;
    cout << res << "\n";
  }
}
