#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MAX = 1e5 + 5;
int main() {
  int n, q;
  cin >> n;
  long long a[MAX], ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x - 1]++;
    ans += x;
  }
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    ans -= a[b - 1] * b;
    ans += a[b - 1] * c;
    a[c - 1] += a[b - 1];
    a[b - 1] = 0;
    cout << ans << "\n";
  }
}
