#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6;
int n, q;
int v[MAX];
long long ans;
void solve() {
  memset(v, 0, sizeof v);
  ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v[x]++;
    ans += x;
  }
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    ans -= a * v[a];
    ans += b * v[a];
    v[b] += v[a];
    v[a] = 0;
    cout << ans << "\n";
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
