#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q;
  cin >> n;
  int arr[1000001];
  for (int i = 0; i < 1000001; i++) {
    arr[i] = 0;
  }
  int num = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    arr[x]++;
    num += x;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    arr[c] = arr[b] + arr[c];
    num += (c - b) * arr[b];
    arr[b] = 0;
    cout << num << endl;
  }
}
