#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
const long long l_INF = 101010101000;
const int INF = 1010101010;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
int main() {
  int n;
  cin >> n;
  map<int, int> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    a[c] += a[b];
    a[b] = 0;
    long long ans = 0;
    for (auto x : a) {
      ans += x.first * x.second;
    }
    cout << ans << endl;
  }
}
