#include <bits/stdc++.h>
#pragma optimize("Ofast")
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  ll sum = 0;
  map<int, int> mp;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    mp[x]++;
    sum += x;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum -= mp[b] * b;
    sum += c * mp[b];
    mp[c] += mp[b];
    mp[b] = 0;
    cout << sum << "\n";
  }
  return 0;
}
