// D - Replacing

import { readFileSync } from 'fs';
const input = readFileSync('/dev/stdin', 'utf8');

const [, a, , ...bc] = input.split('\n');

// const N = BigInt(n);
const A = a.split(' ').map((s) => BigInt(s));
// const Q = BigInt(q);
const BC = bc.map((s) => s.split(' ').map((s) => BigInt(s)));

let now = A;

BC.forEach((value) => {
  const [B, C] = value;
  now = now.map((item) => (item === B ? C : item));

  const anser = String(now.reduce((prev, current) => prev + current));

  console.log(anser);
});
