#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long LINF = 1000000000000000000;
const double EPS = 1e-10;
int main(void) {
  int N;
  cin >> N;
  map<int, int> A;
  long long all = 0;
  for (int i = 0; i < N; i++) {
    int num;
    cin >> num;
    A[num]++;
    all += num;
  }
  int Q;
  cin >> Q;
  int before, after;
  long long result = all;
  for (int i = 0; i < Q; i++) {
    cin >> before >> after;
    if (A.count(after)) {
      result = result - (before * A[before]) + (after * A[after]);
      A[after] += A[before];
      A[before] = 0;
    } else {
      int cnt = A[before];
      A[after] = cnt;
      result = result - (before * A[before]) + (after * A[after]);
    }
    printf("%lld\n", result);
  }
  return (0);
}
