#include <bits/stdc++.h>
using namespace std;

int main() {
  long N, A, B, C, Q, sum = 0;
  map<int, int> M;
  cin >> N;
  while (N-- && cin >> A) M[A]++, sum += A;
  cin >> Q;
  while (cin >> B >> C) {
    cout << (sum += (C - B) * M[B]) << "\n";
    M[C] += M[B], M[B] = 0;
  }
}