#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  freopen("AtCoder6/input.txt", "r", stdin);
  freopen("AtCoder6/output.txt", "w", stdout);
  int n;
  cin >> n;
  int hist[10001] = {0};
  int summ = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    summ += t;
    hist[t]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    summ -= hist[b] * b;
    summ += hist[b] * c;
    hist[c] += hist[b];
    hist[b] = 0;
    cout << summ << endl;
  }
  return 0;
}
