#include <bits/stdc++.h>
using namespace std;
int main() {
  char ch;
  long long n, q, u, v;
  long long a[n + 1];
  map<long long, long long> m;
  scanf("%lld", &n);
  long long sum = 0;
  for (int i = 0; i < n; i++) scanf("%lld", &a[i]), m[a[i]]++, sum += a[i];
  scanf("%lld", &q);
  while (q--) {
    scanf("%lld %lld", &u, &v);
    sum -= m[u] * u;
    sum += m[u] * v;
    m[v] += m[u];
    m[u] = 0;
    printf("%lld\n", sum);
  }
}
