#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const double Pi = acos(-1);
using namespace std;
int debug = 01;
long long c[1000010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, x, y;
  long long sum = 0;
  cin >> n;
  while (n--) {
    cin >> x;
    c[x]++;
    sum += x;
  }
  cin >> n;
  while (n--) {
    cin >> x >> y;
    sum -= x * c[x];
    sum += y * c[x];
    c[y] += c[x];
    c[x] = 0;
    cout << sum << "\n";
  }
}
