#include <bits/stdc++.h>
using namespace std;
int a[1000000];
int main() {
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    ans += x;
    a[x]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    if (a[x] == 0) {
      cout << ans << endl;
      continue;
    }
    ans = ans - x * a[x];
    ans = ans + y * a[x];
    a[y] += a[x];
    a[x] = 0;
    cout << ans << endl;
  }
  return 0;
}
