#include <bits/stdc++.h>
using namespace std;
void readFromText() {}
const double Pi = acos(-1.0);
bool countIF(int a) { return a % 4 == 0; }
bool myFun(int i, int j) {}
const int MAX = 1e6;
int sum[MAX + 9];
int freq[MAX + 9];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  double sum = 0;
  cin >> n;
  long long arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i], freq[arr[i]]++, sum += arr[i];
  long long q;
  cin >> q;
  while (q--) {
    long long b, c;
    cin >> b >> c;
    sum -= freq[b] * b;
    sum += freq[b] * c;
    freq[c] += freq[b];
    freq[b] = 0;
    cout << sum << '\n';
  }
  return 0;
}
