#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int inf = 1000000007;
int main() {
  int n, q;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(n + 1);
  long long sum = 0;
  for (int i = 0; i < (n); ++i) {
    cin >> a[i];
    cnt[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  vector<long long> bc(q + 1);
  for (int i = 1; i <= q; i++) {
    int b, c;
    cin >> b >> c;
    bc[i] = (c - b) * cnt[b];
    cnt[c] += cnt[b];
    cnt[b] = 0;
  }
  vector<long long> ans(q);
  ans[0] = bc[1] + sum;
  for (int i = 1; i < q; i++) {
    ans[i] = ans[i - 1] + bc[i + 1];
  }
  for (int i = 0; i < (q); ++i) cout << ans[i] << endl;
}
