#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, t;
  long long ans = 0;
  cin >> n;
  map<char, int> a;
  for (int i = 0; i < (int)(n); i++) {
    cin >> t;
    a[char(t)]++;
    ans += t;
  }
  cin >> q;
  int b, c;
  for (int i = 0; i < (int)(q); i++) {
    cin >> b >> c;
    ans = ans - b * a[char(b)] + c * a[char(b)];
    a[char(c)] += a[char(b)];
    a[char(b)] = 0;
    cout << ans << endl;
  }
}
