#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100001;
int data[MAX_N];
int main() {
  for (int i = 0; i < MAX_N; i++) {
    data[i] = 0;
  }
  int N, sum;
  cin >> N;
  sum = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    data[a]++;
    sum += a;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    sum += -1 * data[b] * b + data[b] * c;
    data[c] += data[b];
    data[b] = 0;
    cout << sum << endl;
  }
  return 0;
}
