#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); }
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int modInverse(long long int n, long long int p) {
  return power(n, p - 2, p);
}
long long int ncr(long long int n, long long int r, long long int p) {
  if (r == 0) return 1;
  long long int fac[n + 1];
  fac[0] = 1;
  for (long long int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;
  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
int a1[1000001];
int main() {
  int n;
  cin >> n;
  int a[n];
  int sum = 0;
  for (int i = 0; i < 1000001; i++) {
    a1[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
    a1[a[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    if (a1[x] != 0) {
      sum += a1[x] * (y - x);
      a1[y] += a1[x];
      a1[x] = 0;
    }
    cout << sum << '\n';
  }
}
