#include <bits/stdc++.h>
using namespace std;
const int sz = 2e5 + 1;
long long sum = 0;
int mem[sz];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    mem[a]++;
    sum += a;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    sum -= mem[a] * a;
    sum -= mem[b] * b;
    mem[b] += mem[a];
    mem[a] = 0;
    sum += mem[b] * b;
    cout << sum << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  for (; q > 0; q--) {
    solve();
    cout << '\n';
  }
}
