#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a[10010];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int ia;
    cin >> ia;
    a[ia]++;
  }
  ll ans = 0;
  for (int i = 0; i <= n; i++) ans += i * a[i];
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    ans += (c - b) * a[b];
    cout << ans << endl;
    a[c] += a[b];
    a[b] = 0;
  }
}
