'use strict';
function Main(stdin){
  const input = stdin.trim().split(/\r?\n/g);
  const N = Number(input.shift());
  let A = input.shift();
  //const A = input.shift().split(' ').map(str => Number(str));
  const Q = Number(input.shift());
  const BC = input.map(row => row.split(' '));
  let tmp = [];
  let result = [];

  for(let i = 0; i < Q; i++){
      let bf = BC[i][0]
      A = A.replace(new RegExp(bf, 'g'), BC[i][1]);
      tmp = A.split(' ').map(str => Number(str));
      result.push(tmp.reduce(function(a, x){return a + x;}));
  }
  console.log(result.join('\n'))
}

// 固定
Main(require("fs").readFileSync("/dev/stdin", "utf8"));