#include <bits/stdc++.h>
using namespace std;
const int siz = 1e5 + 5;
const int MOD = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  int a[n];
  int sum = 0;
  int freq[siz] = {0};
  for (int &i : a) cin >> i, sum += i, freq[i]++;
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int minus = b * freq[b];
    sum -= minus;
    sum += freq[b] * c;
    freq[c] += freq[b];
    freq[b] = 0;
    cout << sum << '\n';
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
