Kira likes to play with strings very much. Moreover he likes the shape of 'W' very much. He takes a string and try to make a 'W' shape out of it such that each angular point is a '#' character and each sides has same characters. He calls them W strings.
For example, the W string can be formed from "aaaaa#bb#cc#dddd" such as:
a
a             d
a     #     d
a   b c   d
a b   c d
#     #

He also call the strings which can generate a 'W' shape (satisfying the above conditions) W strings.
More formally, a string S is a W string if and only if it satisfies the following conditions (some terms and notations are explained in Note, please see it if you cannot understand):
- The string S contains exactly 3 '#' characters. Let the indexes of all '#' be P1 < P2 < P3 (indexes are 0-origin).
- Each substring of S[0, P1−1], S[P1+1, P2−1], S[P2+1, P3−1], S[P3+1, |S|−1] contains exactly one kind of characters, where S[a, b] denotes the non-empty substring from a+1th character to b+1th character, and |S| denotes the length of string S (See Note for details).
Now, his friend Ryuk gives him a string S and asks him to find the length of the longest W string which is a subsequence of S, with only one condition that there must not be any '#' symbols between the positions of the first and the second '#' symbol he chooses, nor between the second and the third (here the "positions" we are looking at are in S), i.e. suppose the index of the '#'s he chooses to make the W string are P1, P2, P3 (in increasing order) in the original string S, then there must be no index i such that S[i] = '#' where P1 < i < P2 or P2 < i < P3.
Help Kira and he won't write your name in the Death Note.
Note:
For a given string S, let S[k] denote the k+1th character of string S, and let the index of the character S[k] be k. Let |S| denote the length of the string S. And a substring of a string S is a string S[a, b] = S[a] S[a+1] ... S[b], where 0 ≤ a ≤ b < |S|. And a subsequence of a string S is a string S[i0] S[i1] ... S[in−1], where 0 ≤ i0 < i1 < ... < in−1 < |S|.
For example, let S be the string "kira", then S[0] = 'k', S[1] = 'i', S[3] = 'a', and |S| = 4. All of S[0, 2] = "kir", S[1, 1] = "i", and S[0, 3] = "kira" are substrings of S, but "ik", "kr", and "arik" are not. All of "k", "kr", "kira", "kia" are subsequences of S, but "ik", "kk" are not.
From the above definition of W string, for example, "a#b#c#d", "aaa#yyy#aaa#yy", and "o#oo#ooo#oooo" are W string, but "a#b#c#d#e", "#a#a#a", and "aa##a#a" are not.

-----Input-----
First line of input contains an integer T, denoting the number of test cases. Then T lines follow. Each line contains a string S.

-----Output-----
Output an integer, denoting the length of the longest W string as explained before. If S has no W string as its subsequence, then output 0.

-----Constraints-----

- 1 ≤ T ≤ 100 
- 1 ≤ |S| ≤ 10000 (104)
- S contains no characters other than lower English characters ('a' to 'z') and '#' (without quotes)

-----Example-----
Input:
3
aaaaa#bb#cc#dddd
acb#aab#bab#accba
abc#dda#bb#bb#aca

Output:
16
10
11

-----Explanation-----
In the first case: the whole string forms a W String.
In the second case: acb#aab#bab#accba, the longest W string is acb#aab#bab#accba
In the third case: abc#dda#bb#bb#aca, note that even though abc#dda#bb#bb#aca (boldened characters form the subsequence) is a W string of length 12, it violates Ryuk's condition that there should not be any #'s inbetween the 3 chosen # positions. One correct string of length 11 is abc#dda#bb#bb#aca

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = 'def frequency(s,n):\n    f=[[0 for i in range(26)]for j in range(n+1)]\n    count=0\n    for i in range(n):\n        if s[i]!="#":\n            f[count][ord(s[i])-97]+=1\n        else:\n            count+=1\n            for j in range(26):\n                f[count][j]=f[count-1][j]\n    return (f,count)\n    \ndef solve(s):\n    n=len(s)\n    f,count=frequency(s,n)\n    if count<3:\n        return 0\n    ans=0\n    index=[]\n    for i in range(n-1,-1,-1):\n        if s[i]=="#":\n            index.append(i)\n    \n    for c in range(1,count-2+1):\n        if index[-2]==index[-1]+1 or index[-3]==index[-2]+1:\n            index.pop()\n            continue\n        left=max(f[c-1])\n        mid1=0\n        mid2=0\n        right=0\n        for j in range(26):\n            mid1=max(mid1,f[c][j]-f[c-1][j])\n            mid2=max(mid2,f[c+1][j]-f[c][j])\n            right=max(right,f[count][j]-f[c+1][j])\n        if left and mid1 and mid2 and right:\n            ans=max(ans,left+mid1+mid2+right)\n        index.pop()\n    return ans\n    \nfor _ in range(int(input())):\n    s=input()\n    ans=solve(s)\n    if ans:\n        print(ans+3)\n    else:\n        print(0)\n        \n        '
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 13616