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