#include<bits/stdc++.h>
using namespace std;
long long b[1000000];
int main()
{
  long long n, i, x, s = 0, mx = 0, q, l, r, y;
  cin >> n;
  long long a[n + 3];
  for (i = 1; i <= n; i++)
  {
    cin >> a[i];
    b[a[i]]++;
    s += a[i];
  }
  cin >> q;
  while (q--)
  {
    cin >> l >> r;
      s += b[l] * (r-l);
      b[r] += b[l];
      b[l]=0;
    cout << s << endl;
  }
}