#include <bits/stdc++.h>
using namespace std;
const int maxi = 2000000000;
const int maxq = 1000000000;
const long long mod = 1000000007LL;
int aa[1000001];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, sum = 0;
  cin >> n;
  int a[n];
  for (long long i = (0); i < (n); ++i) {
    cin >> a[i];
    aa[a[i]]++;
    sum += a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int num1 = aa[b];
    int num2 = aa[c];
    sum = sum - (num1 * b) + (num1 * c);
    aa[b] = 0;
    aa[c] += num1;
    cout << sum << endl;
  }
  return 0;
}
