As part of a new curriculum reform, the CS department has decided to censor its course texts. In this problem you must help the department by writing a program that eliminates from an input text every occurrence of the words in a filter set.
Formally, a word w can be removed from a string s whenever w is a substring of s (the characters of w appear consecutively in s). Given a text string s and a set T of filter words, return the length of the shortest string that can result from iteratively removing words of T from s. Each word in T may be removed any number of times, and removing one word can make new occurrences appear.
The input contains multiple test cases. Each test case begins with an integer n (1≤n≤50), the size of the filter set T, followed by the text string s, followed by the n filter words t1,…,tn. The text string and every filter word consist only of the lowercase letters 'a' through 'z' and have length between 1 and 50. All filter words within a test case are distinct. Input is terminated by a line containing a single 0, which must not be processed.
For each test case, print a single integer: the minimum possible length of the resulting string.
[…] marks the word removed at each step, and ∅ denotes the empty string.
c[cde]defcde → [cde]fcde → f[cde] → f[aa]baab → [ba]ab → [ab] → ∅[aa]baab → b[aa]b → [bb] → ∅