#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0; i<(int)(n); i++)
int main(){
long N, ans=0; cin >> N; long A, freq[100001]={};
REP(i, N){ cin >> A; freq[A]++; ans+=A; }
long Q; cin >> Q; long B, C; 
REP(i, Q){ 
  cin >> B >> C; 
  ans-=freq[B]*B; ans+=freq[B]*C; 
  freq[C]+=freq[B]; freq[B]=0;
  cout << ans << "\n";
}
}
