#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
const int INF = numeric_limits<int>::max();
long long mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0) res += m;
  return res;
}
long long gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
long long lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  ll n, q;
  cin >> n;
  map<int, int> mp;
  ll sum = 0;
  for (int i = 0; i < (n); i++) {
    int tmp;
    cin >> tmp;
    mp[tmp]++;
    sum += tmp;
  }
  cin >> q;
  for (int i = 0; i < (q); i++) {
    ll b, c;
    cin >> b >> c;
    if (mp[c] == 0) {
      sum = sum + (c - b) * (mp[b]);
      mp[c] += mp[b];
    } else {
      if (c - b >= 0) {
        mp[c] += mp[b];
        sum = sum + (c - b) * abs(mp[c] - mp[b]);
      } else {
        sum = sum + (c - b) * abs(mp[c] - mp[b]);
        mp[c] += mp[b];
      }
    }
    mp[b] = 0;
    cout << sum << endl;
  }
  return 0;
}
