Scary Subsequences
시간 제한1초메모리 제한512 MB
세 고정 문자열 x, y, z와 이들을 모두 부분열로 포함하는 더 긴 문자열 s가 주어질 때, x, y, z 모두의 부분열이 아닌 s의 가장 짧은 부분열의 길이를 구한다.
문제
In this problem, all strings consist only of four characters: a, b, c, and d.
Busy Beaver has three strings , , and that he's really scared of. In particular, he only likes strings that are not a subsequence of any of them.
Answer queries. In query , you are given a string , such that , , and are all subsequences of and . Help Busy Beaver find the length of the shortest subsequence of that is not a subsequence of any of , , or .
1A string is a subsequence of a string if can be obtained from by deleting some (possibly none or all) characters from , without reordering the remaining characters.
입력
The first three lines of the input contain the strings , , and (), consisting of characters a, b, c, d.
The next line contains a single positive integer ().
The next lines each contain a string, the -th of which is (), consisting of characters a, b, c, d such that has , , and as subsequences.
The sum of over all queries does not exceed .
출력
On the -th line, output a single positive integer --- the shortest possible length of a subsequence of that is not a subsequence of any of , , or .
힌트
In the first query, all length subsequences of abcbc are subsequences of one of , , or , but the subsequence cb is not, so the answer is .
In the second query, d is a subsequence of dabcabc that is not a subsequence of any of , , or .
In the third query, bbc is a subsequence of abbcc that is not a subsequence of any of , , or , and it is the shortest such subsequence.