console.log(function(args){
  const [[N],A,[Q],...tmp] = args.trim().split('\n').map( r=>r.split(' ').map( v=>v|0 ) );
  const sum = Array(Q).fill(0)
  const a = new Map();
  for ( let i = 0; i < N; i++ ) {
    a.set(A[i], (a.get(A[i])|0) + 1 );
  }
  for ( let i = 0; i < Q; i++ ) {
    const [B,C] = tmp[i];
    a.set( C, (a.get(C)|0) + (a.get(B)|0) );
    a.delete( B );
    for ( let [k,v] of a ) {
      sum[i] += k * v;
    }
  }
  return sum.join('\n');
}(require('fs').readFileSync('/dev/stdin', 'utf8')));
