#include <bits/stdc++.h>
using namespace std;
long long n, a, sum, num[20], q, b;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a, sum += a, num[a]++;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    sum += (b - a) * num[a];
    num[b] += num[a];
    num[a] = 0;
    cout << sum << endl;
  }
  return 0;
}
