#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
const double PI = acos(-1);
int main() {
  int n;
  cin >> n;
  map<ll, ll> mp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    mp[a]++;
    sum += a;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    ll b, c;
    cin >> b >> c;
    if (mp[b] != 0) {
      ll num = mp[b];
      mp[c] += num;
      mp[b] = 0;
      sum = sum - b * num + c * num;
      cout << sum << endl;
    }
  }
}
