#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using gt = greater<int>;
using minq = priority_queue<int, vector<int>, gt>;
using P = pair<ll, ll>;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
vi a;
map<int, int> mp;
ll sum;
void solve() {
  int b, c;
  cin >> b >> c;
  sum -= mp[b] * b;
  sum += mp[b] * c;
  mp[c] += mp[b];
  mp[b] = 0;
  printf("%lld\n", sum);
}
int main() {
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < (n); ++i) {
    cin >> a[i];
    sum += a[i];
    mp[a[i]]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < (q); ++i) solve();
  return 0;
}
