#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long modx = 998244353;
const long long Nx = 1e5 + 2;
const long long inf = (long long)(1e19 + 10LL);
void solve() {
  long long n;
  cin >> n;
  vector<long long> arr(n);
  unordered_map<long long, long long> counter;
  for (auto &it : arr) {
    cin >> it;
    counter[it]++;
  }
  long long q;
  cin >> q;
  while (q--) {
    long long a, b;
    cin >> a;
    cin >> b;
    long long freq = counter[a];
    counter[b] = counter[b] + freq;
    counter[a] = 0;
    unordered_map<long long, long long> dup;
    long long sum = 0;
    for (auto it : counter) {
      if (it.second == 0) continue;
      sum = sum + it.first * it.second;
      dup[it.first] = it.second;
    }
    cout << sum << endl;
    counter = dup;
  }
}
signed main() {
  long long test = 1;
  while (test--) {
    solve();
  }
  return 0;
}
