#include <bits/stdc++.h>
using namespace std;
int n, q, b, c, x, cont[100001];
long long int s;
int main() {
  cin >> n;
  while (n--) {
    cin >> x;
    cont[x]++;
    s += x;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    s -= cont[b] * b;
    s += cont[b] * c;
    cont[c] += cont[b];
    cout << s << endl;
  }
  return 0;
}
