#include <bits/stdc++.h>
using namespace std;
int n, q, a, b, c;
unsigned long long s = 0;
int main() {
  int v[1000000];
  cin >> n;
  int m = 1000000, x = -1;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    v[a]++;
    if (a < m) m = a;
    if (a > x) x = a;
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> b >> c;
    m = min(min(b, c), m);
    x = max(max(b, c), x);
    v[c] += v[b];
    v[b] = 0;
    for (int i = m; i <= x; ++i) s += v[i] * i;
    cout << s << "\n";
    s = 0;
  }
}
