t = int(input())
for i in range(t):
    str1 = input()
    dic = {}
    maxD = -1
    for j in range(len(str1)):
        if str1[j] in dic.keys():
            maxD = max(maxD, j - dic[str1[j]] - 1)
        else:
            dic[str1[j]] = j
    print(maxD)

88be4f337c3502e7811841d9f6b40cc88c03be35
<mask token>

<mask token>
for i in range(t):
    str1 = input()
    dic = {}
    maxD = -1
    for j in range(len(str1)):
        if str1[j] in dic.keys():
            maxD = max(maxD, j - dic[str1[j]] - 1)
        else:
            dic[str1[j]] = j
    print(maxD)

t = int(input())
for i in range(t):
    str1 = input()
    dic = {}
    maxD = -1
    for j in range(len(str1)):
        if str1[j] in dic.keys():
            maxD = max(maxD, j - dic[str1[j]] - 1)
        else:
            dic[str1[j]] = j
    print(maxD)
