#include <bits/stdc++.h>
using namespace std;
const int NUM = 1e6;
int main() {
  long long n, sum = 0;
  cin >> n;
  int a_max = 0;
  vector<long long> v(NUM, 0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a_max < a) {
      a_max = a;
    }
    v[a] += 1;
  }
  int q, b, c;
  cin >> q;
  for (int i = 0; i < q; i++) {
    sum = 0;
    cin >> b >> c;
    if (a_max < b) {
      a_max = b;
    }
    if (a_max < c) {
      a_max = c;
    }
    v[c] += v[b];
    v[b] = 0;
    for (int i = 0; i <= a_max; i++) {
      sum += v[i] * i;
    }
    cout << sum << endl;
  }
  return 0;
}
