#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
int main() {
  int n, q, num[110000] = {0};
  unsigned long long sum = 0;
  cin >> n;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    sum += a;
    num[a]++;
  }
  cin >> q;
  int b, c;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    sum = sum - b * num[b] + c * num[b];
    printf("%lld\n", sum);
    num[c] += num[b];
    num[b] = 0;
  }
}
