#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> arr(n);
  long long sum = 0;
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    mp[arr[i]]++;
  }
  int q;
  cin >> q;
  int x, y;
  for (int i = 0; i < q; i++) {
    cin >> x >> y;
    sum = sum - (mp[x] * x) + (y * mp[x]);
    mp[y] += mp[x];
    mp[x] = 0;
    cout << sum << endl;
  }
  return 0;
}
