#include <bits/stdc++.h>
using namespace std;
long long a[7001];
int main() {
  long long n;
  cin >> n;
  long long s = 0;
  for (int i = 1; i <= n; i++) {
    long long o;
    cin >> o;
    a[o]++;
    s += o;
  }
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    long long x, y;
    cin >> x >> y;
    s += (y - x) * a[x];
    a[y] += a[x];
    a[x] = 0;
    cout << s << endl;
  }
}
