#include <bits/stdc++.h>
using namespace std;
ifstream fin("input.in");
int n, q, a[100001], sum = 0;
int freq[100001];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    freq[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += freq[b] * (c - b);
    cout << sum << endl;
    freq[c] += freq[b];
    freq[b] = 0;
  }
}
