#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[100010], t;
  memset(a, 0, sizeof(a));
  for (int i = 0; i < n; i++) {
    cin >> t;
    a[t]++;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int x, y;
    int ans = 0;
    cin >> x >> y;
    while (a[x]) {
      a[x]--;
      a[y]++;
    }
    for (int i = 0; i < 100010; i++) ans += i * a[i];
    cout << ans << endl;
  }
  return 0;
}
