#include <bits/stdc++.h>
using namespace std;
const long long N = 3e6 + 7, M = 1000000007, inf = INT_MAX;
const long long INF = LLONG_MAX;
long long n, Q;
long long arr[N + 2], freq[N];
void solve() {
  cin >> n;
  long long sum = 0;
  for (long long i = 0; i < (long long)n; i++) {
    long long x;
    cin >> x;
    sum += x;
    freq[x]++;
  }
  cin >> Q;
  while (Q--) {
    long long b, c;
    cin >> b >> c;
    sum -= freq[b] * b;
    sum -= freq[c] * c;
    freq[c] += freq[b];
    sum += freq[c] * c;
    cout << sum << '\n';
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long test = 1;
  while (test--) solve();
}
