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