#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  for (int& i : a) cin >> i;
  cin >> q;
  map<int, int> m;
  int r = 0;
  for (int i = 0; i < n; ++i) ++m[a[i]], r += a[i];
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    cout << (r += (c - b) * m[b]) << endl;
    m[c] += m[b];
    m[b] = 0;
  }
}
