#include <bits/stdc++.h>
int main() {
  std::unordered_map<int, int> nums;
  int n_c;
  std::cin >> n_c;
  int sum = 0;
  for (int i = 0; i < n_c; i++) {
    int q;
    std::cin >> q;
    nums[q]++;
    sum += q;
  }
  int o_c;
  std::cin >> o_c;
  for (int o = 0; o < o_c; o++) {
    int a, b;
    std::cin >> a >> b;
    sum += nums[a] * b - nums[a] * a;
    nums[b] += nums[a];
    nums[a] = 0;
    std::cout << sum << "\n";
  }
}
