#include <bits/stdc++.h>
using namespace std;
bool comp1(pair<int, int> &a, pair<int, int> &b) { return a.second > b.second; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  map<int, int> m;
  int s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
    m[a[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    s += m[b] * (c - b);
    m[c] += m[b];
    m[b] = 0;
    cout << s << "\n";
  }
}
