#include <bits/stdc++.h>
using namespace std;
const long long int sz = 1e5 + 5;
const long long int szz = 1e6 + 6;
const long long int mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  clock_t startTime = clock();
  long long int n;
  cin >> n;
  long long int ar[n];
  map<long long int, long long int> mp;
  long long int sum = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> ar[i];
    mp[ar[i]]++;
    sum += ar[i];
  }
  long long int q;
  while (q--) {
    long long int a, b;
    cin >> a >> b;
    sum -= a * mp[a];
    sum += b * mp[a];
    mp[b] += mp[a];
    mp[a] = 0;
    cout << sum << "\n";
  }
  cerr << "\n"
       << setprecision(20)
       << double(clock() - startTime) / (double)CLOCKS_PER_SEC << " seconds."
       << "\n";
}
