#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long power(long long first, unsigned long long second,
                unsigned long long m) {
  if (second == 0) return 1;
  long long p = power(first, second / 2, m) % m;
  p = (p * p) % m;
  return (second % 2 == 0) ? p : (first * p) % m;
}
long long modInverse(long long a, long long m) {
  { return power(a, m - 2, m); }
}
const long long maxn = 500003;
void solve() {
  long long n;
  cin >> n;
  long long a[n];
  long long sum = 0;
  long long first, second;
  long long f[maxn] = {0};
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    f[a[i]]++;
  }
  long long q;
  cin >> q;
  while (q--) {
    cin >> first >> second;
    sum -= f[first] * first;
    sum += f[second] * second;
    f[second] += f[first];
    f[first] = 0;
    cout << sum << "\n";
  }
}
signed main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
