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+1).fill(0);
  sum[0] = 0;
  const a = new Map();
  for ( let i = 0; i < N; i++ ) {
    a.set(A[i], (a.get(A[i])|0) + 1 );
    sum[0] += A[i];
  }
  for ( let i = 0; i < Q; i++ ) {
    const [B,C] = tmp[i];
    if ( a.has(B) ) {
      const b = a.get(B);
      const c = a.has(C) ? a.get(C) : 0;
      sum[i+1] = sum[i] + b*C - b*B;
      a.set( C, c + b );
      a.delete( B );
    } else sum[i+1] = sum[i];
  }
  return sum.slice(1).join('\n');
}(require('fs').readFileSync('/dev/stdin', 'utf8')));
