#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int c[N];
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int n, q;
  cin >> n;
  int a[n];
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    sum += a[i];
    c[a[i]]++;
  }
  cin >> q;
  while (q--) {
    int b, to;
    cin >> b >> to;
    sum -= c[b] * b;
    sum += c[b] * to;
    c[to] += c[b];
    c[b] = 0;
    cout << sum << "\n";
  }
}
