#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A[N];
  int freq[100001];
  memset(freq, 0, sizeof freq);
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    freq[A[i]]++;
    sum += A[i];
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    sum = sum + (C - B) * freq[B];
    freq[C] += freq[B];
    freq[B] = 0;
    cout << sum << endl;
  }
}
