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 = Array(100000).fill(0);
  for ( let i = 0; i < N; i++ ) {
    a[A[i]]++;
  }
  for ( let i = 0; i < Q; i++ ) {
    const [B,C] = tmp[i];
    a[C] += a[B];
    a[B] = 0;
    for ( let j = 1; j < 10000; j++ ) {
      if ( a[j] ) sum[i] += j * a[j];
    }
  }
  return sum.join('\n');
}(require('fs').readFileSync('/dev/stdin', 'utf8')));