#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 7;
void solve() {
  map<int, int> hash;
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    hash[x]++;
    sum += x;
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    sum -= hash[a] * a;
    sum += b * hash[a];
    hash[b] += hash[a];
    hash[a] = 0;
    cout << sum << '\n';
  }
}
int main() {
  int t = 1;
  while (t--) solve();
}
