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