But do you need to ignore the exception? Can’t you re-throw it? or throw another? At least then you’d know something was not as you expected.
Length of all words <10^6 tells you how many trie elements you need.
The output format looks ok to me. Are you removing the line feed after the final test case? There’s no need to.