#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n;
  int a;
  map<int, int> mp;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    sum += a;
    mp[a]++;
  }
  int q;
  cin >> q;
  int x, y;
  while (q--) {
    cin >> x >> y;
    sum -= mp[x] * x;
    sum += mp[x] * y;
    mp[y] += mp[x];
    mp[x] = 0;
    cout << sum << endl;
  }
}
