#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1000000000000000000;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  long long sum = 0;
  vector<int> count(1000000);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    count[a]++;
    sum += a;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    sum += c * count[b];
    sum -= b * count[b];
    cout << sum << endl;
    count[c] += count[b];
    count[b] = 0;
  }
  return 0;
}
