#include <bits/stdc++.h>
int main() {
  int arr[1000007]{};
  long long num, l, sum = 0;
  std::cin >> num;
  for (int x = 0; x < num; x++) {
    std::cin >> l, arr[l]++;
    sum += l;
  }
  long long Q;
  std::cin >> Q;
  while (Q--) {
    int B, C;
    std::cin >> B >> C;
    if (arr[B]) {
      long long number = arr[B] * B;
      sum += ((arr[B] * C) - number);
      arr[C] += arr[B];
      arr[B] = 0;
    }
    std::cout << sum << "\n";
  }
}
