#include <bits/stdc++.h>
using namespace std;
void solve();
void queries() {
  int n;
  cin >> n;
  while (n--) solve();
}
long long q, n;
void solve() { return; }
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  long long rs = 0;
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    mp[arr[i]]++;
    rs += arr[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    rs -= mp[a] * a;
    rs -= mp[b] * b;
    mp[b] += mp[a];
    mp[a] = 0;
    rs += mp[b] * b;
    cout << rs << "\n";
    ;
  }
  return 0;
}
