Please can anyone explain approach for this problem?

https://www.codechef.com/ICPCIN19/problems/PENS

ig you can just try using greedy, using the pen that has the longest of the remaining word’s prefix in it and then switching to next choosing by the same method.

Yeah but greedy solution don’t always work. If there is a way to prove the greedy solution then please share.