process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function dogTeam(animals, index) { return animals[index] === 'DM'; } function catTeam(animals, index) { return animals[index] === 'CE'; } function deliver(zoos, t, s, d, threshold, delivered, position, cargo = []) { const filteredCargo = []; const currentZoo = zoos[position]; for (let i = 0, l = cargo.length; i < l; i++) { const animal = cargo[i]; if (currentZoo.destination.includes(animal)) { delivered++; } else { filteredCargo.push(animal); } } if (delivered >= threshold) { return currentZoo.position; } if (position + 1 >= zoos.length) { // end of road return Infinity; } //if (delivered + filteredCargo.length + t.length - position - 1 < threshold) { // can't make it // return -1; //} const skipCurrentZoo = deliver(zoos, t, s, d, threshold, delivered, position + 1, filteredCargo); if (!currentZoo.source.length) { return skipCurrentZoo; } // Take new animals const speciman = filteredCargo[0]; const canTakeDogs = speciman === undefined || dogTeam(t, speciman); const canTakeCats = speciman === undefined || catTeam(t, speciman); let pickupDogs = Infinity; let pickupCats = Infinity; if (canTakeDogs) { const currentCargo = cargo.slice(0); let pickedAFew = false; for (let i = 0, l = currentZoo.source.length; i < l; i++) { const animal = currentZoo.source[i]; if (dogTeam(t, animal)) { currentCargo.push(animal); pickedAFew = true; } } if (pickedAFew) { pickupDogs = deliver(zoos, t, s, d, threshold, delivered, position + 1, currentCargo); } } if (canTakeCats) { const currentCargo = cargo.slice(0); let pickedAFew = false; for (let i = 0, l = currentZoo.source.length; i < l; i++) { const animal = currentZoo.source[i]; if (catTeam(t, animal)) { currentCargo.push(animal); pickedAFew = true; } } if (pickedAFew) { pickupCats = deliver(zoos, t, s, d, threshold, delivered, position + 1, currentCargo); } } return Math.min(pickupDogs, pickupCats, skipCurrentZoo); } /* function findCombinations(t, s, d, threshold) { const animals = {DM: [], CE: []}; for (let i = 0, l = t.length; i < l; i++) { animals[t[i]].push(i); } if (animals.DM.length < threshold && animals.CE.length < threshold) { return null; } const possibilities = []; if (animals.DM.length >= threshold) { possibilities.push(animals.DM); } if (animals.CE.length >= threshold) { possibilities.push(animals.CE); } return possibilities; } */ /* function minimumZoos(animals, s, d, threshold) { let delivered = 0; let zoo = 0; let index = -1; const sortedAnimals = animals.sort((a, b) => d[a] - d[b]); do { index++; const animalIndex = sortedAnimals[index]; zoo = d[animalIndex]; delivered++; } while (delivered < threshold); return zoo; } */ function groupByZoo(sources, destinations) { const zoos = {}; for (let animal = 0, l = sources.length; animal < l; animal++) { const source = sources[animal]; const destination = destinations[animal]; zoos[source] = zoos[source] || {position: source, source: [], destination: []}; zoos[destination] = zoos[destination] || {position: destination, source: [], destination: []}; zoos[source].source.push(animal); zoos[destination].destination.push(animal); } return Object.keys(zoos) .map(position => zoos[position]) .sort((a, b) => a.position - b.position); } function minimumZooNumbers(m, n, t, s, d) { // Return a list of length n consisting of the answers const minimums = []; let impossible = false; const zoos = groupByZoo(s, d); for (let x = 1; x <= n; x++) { if (impossible) { minimums.push(-1); continue; } const minimun = deliver(zoos, t, s, d, x, 0, 0, []); if (minimun > m) { impossible = true; minimums.push(-1); } else { minimums.push(minimun); } } return minimums; } const teams = { D: 'DM', M: 'DM', C: 'CE', E: 'CE' }; function main() { var cases = parseInt(readLine()); for(var a0 = 0; a0 < cases; a0++){ var m_temp = readLine().split(' '); var m = parseInt(m_temp[0]); var n = parseInt(m_temp[1]); t = readLine().split(' ').map(t => teams[t]); s = readLine().split(' '); s = s.map(num => Number(num)); d = readLine().split(' '); d = d.map(num => Number(num)); var result = minimumZooNumbers(m, n, t, s, d); console.log(result.join(" ")); } }