#include <bits/stdc++.h>
using namespace std;
long long int m = 1000000007;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  unordered_map<int, int> m;
  long long int sum = 0;
  int n;
  cin >> n;
  while (n--) {
    int x;
    cin >> x;
    m[x]++;
    sum += x;
  }
  int qq;
  cin >> qq;
  while (qq--) {
    int b, c;
    cin >> b >> c;
    int count = m[b];
    m[b] = 0;
    m[c] += count;
    sum -= count * b;
    sum += count * c;
    cout << sum << '\n';
  }
}
