#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int N, Q;
  cin >> N;
  int a;
  unordered_map<int, int> nums;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> a;
    if (nums.find(a) != nums.end()) {
      nums[a] += 1;
    } else {
      nums[a] = 1;
    }
    sum += a;
  }
  cin >> Q;
  int b, c;
  for (int i = 0; i < Q; i++) {
    cin >> b >> c;
    if (nums.find(b) != nums.end() && nums[b] != 0) {
      if (b > c) {
        sum -= (b - c) * nums[b];
      } else {
        sum += (c - b) * nums[b];
      }
      if (nums.find(c) == nums.end()) nums[c] = 0;
      nums[c] += nums[b];
      nums[b] = 0;
    }
    cout << sum << "\n";
  }
}
