#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MOD = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int a[n];
  unordered_map<int, int> mp;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    a[i] = temp;
    mp[temp] += 1;
    sum += temp;
  }
  int Q;
  cin >> Q;
  for (int q = 0; q < Q; q++) {
    int b, c;
    cin >> b >> c;
    int countb = mp[b];
    int countc = mp[c];
    sum = sum - mp[b] * b + c * mp[b];
    mp[c] += mp[b];
    mp[b] = 0;
    cout << sum << '\n';
  }
}
