#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  map<int, int> A;
  for (int i = 0; i < (int)(N); i++) {
    int a;
    cin >> a;
    if (A.count(a)) {
      A[a]++;
    } else {
      A[a] = 1;
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < (int)(Q); i++) {
    int b, c;
    cin >> b >> c;
    int tmp;
    if (A.count(b)) {
      tmp = A[b];
      if (A.count(c)) {
        A[c] += tmp;
      } else {
        A[c] = tmp;
      }
      A[b] = 0;
    }
    long long total = 0;
    for (auto a : A) {
      total += a.first * a.second;
    }
    cout << total << endl;
  }
}
