#include <bits/stdc++.h>
using namespace std;
void printVector(set<int> x) {
  for (auto i : x) {
    cout << i << " ";
  }
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, a, b, x, q;
  cin >> n;
  long long int sum = 0;
  map<int, int> mapping;
  for (int i = 0; i < n; i++) {
    cin >> x;
    sum += x;
    mapping[x]++;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    x = mapping[a] + mapping[b];
    sum = (sum) - (mapping[a] * a) + (b * mapping[b]);
    cout << sum << endl;
    mapping[b] = x;
    mapping[a] = 0;
  }
}
