#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;
const int INF = 1e9;
ll pow2(ll a, int b) {
  if (b == 1) {
    return a;
  } else {
    return pow2(a, b - 1) * a;
  }
};
int main() {
  int n, q;
  cin >> n;
  map<int, int> a;
  int ans = 0;
  for (int i = 0; i < (n); ++i) {
    int test;
    cin >> test;
    a[test] += 1;
    ans += test;
  }
  cin >> q;
  for (int i = 0; i < (q); ++i) {
    int b, c;
    cin >> b >> c;
    ans = ans - a[b] * b + a[b] * c;
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
  }
}
