// D - Replacing

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

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

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

let now = A;

const ansers: string[] = [];

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

  ansers.push(String(now.reduce((prev, current) => prev + current)));
});

console.log(ansers.join('\n'));
