… the spell that appears first in the book is used.
Next N lines contain spells sorted by appearance in the spellbook.
Then you shouldn’t sort it by yourself. [edit: @simes tell it more subtly, sorry
]
One more example:
KULEK
2
KEK
UL
and I don’t know, the correct answer is KEK or empty string.
You can submit two version and SPOJ “tell” you what is correct.