#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int sum = 0;
  int a;
  map<int, int> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    mp[a]++;
    sum += a;
  }
  int q;
  cin >> q;
  int x, y;
  while (q--) {
    cin >> x >> y;
    int k = mp[x];
    sum -= k * x;
    sum += k * y;
    mp[x] = 0;
    mp[y] += k;
    cout << sum << endl;
  }
}
int main() { solve(); }
