#include <bits/stdc++.h>
using namespace std;
using bigint = long long int;
using vi = vector<int>;
using vll = vector<long long int>;
int cnt[200001];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  int N;
  cin >> N;
  bigint sum = 0;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    cnt[x]++;
    sum += x;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int B, C;
    cin >> B >> C;
    sum = sum - cnt[B] * B + cnt[B] * C;
    cnt[C] += cnt[B];
    cnt[B] = 0;
    cout << sum << endl;
  }
}
