#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int mx = 1e5 + 10;
int total = 0;
vector<int> freq(mx, 0);
void solve() {
  int b, c;
  cin >> b >> c;
  total = total + (freq[b] * (c - b));
  int temp = freq[b];
  freq[b] = 0;
  freq[c] += temp;
  cout << total << endl;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    total += a[i];
    freq[a[i]]++;
  }
  int test;
  cin >> test;
  while (test--) {
    solve();
  }
  return 0;
}
