NOI Contest Archive

Archive / 2019 / Selection Test

King Rawana’s Palace

Medium · National Olympiad in Informatics Sri Lanka - Screening Test

Kamal is a historian looking for King Rawana’s Palace. According to the ancient texts, there are said to be an N number of gates, each protected by a puzzle. Kamal has found the first entrance and is asking for your help to figure out how to solve these puzzles.

In each gate there is a grid of 2 columns and R rows. The first row has two numbers S1 and S2 in the first and second column respectively. The numbers in all other rows is the next greater natural number to the number directly above.

On top of each gate there are 2 numbers, D1 & D2. Near the gate there are 4 piles of puzzle pieces. Each puzzle piece has a word on it. According to the ancient text you must fill each cell with a puzzle piece based on the following rules.

  1. If the number in the cell is divisible by D1, it should be filled with a piece from the first pile.
  2. If the number in the cell is divisible by D2, it should be filled with a piece from the second pile.
  3. If the number in the cell is divisible by both D1 & D2, it should be filled with a piece from the third pile.
  4. If the cell cannot be filled with a piece from the first 3 piles, it should be filled with a piece from the fourth pile.

Given the 5 numbers, S1, S2, R, D1 and D2 and the words in each 4 types of puzzle pieces, you must write a program to print the solution to the puzzle.

Input Format

First line contains a single integer, N. N lines follow, each with 5 integers (S1, S2, R, D1, D2 respectively), followed by a space and 4 words, each separated by a space (words on the puzzle pieces on each pile, respectively).

Output Format

For each gate print R lines - the solution for the puzzle. Each line should contain two space separated words.

Constraints

Sample Input 0

3
1 45 5 3 5 Fizz Buzz FizzBuzz Nil
4 13 10 2 7 Ba Bi Be Bu
49 23 5 5 10 Oong Greeng Kattu Eswah

Sample Output 0

Nil FizzBuzz
Nil Nil
Fizz Nil
Nil Fizz
Buzz Nil
Ba Bu
Bu Be
Ba Bu
Bi Ba
Ba Bu
Bu Ba
Ba Bu
Bu Ba
Ba Bi
Bu Ba
Eswah Eswah
Kattu Eswah
Eswah Oong
Eswah Eswah
Eswah Eswah