#include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
int main() {
  int n;
  cin >> n;
  unordered_map<int, int> m;
  long long ans = 0;
  vector<int> arr(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    m[arr[i]] += 1;
    ans += arr[i];
  }
  int q;
  cin >> q;
  int b, c;
  while (q--) {
    cin >> b >> c;
    ans -= m[c] * c * 1ll + m[b] * b * 1ll;
    m[c] += m[b];
    m[b] = 0;
    ans += m[c] * c * 1ll;
    cout << ans << "\n";
  }
  return 0;
}
