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