#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
  long long n;
  cin >> n;
  vector<long long> cntMemo(n + 1, 0);
  long long sum = 0;
  for (int i = 0; i < (int)(n); i++) {
    long long a;
    cin >> a;
    cntMemo[a]++;
    sum += a;
  }
  long long q;
  cin >> q;
  for (int i = 0; i < (int)(q); i++) {
    long long b, c;
    cin >> b >> c;
    sum += cntMemo[b] * (c - b);
    cntMemo[c] += cntMemo[b];
    cntMemo[b] = 0;
    cout << sum << endl;
  }
}
