#include <bits/stdc++.h>
using namespace std;
long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; }
int main() {
  vector<long long> A(100000);
  int N;
  cin >> N;
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    A[a - 1]++;
    ans += a;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int B, C;
    cin >> B >> C;
    if (B > 5 || C > 5) continue;
    ans -= A[B - 1] * B;
    A[C - 1] += A[B - 1];
    ans += A[B - 1] * C;
    A[B - 1] = 0;
    cout << ans << endl;
  }
  return 0;
}
