#include <bits/stdc++.h>
using namespace std;
long long int n, m, k;
int solve(long long int m, vector<long long int> v) { return 0; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> c(10);
  long long int sum = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
    c[v[i]]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    sum += (c[a] * (b - a));
    c[b] += c[a];
    c[a] = 0;
    cout << sum << "\n";
  }
  return 0;
}
