#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
const int MX = 100005;
int main() {
  int n;
  cin >> n;
  vector<ll> cnt(MX);
  for (int i = 0; i < (int)(n); i++) {
    int a;
    cin >> a;
    cnt[a]++;
  }
  ll total = 0;
  for (int i = 0; i < (int)(MX); i++) total += i * cnt[i];
  int q;
  for (int i = 0; i < (int)(q); i++) {
    int b, c;
    cin >> b >> c;
    total -= b * cnt[b];
    total -= c * cnt[c];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    total += b * cnt[b];
    total += c * cnt[c];
    cout << total << endl;
  }
}
