#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int a[N];
int main() {
  int n;
  cin >> n;
  int x;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    sum += x;
    a[x]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << sum << endl;
  }
  return 0;
}
