#include <bits/stdc++.h>
using namespace std;
void solve();
const long long int N = 1e6 + 10;
long long int f[N];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  while (t--) solve();
  return 0;
}
void solve() {
  long long int i, j;
  long long int n, q;
  cin >> n;
  vector<long long int> a(n);
  long long int sum = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    f[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  while (q--) {
    cin >> i >> j;
    sum -= f[i] * i;
    sum += f[j] * j;
    f[j] += f[i];
    f[i] = 0;
    cout << sum << "\n";
  }
}
