#include<iostream>
#include<map>
using namespace std;

int main(){
  int N;  cin >> N;
  map<int,int>A;
  long long sum = 0;
  for(int i=0;i<N;i++){
    int a;
    cin >> a;
    A[a]++;
    sum += a;
  }
  int Q;  cin >> Q;
  for(int i=0;i<Q;i++){
    int B,C;  cin >> B >> C;
    sum += A[B]*(C - B);
    A[C] += A[B];
    A[B] = 0;
    cout << sum << endl;
  }
}