#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
vector<ll> prime;
int main() {
  ll n;
  cin >> n;
  vector<int> a(10000);
  for (ll i = 0; i < (10000); ++i) {
    a[i] = 0;
  }
  ll sum = 0;
  for (ll i = 0; i < (n); ++i) {
    int in;
    cin >> in;
    sum += in;
    a[in]++;
  }
  ll q;
  cin >> q;
  for (ll i = 0; i < (q); ++i) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << sum << endl;
  }
  return 0;
}
