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