#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> a(10010);
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    a[x]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    int sum = 0;
    for (int j = 1; j < 10001; ++j) {
      int xx = a[j] * j;
      sum += xx;
    }
    cout << sum << endl;
  }
}
