#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, sum = 0;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
    sum += x;
  }
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    int c = count(v.begin(), v.end(), x);
    while (c--) {
      replace(v.begin(), v.end(), x, y);
      sum += abs(x - y);
    }
    cout << sum << endl;
  }
  return 0;
}
