#include <bits/stdc++.h>
using namespace std;
int counter[100001] = {0};
int main() {
  int n;
  cin >> n;
  int arr[n];
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    counter[arr[i]]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum = sum - counter[b] * b + counter[b] * c;
    counter[c] += counter[b];
    counter[b] = 0;
    cout << sum << endl;
  }
}
