#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  long long int sum = 0;
  if (n == 0)
    printf("0\n");
  else {
    unordered_map<int, int> m;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      m[a[i]]++;
      sum = sum + a[i];
    }
    int q;
    cin >> q;
    while (q--) {
      int search, val;
      cin >> search >> val;
      sum = sum - (search * m[search]);
      sum = sum + (val * m[search]);
      m[val] = m[val] + m[search];
      m[search] = 0;
      cout << sum << "\n";
    }
  }
}
