#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000009;
int A[1000010]{0};
int main() {
  int N;
  cin >> N;
  ll res = 0;
  map<int, int> A;
  for (int i = 0; i < N; ++i) {
    int el;
    cin >> el;
    A[el]++;
    res += el;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int B, C;
    cin >> B >> C;
    ll ans = res - B * A[B] + C * A[C];
    cout << ans << endl;
    A[B] = 0;
    A[C] += A[B];
  }
  return 0;
}
