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