#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  ll ans = 0;
  for (int i = 0; i < (int)(n); i++) {
    int a;
    cin >> a;
    mp[a]++;
    ans += a;
  }
  int q;
  cin >> q;
  for (int qi = 0; qi < (int)(q); qi++) {
    int b, c;
    cin >> b >> c;
    ans = ans - mp[b] * b + mp[b] * c;
    mp[c] += mp[b];
    mp[b] = 0;
    cout << ans << endl;
  }
}
