t=int(input())
while t:
    t=t-1
    s=input()
    l=[]
    if len(s)==3:
        if '1' in s and '2' in s and '3' in s:
            print(3)
            continue
    for i in range(len(s)-1):
        for j in range(i+2,len(s)):
            ans=s[i:j]
            if '1' in ans and '2' in ans and '3' in ans:
                l.append(len(ans))
    if l==[]:
        print(0)
    else:
        print(min(l))


from collections import defaultdict

def smallest(s1, s2):
    assert s2 != ''
    d = defaultdict(int)
    nneg = [0]  # number of negative entries in d
    def incr(c):
        d[c] += 1
        if d[c] == 0:
            nneg[0] -= 1
    def decr(c):
        if d[c] == 0:
            nneg[0] += 1
        d[c] -= 1
    for c in s2:
        decr(c)
    minlen = len(s1) + 1
    j = 0
    for i in range(len(s1)):
        while nneg[0] > 0:
            if j >= len(s1):
                return minlen
            incr(s1[j])
            j += 1
        minlen = min(minlen, j - i)
        decr(s1[i])
    return minlen

t = int(input())
for i1 in range(t):
    x = '123'
    y = input()
    num = [0, 0, 0, 0]
    for i2 in y:
        temp = int(i2)
        num[temp] += 1
    if num[1] == 0 or num[2] == 0 or num[3] == 0:
        print(0)
    else:
        print(smallest(y, x))


def s_check(a):
    return len(set(a)) == 3

t = int(input())
for i1 in range(t):
    s = [int(i) for i in input()]
    pointer = [0, 1]
    advance = 1
    result = len(s) + 1
    if s_check(s) == 0:
        print(0)
        continue
    while True:
        if s_check(s[pointer[0]:pointer[1]]):
            if result > pointer[1] - pointer[0]:
                result = pointer[1] - pointer[0]
            advance = 0
        else:
            advance = 1
        pointer[advance] += 1
        if pointer[1] > len(s):
            break
    print(result)


t = int(input())
for i1 in range(t):
    s = [int(i) for i in input()]
    pointer = [0, 0]
    advance = 1
    result = len(s) + 1
    if len(set(s)) < 3:
        print(0)
        continue
    c = [0, 0, 0, 0]
    c[s[0]] += 1
    while True:
        if c[1] > 0 and c[2] > 0 and c[3] > 0:
            if result > pointer[1] - pointer[0] + 1:
                result = pointer[1] - pointer[0] + 1
            advance = 0
        else:
            advance = 1
        if pointer[1] >= len(s) - 1:
            break
        if advance == 1:
            pointer[1] += 1
            c[s[pointer[1]]] += 1
        else:
            c[s[pointer[0]]] -= 1
            pointer[0] += 1
    print(result)


from collections import defaultdict

def smallest(s1, s2):
    assert s2 != ''
    d = defaultdict(int)
    nneg = [0]  # number of negative entries in d
    def incr(c):
        d[c] += 1
        if d[c] == 0:
            nneg[0] -= 1
    def decr(c):
        if d[c] == 0:
            nneg[0] += 1
        d[c] -= 1
    for c in s2:
        decr(c)
    minlen = len(s1) + 1
    j = 0
    for i in range(len(s1)):
        while nneg[0] > 0:
            if j >= len(s1):
                return minlen
            incr(s1[j])
            j += 1
        minlen = min(minlen, j - i)
        decr(s1[i])
    return minlen

t = int(input())
for i1 in range(t):
    x = '123'
    y = input()
    print(smallest(y, x))


t = int(input())
for i1 in range(t):
    s = [int(i) for i in input()]
    pointer = [0, 0]
    advance = 1
    result = len(s) + 1
    if len(set(s)) < 3:
        print(0)
        continue
    c = [0, 0, 0, 0]
    c[s[0]] += 1
    while True:
        if c[1] > 0 and c[2] > 0 and c[3] > 0:
            if result > pointer[1] - pointer[0] + 1:
                result = pointer[1] - pointer[0] + 1
            advance = 0
        else:
            advance = 1
        if advance == 1 and pointer[1] >= len(s) - 1:
            break
        if advance == 1:
            pointer[1] += 1
            c[s[pointer[1]]] += 1
        else:
            c[s[pointer[0]]] -= 1
            pointer[0] += 1
    print(result)


z=int(input())
for h in range(z):
    s=list(map(int,input()))
    a = []
    d = {}
    flag1=0
    for i in range(len(s)):
        cnt = 0
        d[1] = 0
        d[2] = 0
        d[3] = 0
        flag2=0
        for j in range(i, len(s)):
            if s[j] == 1:
                d[1] += 1
            if s[j] == 2:
                d[2] += 1
            if s[j] == 3:
                d[3] += 1
            cnt += 1
            if d[1] >= 1 and d[2] >= 1 and d[3] >= 1:
                a.append(cnt)
                flag2=1
                break
            if d[1]==1 and d[2]==1 and d[3]==1:
                flag1=1
                flag2=1
                break
        if flag1==1:
            break
        if flag2==0:
            break
    if len(a) > 0:
        print(min(a))
    else:
        print(0)


z=int(input())
for h in range(z):
    s=list(map(int,input()))
    a = []
    d = {}
    for i in range(len(s)):
        cnt = 0
        d[1] = 0
        d[2] = 0
        d[3] = 0
        for j in range(i, len(s)):
            if s[j] == 1:
                d[1] += 1
            if s[j] == 2:
                d[2] += 1
            if s[j] == 3:
                d[3] += 1
            cnt += 1
            if d[1] >= 1 and d[2] >= 1 and d[3] >= 1:
                a.append(cnt)
                break

    if len(a) > 0:
        print(min(a))
    else:
        print(0)


from collections import defaultdict

def findSubString(str):
	n = len(str)
	min_len = 200000
	count,start=0,0
	curr_count = defaultdict(lambda: 0)
	for j in range(n):
		curr_count[str[j]] += 1

		if curr_count[str[j]] == 1:
			count += 1

		if count == 3:
		  #  print(curr_count)
		    while curr_count[str[start]]>1:
		        if curr_count[str[start]] > 1:
		            curr_count[str[start]] -= 1
		        start+=1
		  #  print(start)
		    len_window = j - start + 1
		  #  print("wind",len_window)
		    if min_len > len_window:
		        min_len = len_window
		        start_index = start

	return min_len
for i in range(int(input())):
    s=input()
    if set(s)!=set(['1','2','3']):print(0)
    else:
        print(findSubString(s))


from collections import defaultdict

def findSubString(str):
	n = len(str)
	min_len = 200000
	count,start=0,0
	curr_count = defaultdict(lambda: 0)
	for j in range(n):
		curr_count[str[j]] += 1

		if curr_count[str[j]] == 1:
			count += 1

		if count == 3:
		  #  print(curr_count)
		    while curr_count[str[start]]>1:
		        if curr_count[str[start]] > 1:
		            curr_count[str[start]] -= 1
		        start+=1
		  #  print(start)
		    len_window = j - start + 1
		  #  print("wind",len_window)
		    if min_len > len_window:
		        min_len = len_window
		        start_index = start

	return len(str[start_index: start_index + min_len])
for i in range(int(input())):
    s=input()
    if set(s)!=set(['1','2','3']):print(0)
    else:
        print(findSubString(s))


from collections import defaultdict

def findSubString(str):
	n = len(str)
	min_len = 200000
	dist_count = len(set([x for x in str]))
	count,start=0,0
	curr_count = defaultdict(lambda: 0)
	for j in range(n):
		curr_count[str[j]] += 1

		if curr_count[str[j]] == 1:
			count += 1

		if count == dist_count:
			while curr_count[str[start]] > 1:
				if curr_count[str[start]] > 1:
					curr_count[str[start]] -= 1
				start += 1

			len_window = j - start + 1
			if min_len > len_window:
				min_len = len_window
				start_index = start

	return len(str[start_index: start_index + min_len])
for i in range(int(input())):
    s=input()
    if set(s)!=set(['1','2','3']):print(0)
    else:
        print(findSubString(s))


def solve():
    n = input()
    l = list(n)
    check = [-1 , 0, 0, 0]
    for i in range(len(l)):
        check[int(l[i])]+=1
    if check[1] == 0 or check[2] ==0 or check[3] == 0:
        print(0)
    else:
        i, j = 0, 0
        count = [-1, 0, 0, 0]
        ans = len(n)
        while i < len(l) or j < len(l):
            if count[1] > 0 and count[2] > 0 and count[3] > 0:
                ans = min(ans, j-i)
                count[int(l[i])] -= 1
                i += 1
            elif (count[1] == 0 or count[2] == 0 or count[3] == 0) and j < len(l):
                count[int(l[j])] += 1
                j += 1
            else:
                break

        print(ans)

for _ in range(int(input())):
    solve()


def solve():
    n = input()
    l = list(n)
    check = [-1 , 0, 0, 0]
    for i in range(len(l)):
        check[int(l[i])]+=1
    if check[1] == 0 or check[2] ==0 or check[3] == 0:
        print(0)
    else:
        i, j = 0, 0
        count = [-1, 0, 0, 0]
        ans = len(n)
        while i < len(l) and j < len(l):
            if count[1] > 0 and count[2] > 0 and count[3] > 0:
                ans = min(ans, j-i)
                count[int(l[i])] -= 1
                i += 1
            elif count[1] == 0 or count[2] == 0 or count[3] == 0:
                count[int(l[j])] += 1
                j += 1
        print(ans)

for _ in range(int(input())):
    solve()


def solve():
    n = input()
    l = list(n)
    ans = float('inf')
    check = [-1 , 0, 0, 0]
    for i in range(len(l)):
        check[int(l[i])]+=1
    if check[1] == 0 or check[2] ==0 or check[3] == 0:
        print(0)
    elif check[1] == 1 and check[2] == 1 and check[3] == 1:
        print(3)
    else:
        i, j = 0, 0
        count = [-1, 0, 0, 0]
        while i < len(l) and j < len(l):
            if count[1] > 0 and count[2] > 0 and count[3] > 0:
                ans = min(ans, j-i)
                count[int(l[i])] -= 1
                i += 1
            elif count[1] == 0 or count[2] == 0 or count[3] == 0:
                count[int(l[j])] += 1
                j += 1
        print(ans)

for _ in range(int(input())):
    solve()


def f(s):
    ans = 300000
    ind = 0
    a = 0
    n = len(s)
    while a<n-2 and s[a]==s[a+1]:
        a+=1
    b = a+1
    while a<n-2 and b<n-1:
        while b<n-1 and s[b]==s[b+1]:
            b+=1
        if b<n-1 and s[a]!=s[b+1]:
            ans = min(ans,b+2-a)
            ind = 1
        a = b
        b = a+1
    if ind == 0:
        return 0
    else:
        return ans

t = int(input())
for i in range(t):
    print(f(input()))


def f(s):
    a = 0
    b = 0
    c = 0
    for i in s:
        if i == '1':
            a+=1
        elif i == '2':
            b+=1
        else:
            c+=1
    if a*b*c == 0:
        return 0
    x = 0
    while s[x] == s[x+1]:
        x+=1
    s = s[x:]
    y = 1
    while s[y] == s[y+1]:
        y+=1
    if s[0] == s[y+1]:
        return f(s[y:])
    elif f(s[y:]) == 0:
        return y+2
    else:
        return min(y+2,f(s[y:]))

t = int(input())
for i in range(t):
    print(f(input()))


def f(s):
    if len(s)<=2:
        return 0
    elif s[0] == s[1]:
        return f(s[1:])
    else:
        a = 0
        b = 0
        c = 0
        for i in s:
            if i == '1':
                a+=1
            elif i == '2':
                b+=1
            else:
                c+=1
        if a*b*c == 0:
            return 0
        else:
            ind = 1
            while s[ind]==s[ind+1]:
                ind+=1
            if s[0]==s[ind+1]:
                return f(s[ind:])
            elif f(s[ind:])==0:
                return ind+2
            else:
                return min(ind+2,f(s[ind:]))

t = int(input())
for i in range(t):
    print(f(input()))


import re
for t in range(int(input())):
    x=input()
    log=0
    for i in "123":
        if i not in x:
            log=1
            print(0)
            break
    if log==0:
        p=re.compile("12+3|21+3|13+2|23+1|31+2|32+1")
        print(min(len(b) for b in p.findall(x)))


import re
for t in range(int(input())):
    x=input()
    log=0
    for i in "123":
        if i not in x:
            log=1
            print(0)
            break
    if log==0:
        ans=len(x)
        p=re.compile("1+|2+|3+")
        for i in re.finditer(p,x):
            s=i.start()
            e=i.end()
            if s>0 and e<len(x):
                if x[s-1]!=x[e]:
                    l=e-s+2
                    if l<ans:
                        ans=l
        print(ans)


def ternary_string(str)
  s = []
  i = 0
  h = {"1"=>false, "2"=>false, "3"=>false}
  min_size = str.length + 1
  while i < str.length
    s << str[i]
    h[str[i]] = true
    fin = s.size - 1
    if s[0] == s[fin]
      while fin > 0
        if s[fin] == s[0]
          s.shift
        end
        fin -= 1
      end
    end
    if h.values.all?
      min_size = [min_size, s.size].min
    end
    i += 1
  end
  min_size > str.length ? 0 : min_size
end

test_cases = gets.chomp.to_i
i = 0
while i < test_cases
  st = gets.chomp
  p ternary_string(st)
  i += 1
end


def ternary_string(str)
  h = {"1"=>false, "2"=>false, "3"=>false}
  i = 0
  min_size = 0
  while i < str.length
    h[str[i]] = i
    if h.values.all?
      if min_size == 0
        min_size = h.values.max - h.values.min + 1
      else
        min_size = [min_size, h.values.max - h.values.min + 1].min
      end
    end
    i += 1
  end
  min_size
end

test_cases = gets.chomp.to_i
i = 0
while i < test_cases
  st = gets.chomp
  p ternary_string(st)
  i += 1
end


def ternary_string(str)
  s = []
  i = 0
  h = {"1"=>false, "2"=>false, "3"=>false}
  min_size = str.length + 1
  while i < str.length
    s << str[i]
    h[str[i]] = true
    fin = s.size - 1
    if s[0] == s[fin]
      while fin > 0
        if s[fin] == s[0]
          s.shift
        end
        fin -= 1
      end
    end
    min_size = [min_size, s.size].min if h.values.all?
#    p s
    i += 1
  end
  min_size > str.length ? 0 : min_size
end

test_cases = gets.chomp.to_i
i = 0
while i < test_cases
  st = gets.chomp
  puts ternary_string(st)
  i += 1
end


for _ in range(input()):
    dp={}
    s=raw_input()
    dp[1]=[]
    dp[2]=[]
    dp[3]=[]
    for i in range(len(s)):
        dp[int(s[i])].append(i)
    i=0
    j=0
    k=0
    val=10**18
    n1=len(dp[1])
    n2=len(dp[2])
    n3=len(dp[3])
    while i<n1 and j<n2 and k<n3:
        if dp[1][i]<dp[2][j] and dp[1][i]<dp[3][k]:
            val=min(val,max(dp[2][j],dp[3][k])-dp[1][i])
            i+=1
        elif dp[2][j]<dp[1][i] and dp[2][j]<dp[3][k]:
            val=min(val,max(dp[1][i],dp[3][k])-dp[2][j])
            j+=1
        else:
            val=min(val,max(dp[2][j],dp[1][i])-dp[3][k])
            k+=1
    if val!=10**18:
        print val+1
    else:
        print 0


import sys

t = int(sys.stdin.readline().strip())
for i in range(t):
	s = sys.stdin.readline().strip()
	start = 0
	end = 0
	acc = [0,0,0]
	ans = float('inf')
	while start < len(s) and end < len(s):
		if acc[0] and acc[1] and acc[2]:
			ans = min(ans, end-start)
			acc[int(s[start]) - 1] -= 1
			start += 1
		else:
			acc[int(s[end]) - 1] += 1
			end += 1
	while acc[0] and acc[1] and acc[2] and start < len(s):
		ans = min(ans, end-start)
		acc[int(s[start]) - 1] -= 1
		start += 1
	if ans == float('inf'):
		print(0)
	else:
		print(ans)


t = input()
while t > 0:
    t -= 1
    s = raw_input()
    len_s = len(s)
    last1 = s.find("1")
    last2 = s.find("2")
    last3 = s.find("3")
    min_len = 9999999999999999
    if last1 == -1 or last2 == -1 or last3 == -1:
        print 0
        continue
    while last1 != -1 and last2 != -1 and last3 != -1:
        min_ = min(last1, last2, last3)
        max_ = max(last1, last2, last3)
        diff = max_ - min_
        if diff < min_len:
            min_len = diff
        if s[min_] == "1":
            last1 = s.find("1", last1 + 1)
        if s[min_] == "2":
            last2 = s.find("2", last2 + 1)
        if s[min_] == "3":
            last3 = s.find("3", last3 + 1)
    print min_len + 1


t = input()
while t > 0:
    t -= 1
    s = raw_input()
    # print type(s)
    ans = []
    len_s = len(s)
    last1 = s.find("1")
    last2 = s.find("2")
    last3 = s.find("3")
    min_len = 9999999999999999
    ans = 9999999999999999999
    if last1 == -1 or last2 == -1 or last3 == -1:
        print 0
        continue
    while last1 != -1 and last2 != -1 and last3 != -1:
        min_ = min(last1, last2, last3)
        max_ = max(last1, last2, last3)
        diff = max_ - min_
        if diff < min_len:
            min_len = diff
            ans = int(s[min_: max_ + 1])
        #elif diff == min_len:
        #    temp = int(s[min_: max_ + 1])
        #    if temp < ans:
        #        ans = temp
        if s[min_] == "1":
            last1 = s.find("1", last1 + 1)
        if s[min_] == "2":
            last2 = s.find("2", last2 + 1)
        if s[min_] == "3":
            last3 = s.find("3", last3 + 1)
    print min_len + 1


t = input()
while t > 0:
    t -= 1
    s = raw_input()
    # print type(s)
    ans = []
    len_s = len(s)
    last1 = s.find("1")
    last2 = s.find("2")
    last3 = s.find("3")
    min_len = 9999999999999999
    ans = 9999999999999999999
    if last1 == -1 or last2 == -1 or last3 == -1:
        print 0
        continue
    while last1 != -1 and last2 != -1 and last3 != -1:
        min_ = min(last1, last2, last3)
        max_ = max(last1, last2, last3)
        diff = max_ - min_
        # print last1, last2, last3
        if diff < min_len:
            min_len = diff
            # print min_, max_ + 1
            # print s[min_: max_ + 1]
            ans = int(s[min_: max_ + 1])
        elif diff == min_len:
            temp = int(s[min_: max_ + 1])
            if temp < ans:
                ans = temp
        if s[min_] == "1":
            last1 = s.find("1", last1 + 1)
        if s[min_] == "2":
            last2 = s.find("2", last2 + 1)
        if s[min_] == "3":
            last3 = s.find("3", last3 + 1)
    print min_len + 1


for _ in range(input()):
    a = input()
    b = list(str(a))
    if b.count('1')==0 or b.count('2')==0 or b.count('3')==0 :
        print 0
        continue
    p = q = r = -1
    ans = 10**6
    for i in range(len(b)):
        if p!=-1 and q!=-1 and r!=-1:
            l = min(p,min(q,r))
            m = max(p,max(q,r))
            ans = min(ans,m-l+1)
        if b[i]=='1':
            p = i
        elif b[i]=='2':
            q = i
        elif b[i]=='3':
            r = i
    if p!=-1 and q!=-1 and r!=-1:
            l = min(p,min(q,r))
            m = max(p,max(q,r))
            ans = min(ans,m-l+1)
    print ans


for _ in range(input()):
    b = raw_input()
    '''
    if b.count('1')==0 or b.count('2')==0 or b.count('3')==0 :
        print 0
        continue
    '''
    p = q = r = -1
    ans = 10**7
    for i in range(len(b)):
        if p!=-1 and q!=-1 and r!=-1:
            l = min(p,min(q,r))
            m = max(p,max(q,r))
            ans = min(ans,m-l+1)
        if b[i]=='1':
            p = i
        elif b[i]=='2':
            q = i
        elif b[i]=='3':
            r = i
    if p!=-1 and q!=-1 and r!=-1:
            l = min(p,min(q,r))
            m = max(p,max(q,r))
            ans = min(ans,m-l+1)
    if ans==10**7:
        ans = 0
    print ans


for _ in range(input()):
    a = input()
    b = list(str(a))
    if b.count('1')==0 or b.count('2')==0 or b.count('3')==0 :
        print 0
        continue
    p = q = r = -1
    for i in range(len(b)):
        if p!=-1 and q!=-1 and r!=-1:
            break
        if b[i]=='1':
            p = i
        elif b[i]=='2':
            q = i
        elif b[i]=='3':
            r = i
    if p!=-1 and q!=-1 and r!=-1:
            l = min(p,min(q,r))
            m = max(p,max(q,r))
            print m-l+1


#!python2

def arr_in():
    return [int(i) for i in raw_input().split()]

def zip_str(s):
    ret = []
    ch, c = s[0], 1
    for i in range(1, len(s)):
        if s[i] != ch:
            ret.append((ch, c))
            ch = s[i]
            c = 1
        else:
            c += 1
    ret.append((ch, c))
    return ret

for _ in range(input()):
    s = raw_input()
    a = zip_str(s)

    mi = 200001
    for j in range(2, len(a)):
        if a[j-2][0] + a[j-1][0] + a[j][0] not in ('123', '132', '231', '213', '321', '312'):
            continue
        mi = min(mi, a[j-1][1] + 2)

    if mi != 200001:
        print mi
    else:
        print 0


#!python2

def arr_in():
    return [int(i) for i in raw_input().split()]

for _ in range(input()):
    s = 'x' + raw_input()
    pre = [0, 0, 0]
    mi = 200010
    for i in range(1, len(s)):
        pre[ord(s[i]) - 49] = i
        if all(pre):
            mi = min(mi, i - min(pre) + 1)
    if mi != 200010:
        print mi
    else:
        print 0


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <string.h>

int main()
{
    int test;
    int one;
    int two;
    int three;
    char prev;
    int start;
    int min;
    char *S=(char *)malloc(sizeof(char)*(2*100000+10));
    scanf("%d",&test);
    for(int t=0;t<test;t++)
    {
        scanf("%s",S);
        one=0;
        two=0;
        three=0;
        prev=S[0];
        start=0;
        min=INT_MAX;
        for(int i=1;S[i]!='\0';i++)
        {
            if(S[i]=='1')
            {
                if(prev!=S[i])
                {
                    prev=S[i];
                    if(start!=0 && ((S[start-1]=='2' && S[i-1]=='3') || (S[start-1]=='3' && S[i-1]=='2')))
                    {
                        if(i-start+2<min)
                        {
                            min=i-start+2;
                        }
                    }
                    start=i;
                }
            }
            else if(S[i]=='2')
            {
                if(prev!=S[i])
                {
                    prev=S[i];
                    if(start!=0 && ((S[start-1]=='1' && S[i-1]=='3') || (S[start-1]=='3' && S[i-1]=='1')))
                    {
                        if(i-start+2<min)
                        {
                            min=i-start+2;
                        }
                    }
                    start=i;

                }
            }
            else
            {
                if(prev!=S[i])
                {
                    prev=S[i];
                    if(start!=0 && ((S[start-1]=='1' && S[i-1]=='2') || (S[start-1]=='2' && S[i-1]=='1')))
                    {
                        if(i-start+2<min)
                        {
                            min=i-start+2;
                        }
                    }
                    start=i;
                }
            }
        }
        if(min==INT_MAX)
        {
            printf("0\n");
        }
        else
        {
            printf("%d\n",min);
        }

    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define 	N 	200000

struct info {
	int *a;
	int c;
} pos[4];

char s[N];

void rlc (int i, int c) {
	pos[i].a = (int *) realloc (pos[i].a, c * sizeof *pos[i].a);
}

void push (int n, int i) {
	int c;
	c = pos[n].c += 1;
	rlc (n, c);
	pos[n].a[c - 1] = i;
}

int l_b (int i, int n, int x) {

    int l = 0;
    int h = n;
    while (l < h){

        int m = l + (h - l) / 2;
        if (x <= pos[i].a[m]){
            h = m;
        }
        else{
            l = m + 1;
        }
    }
    return l;
}

int main () {
	int t;
	scanf ("%d\n", &t);

	while (t--) {

		for (int i = 1; i <= 3; i++) {
			pos[i].c = 0;
			rlc (i, 0);
		}

		int i, j, len;
		scanf ("%s", s);
		len = strlen (s);
		for (i = 0; i < len; i++) {
			if (s[i] == '1') {
				push (1, i);
			}
			else if (s[i] == '2') {
				push (2, i);
			}
			else {
				push (3, i);
			}
		}

		if (pos[1].c == 0 || pos[2].c == 0 || pos[3].c == 0) {
			printf("0\n");
			continue;
		}

		int one, two, three, ans, t, p;
		ans = len;
		for (i = 1; i <= 3; i++) {
			int n = pos[i].c;
			for (j = 0; j < pos[i].c; j++) {
				p = pos[i].a[j];
				if (i == 1) {
					one = p;
					t = l_b (2, pos[2].c, one);
					if (t == pos[2].c) {
						continue;
					}
					two = pos[2].a[t];
					t = l_b (3, pos[3].c, two);
					if (t == pos[3].c) {
						continue;
					}
					three = pos[3].a[t];
					if (three - one + 1 < ans) {

						ans = three - one + 1;
					}

					t = l_b (3, pos[3].c, one);
					if (t == pos[3].c) {
						continue;
					}
					three = pos[3].a[t];
					t = l_b (2, pos[2].c, three);
					if (t == pos[2].c) {
						continue;
					}
					two = pos[2].a[t];
					if (two - one + 1 < ans) {
						ans = two - one + 1;
					}
				}
				else if (i == 2) {
					two = p;
					t = l_b (1, pos[1].c, two);
					if (t == pos[1].c) {
						continue;
					}
					one = pos[1].a[t];
					t = l_b (3, pos[3].c, one);
					if (t == pos[3].c) {
						continue;
					}
					three = pos[3].a[t];
					if (three - two + 1 < ans) {
						ans = three - two + 1;
					}

					t = l_b (3, pos[3].c, two);
					if (t == pos[3].c) {
						continue;
					}
					three = pos[3].a[t];
					t = l_b (1, pos[1].c, three);
					if (t == pos[1].c) {
						continue;
					}
					one = pos[1].a[t];
					if (one - two + 1 < ans) {
						ans = one - two + 1;
					}
				}
				else {
					three = p;
					t = l_b (2, pos[2].c, three);
					if (t == pos[2].c) {
						continue;
					}
					two = pos[2].a[t];
					t = l_b (1, pos[1].c, two);
					if (t == pos[1].c) {
						continue;
					}
					one = pos[1].a[t];
					if (one - three + 1 < ans) {
						ans = one - three + 1;
					}

					t = l_b (1, pos[1].c, three);
					if (t == pos[1].c) {
						continue;
					}
					one = pos[1].a[t];
					t = l_b (2, pos[2].c, one);
					if (t == pos[2].c) {
						continue;
					}
					two = pos[2].a[t];
					if (two - three + 1 < ans) {
						ans = two - three + 1;
					}
				}
			}
		}
		printf("%d\n", ans);
	}

}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define 	N 	200000

struct info {
	int *a;
	int c;
} pos[4];

char s[N];

void rlc (int i, int c) {
	pos[i].a = (int *) realloc (pos[i].a, c * sizeof *pos[i].a);
}

void push (int n, int i) {
	int c;
	c = pos[n].c += 1;
	rlc (n, c);
	pos[n].a[c - 1] = i;
}

int l_b (int i, int n, int x) {

    int l = 0;
    int h = n;
    while (l < h){

        int m = l + (h - l) / 2;
        if (x <= pos[i].a[m]){
            h = m;
        }
        else{
            l = m + 1;
        }
    }
    return l;
}

int main () {
	int t;
	scanf ("%d\n", &t);

	while (t--) {

		for (int i = 0; i < 4; i++) {
			pos[i].c = 0;
		}

		int i, j, len;
		scanf ("%s", s);
		len = strlen (s);
		for (i = 0; i < len; i++) {
			if (s[i] == '1') {
				push (1, i);
			}
			else if (s[i] == '2') {
				push (2, i);
			}
			else {
				push (3, i);
			}
		}

		if (pos[1].c == 0 || pos[2].c == 0 || pos[3].c == 0) {
			printf("0\n");
			continue;
		}

		int one, two, three, ans, t, p;
		ans = len;
		for (i = 1; i <= 3; i++) {
			int n = pos[i].c;
			for (j = 0; j < pos[i].c; j++) {
				p = pos[i].a[j];
				if (i == 1) {
					one = p;
					t = l_b (2, pos[2].c, one);
					if (t != pos[2].c) {
						two = pos[2].a[t];
						t = l_b (3, pos[3].c, two);
						if (t != pos[3].c) {
							three = pos[3].a[t];
							if (three - one + 1 < ans) {
								ans = three - one + 1;
							}
						}
					}

					t = l_b (3, pos[3].c, one);
					if (t != pos[3].c) {
						three = pos[3].a[t];
						t = l_b (2, pos[2].c, three);
						if (t != pos[2].c) {
							two = pos[2].a[t];
							if (two - one + 1 < ans) {
								ans = two - one + 1;
							}
						}
					}
				}
				else if (i == 2) {
					two = p;
					t = l_b (1, pos[1].c, two);
					if (t != pos[1].c) {
						one = pos[1].a[t];
						t = l_b (3, pos[3].c, one);
						if (t != pos[3].c) {
							three = pos[3].a[t];
							if (three - two + 1 < ans) {
								ans = three - two + 1;
							}
						}
					}

					t = l_b (3, pos[3].c, two);
					if (t != pos[3].c) {
						three = pos[3].a[t];
						t = l_b (1, pos[1].c, three);
						if (t != pos[1].c) {
							one = pos[1].a[t];
							if (one - two + 1 < ans) {
								ans = one - two + 1;
							}
						}
					}
				}
				else {
					three = p;
					t = l_b (2, pos[2].c, three);
					if (t != pos[2].c) {
						two = pos[2].a[t];
						t = l_b (1, pos[1].c, two);
						if (t != pos[1].c) {
							one = pos[1].a[t];
							if (one - three + 1 < ans) {
								ans = one - three + 1;
							}
						}
					}

					t = l_b (1, pos[1].c, three);
					if (t != pos[1].c) {
						one = pos[1].a[t];
						t = l_b (2, pos[2].c, one);
						if (t != pos[2].c) {
							two = pos[2].a[t];
							if (two - three + 1 < ans) {
								ans = two - three + 1;
							}
						}
					}
				}
			}
		}
		printf("%d\n", ans);

	}

}


#include<stdio.h>
#include<string.h>
char s[200005];
int min(int a, int b)
{
	return a < b ? a : b;
}
int main(void)
{
	int t;
	scanf("%d", &t);
	int len, flag;
	int Min;
	while (t--)
	{
		flag = 0;
		Min = 200005;
		scanf("%s", s, 200005);
		int p1 = 0;
		int p2;
		int len = (int)strlen(s);
		int flag2 = 0;
		for (int i = 1; i < len; i++)
		{
			if (flag == 0)
			{
				if (s[i] != s[p1])
				{
					flag = 1;
					p2 = i;
				}
				else
					p1 = i;
			}
			else
			{
				if (s[i] != s[p1] && s[i] != s[p2])
				{
					Min = min(Min, i - p1 + 1);
					flag2 = 1;
				}
				if (s[i] == s[p1])
				{
					int idenx;
					idenx = p2;
					p2 = p1;
					p1 = idenx;
				}
				if (s[i] = s[p2])
					p2 = i;
			}
		}
		if (flag2 == 1)
			printf("%d\n", Min);
		else
			printf("0\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
char s[200005];
int min(int a, int b)
{
	return a < b ? a : b;
}
int main(void)
{
	int t;
	scanf("%d", &t);
	int len, flag;
	int Min;
	while (t--)
	{
		flag = 0;
		Min = 200005;
		scanf("%s", s, 200005);
		int p1 = 0;
		int p2;
		int len = (int)strlen(s);
		int flag2 = 0;
		for (int i = 1; i < len; i++)
		{
			if (flag == 0)
			{
				if (s[i] != s[p1])
				{
					flag = 1;
					p2 = i;
				}
				else
					p1 = i;
			}
			else
			{
				if (s[i] != s[p1] && s[i] != s[p2])
				{
					Min = min(Min, i - p1 + 1);
					flag2 = 1;
					int ide;
					ide = p2;
					p2 = i;
					p1 = ide;
				}
				if (s[i] == s[p1])
				{
					int idenx;
					idenx = p2;
					p2 = p1;
					p1 = idenx;
				}
				if (s[i] = s[p2])
					p2 = i;
			}
		}
		if (flag2 == 1)
			printf("%d\n", Min);
		else
			printf("0\n");
	}
	return 0;
}


#include<stdio.h>
#include<conio.h>
#include<string.h>
int max(int a,int b,int c)
{
    if(a>=b&&a>=c)
    {
        return a;
    }
    else if(b>=a&&b>=c)
    {
        return b;
    }
    else
    {
        return c;
    }
}
int min(int a,int b,int c)
{
    if(a<=b&&a<=c)
    {
        return a;
    }
    else if(b<=a&&b<=c)
    {
        return b;
    }
    else
    {
        return c;
    }
}
int main()
{
    int t,i;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        int a=-1,b=-1,c=-1,k,l,j,s=200001,z,x,y;
        char S[200001];
        scanf("%s",&S);
        l=strlen(S);
        for(j=0;j<l;j++)
        {
            if(S[j]=='1')
            {
                a=j;
            }
            else if(S[j]=='2')
            {
                b=j;
            }
            else if(S[j]=='3')
            {
                c=j;
            }
            if(a>=0&&b>=0&&c>=0)
            {
                x=max(a,b,c);
                y=min(a,b,c);
                z=x-y+1;
                if(z<s)
                {
                    s=z;
                }
            }
        }
        if(s==200001)
        {
            printf("0\n");
        }
        else
        {
            printf("%d\n",s);
        }
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

int main()
{
	long long int t;
	scanf("%lld", &t);

	do
	{
		char s[20000];
		scanf("%s", &s);

		long long int min = INT_MAX;
		long long int freq[3];
		long long int i , j;
		for ( i = 0; i < 3; ++i )
		{
			freq[i] = 0;
		}
		long long int alfa = 0;
		for ( i = 0; i < strlen(s); ++i )
		{
			if ( s[i] == '1' )
			{
				freq[0] += 1;
			}
			else if ( s[i] == '2' )
			{
				freq[1] += 1;
			}
			else
			{
				freq[2] += 1;
			}
			long long int cnt = 0;
			for ( j = 0; j < 3; ++j )
			{
				if ( freq[j] > 0 )
				{
					++cnt;
				}
			}
			if ( cnt == 3 )
			{
				//printf("i es %lld\n", i);
				long long int suma = 0;
				for ( j = 0; j < 3; ++j )
				{
					suma += freq[j];
				}

				long long int index = 0;
				long long int aux = 0;
				do
				{
					if ( s[index] == '1' )
					{
						if ( freq[0] > 1 )
						{
							freq[0] -= 1;
							suma -= 1;
						}
						else
						{
							aux = 1;
						}
					}
					else if ( s[index] == '2' )
					{
						if ( freq[1] > 1 )
						{
							freq[1] -= 1;
							suma -= 1;
						}
						else
						{
							aux = 1;
						}
					}
					else
					{
						if ( freq[2] > 1 )
						{
							freq[2] -= 1;
							suma -= 1;
						}
						else
						{
							aux = 1;
						}
					}
					++index;
				} while ( aux == 0 );

				if ( suma < min )
				{
					min = suma;
					alfa = 1;
				}

				for ( j = 0; j < 3; ++j )
				{
					freq[j] = 1;
				}
			}
		}

		if ( alfa == 1 )
		{
			printf("%lld\n", min);
		}
		else
		{
			printf("0\n");
		}

		--t;
	}while ( t != 0 );
	//system("pause");
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

int main()
{
	long long int t;
	scanf("%lld", &t);

	do
	{
		char s[20000];
		scanf("%s", &s);
		long long int i, j;
		long long int st = 1;
		long long int fn = 1;
		long long int min = INT_MAX;
		long long int suma = 0;
		for ( i = 1; i < strlen(s)-1; ++i )
		{
			if ( s[i] == s[i+1] )
			{
				++fn;
				suma += 1;
			}
			else
			{
				if ( s[st-1] != s[fn+1] && s[st] != s[st-1] && s[st] != s[fn+1] )
				{
					//printf("st %lld fn %lld\n", st, fn);
					if ( min > suma )
					{
						min = suma;
					}
				}
				suma = 0;
				fn = i+1;
				st = i+1;
			}
		}
		if ( min != INT_MAX )
		{
			printf("%lld\n", min+3);
		}
		else
		{
			printf("0\n");
		}

		--t;
	} while ( t != 0 );
	//system("pause");
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

int main()
{
	long long int t;
	scanf("%lld", &t);

	do
	{
		char s[200000];
		scanf("%s", &s);
		long long int i, j;
		long long int st = 1;
		long long int fn = 1;
		long long int min = INT_MAX;
		long long int suma = 0;
		for ( i = 1; i < strlen(s)-1; ++i )
		{
			if ( s[i] == s[i+1] )
			{
				++fn;
				suma += 1;
			}
			else
			{
				if ( s[st-1] != s[fn+1] && s[st] != s[st-1] && s[st] != s[fn+1] )
				{
					//printf("st %lld fn %lld\n", st, fn);
					if ( min > suma )
					{
						min = suma;
					}
				}
				suma = 0;
				fn = i+1;
				st = i+1;
			}
		}
		if ( min != INT_MAX )
		{
			printf("%lld\n", min+3);
		}
		else
		{
			printf("0\n");
		}

		--t;
	} while ( t != 0 );
	//system("pause");
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

int main()
{
	long long int t;
	scanf("%lld", &t);

	do
	{
		char s[20000];
		scanf("%s", &s);

		long long int min = INT_MAX;
		long long int freq[3];
		long long int i , j;
		for ( i = 0; i < 3; ++i )
		{
			freq[i] = 0;
		}
		long long int alfa = 0;
		for ( i = 0; i < strlen(s); ++i )
		{
			if ( s[i] == '1' )
			{
				freq[0] += 1;
			}
			else if ( s[i] == '2' )
			{
				freq[1] += 1;
			}
			else
			{
				freq[2] += 1;
			}
			long long int cnt = 0;
			for ( j = 0; j < 3; ++j )
			{
				if ( freq[j] > 0 )
				{
					++cnt;
				}
			}
			if ( cnt == 3 )
			{
				//printf("i es %lld\n", i);
				long long int suma = 0;
				for ( j = 0; j < 3; ++j )
				{
					suma += freq[j];
				}

				long long int index = 0;
				long long int aux = 0;
				do
				{
					if ( s[index] == '1' )
					{
						if ( freq[0] > 1 )
						{
							freq[0] -= 1;
							suma -= 1;
						}
						else
						{
							aux = 1;
						}
					}
					else if ( s[index] == '2' )
					{
						if ( freq[1] > 1 )
						{
							freq[1] -= 1;
							suma -= 1;
						}
						else
						{
							aux = 1;
						}
					}
					else
					{
						if ( freq[2] > 1 )
						{
							freq[2] -= 1;
							suma -= 1;
						}
						else
						{
							aux = 1;
						}
					}
					++index;
				} while ( aux == 0 );

				if ( suma < min )
				{
					min = suma;
					alfa = 1;
				}
				/*
				for ( j = 0; j < 3; ++j )
				{
					freq[j] = 1;
				}
				*/
			}
		}

		if ( alfa == 1 )
		{
			printf("%lld\n", min);
		}
		else
		{
			printf("0\n");
		}

		--t;
	}while ( t != 0 );
	//system("pause");
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

int main()
{
	long long int t;
	scanf("%lld", &t);

	do
	{
		char s[20000];
		scanf("%s", &s);

		long long int min = INT_MAX;
		long long int freq[3];
		long long int i , j;
		for ( i = 0; i < 3; ++i )
		{
			freq[i] = 0;
		}
		long long int alfa = 0;
		for ( i = 0; i < strlen(s); ++i )
		{
			if ( s[i] == '1' )
			{
				freq[0] += 1;
			}
			else if ( s[i] == '2' )
			{
				freq[1] += 1;
			}
			else
			{
				freq[2] += 1;
			}
			long long int cnt = 0;
			for ( j = 0; j < 3; ++j )
			{
				if ( freq[j] > 0 )
				{
					++cnt;
				}
			}
			if ( cnt == 3 )
			{
				long long int suma = 0;
				for ( j = 0; j < 3; ++j )
				{
					suma += freq[j];
				}

				long long int index = 0;
				long long int aux = 0;
				do
				{
					if ( s[index] == '1' )
					{
						if ( freq[0] > 1 )
						{
							freq[0] -= 1;
							suma -= 1;
						}
						else
						{
							aux = 1;
						}
					}
					else if ( s[index] == '2' )
					{
						if ( freq[1] > 1 )
						{
							freq[1] -= 1;
							suma -= 1;
						}
						else
						{
							aux = 1;
						}
					}
					else
					{
						if ( freq[2] > 1 )
						{
							freq[2] -= 1;
							suma -= 1;
						}
						else
						{
							aux = 1;
						}
					}
					++index;
				} while ( aux == 0 );

				if ( suma < min )
				{
					min = suma;
					alfa = 1;
				}

				for ( j = 0; j < 3; ++j )
				{
					freq[j] = 0;
				}
			}
		}

		if ( alfa == 1 )
		{
			printf("%lld\n", min);
		}
		else
		{
			printf("0\n");
		}

		--t;
	}while ( t != 0 );
	//system("pause");
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	long long int t,i;cin>>t;
	while(t--){
		string s;
		cin>>s;
		long long int f1,f2,f3;f1=f2=f3=0;
		for(i=0;i<s.length();i++){
			if(s[i]=='1') f1=1;
			if(s[i]=='2') f2=1;
			if(s[i]=='3') f3=1;
		}
		if(f1&f2&f3){
			long long int c1,c2,c3;c1=c2=c3=-1;
			long long int ans=s.length();
			for(i=0;i<s.length();i++){
				if(s[i]=='1') c1=i;
				if(s[i]=='2') c2=i;
				if(s[i]=='3') c3=i;
				if(c1!=-1&&c2!=-1&&c3!=-1){
					ans=min(ans,max(c1,max(c2,c3))-min(c1,min(c2,c3))+1);
				}
			}
			cout<<ans<<endl;
		}
		else{
			cout<<"0"<<endl;
		}
	}
}


#include <stdio.h>
#include <string.h>
#define ll long long int
int diffee(int a,int b,int c);

    //     bool comparev(const pair<int,int> &a,const pair<int,int> &b){
    //          if((a.S-a.F)!=(b.S-b.F)){
    //            return ((a.S-a.F)>(b.S-b.F));
    //          }
    //          else{
    //            return (a.F<b.F);
    //          }
    //     };
    //    struct compares{
    //        bool operator() (const pair<int,int> &a,const pair<int,int> &b) const{
    //          if((a.S-a.F)!=(b.S-b.F)) return ((a.S-a.F)>(b.S-b.F));
    //          else return (a.F<b.F);
    //        }
    //    };

void Terminal(char *s)
{
	int c1=-1,c2=-1,c3=-1;
	int i,ans=200001;
	for(i=0;i<=strlen(s);i++)
	{
		if(c1==-1 || c2==-1 || c3==-1)
		{
			if(s[i]==49)
				c1=i;
			if(s[i]==50)
				c2=i;
			if(s[i]==51)
				c3=i;
		}
		else
		{
			if(ans>diffee(c1,c2,c3))
				ans=diffee(c1,c2,c3);
			if(s[i]==49)
				c1=i;
			if(s[i]==50)
				c2=i;
			if(s[i]==51)
				c3=i;
		}
	}
	if(ans==200001)
		printf("0\n");
	else
		printf("%d\n",ans);
}

int diffee(int a,int b,int c)
{
	int Max,Min;
	if(a>b && a>c)
		Max=a;
	else if(b>a && b>c)
		Max=b;
	else if(c>a && c>b)
		Max=c;
	if(a<b && a<c)
		Min=a;
	else if(b<a && b<c)
		Min=b;
	else if(c<a && c<b)
		Min=c;
	return Max-Min+1;
}
int main()
{
	int tlll;
	scanf("%d",&tlll);
	int ii;
	for(ii=0;ii<tlll;ii++)
	{
		char ss[200000];
		scanf("%s",ss);
		Terminal(ss);
	}
}


#include<stdio.h>
#include<string.h>
#include<limits.h>
char a[300000];
int main()
{
	int t,ans,i,length,start,end;
	scanf("%d",&t);
	while(t--)
	{
		ans=INT_MAX;
		scanf("%s",a);
		length=0,start=0;
		for(i=0;i<strlen(a);i++)
		{
			if(a[i]!='1' && length!=0)
			{
				end=i,length=0;
				if(a[start]!='1' && a[end]!=a[start])
					if(ans>end-start+1)
						ans=end-start+1;
			}
			if(a[i]!='1' && length==0)
				start=i;
			if(a[i]=='1')
				length++;
		}
		length=0,start=0;
		for(i=0;i<strlen(a);i++)
                {
                        if(a[i]!='2' && length!=0)
                        {
                                end=i,length=0;
                                if(a[start]!='2' && a[end]!=a[start])
                                        if(ans>end-start+1)
                                                ans=end-start+1;
                        }
                        if(a[i]!='2' && length==0)
                                start=i;
                        if(a[i]=='2')
                                length++;
                }
		length=0,start=0;
		for(i=0;i<strlen(a);i++)
                {
                        if(a[i]!='3' && length!=0)
                        {
                                end=i,length=0;
                                if(a[start]!='3' && a[end]!=a[start])
                                        if(ans>end-start+1)
                                                ans=end-start+1;
                        }
                        if(a[i]!='3' && length==0)
                                start=i;
                        if(a[i]=='3')
                                length++;
                }
		if(ans==INT_MAX)
			printf("0\n");
		else
			printf("%d\n",ans);
	}
}


#include <stdio.h>
char S[200002];
int main()
{
	int t;
	int T;
	int oneF;
	int twoF;
	int threeF;
	int max;
	int min;
	int R;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%s", &S);
		R = 200001;
		oneF = -1000001;
		twoF = -1000001;
		threeF = -1000001;
		for (t = 0; S[t]; t++)
		{
			switch(S[t])
			{
				case '1':
				{
					oneF = t;
					break;
				}
				case '2':
				{
					twoF = t;
					break;
				}
				case '3':
				{
					threeF = t;
					break;
				}
			}
			max = -1;
			min = 200001;
			if (oneF > twoF)
			{
				if (oneF > threeF)
				{
					max = oneF;
				}
				else
				{
					max = threeF;
				}
			}
			else
			{
				if (twoF > threeF)
				{
					max = twoF;
				}
				else
				{
					max = threeF;
				}
			}
			if (oneF < twoF)
			{
				if (oneF < threeF)
				{
					min = oneF;
				}
				else
				{
					min = threeF;
				}
			}
			else
			{
				if (twoF < threeF)
				{
					min = twoF;
				}
				else
				{
					min = threeF;
				}
			}
			if (max - min + 1 < R)
			{
				R = max - min + 1;
			}
		}
		if (R == 200001)
		{
			printf("0\n");
		}
		else
		{
			printf("%d\n", R);
		}
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int t;
    scanf("%d",&t);
    char s[200001];
    while(t--)
    {
        scanf("%s",s);
        int i=0,j=0,cnt[4]={0};
        int n=strlen(s);
        int ans=n;
        for(j=0;j<n;j++)
        {
            cnt[s[j]-'0']++;
            while(cnt[s[i]-'0']>1)
            {
                cnt[s[i]-'0']--;
                i++;
            }
            if(cnt[1]&cnt[2]&cnt[3])
            {
                if(ans>j-i+1)
                ans=j-i+1;

            }

        }
         if(ans==n)
            ans=0;
            printf("%d\n",ans);
    }
}


#include<stdio.h>
#include<string.h>
int main()
{
    int t;
    scanf("%d",&t);
    char s[200001];
    while(t--)
    {
        scanf("%s",s);
        int i=0,j=0,cnt[4]={0};
        int n=strlen(s);
        int ans=n;
        for(j=0;j<n;j++)
        {
            cnt[s[j]-'0']++;
            while(cnt[s[i]-'0']>1)
            {
                cnt[s[i]-'0']--;
                i++;
            }
            if(cnt[1]&cnt[2]&cnt[3])
            {
                if(ans>j-i+1)
                ans=j-i+1;

            }
            if(ans==n)
            ans=0;
            printf("%d\n",ans);
        }
    }
}


#include<stdio.h
#include<string.h>
int main()
{
    int t;
    scanf("%d",&t);
    char s[200001];
    while(t--)
    {
        scanf("%s",s);
        int i=0,j=0,cnt[4]={0};
        int n=strlen(s);
        int ans=n;
        for(j=0;j<n;j++)
        {
            cnt[s[j]-'0']++;
            while(cnt[s[i]-'0']>1)
            {
                cnt[s[i]-'0']--;
                i++;
            }
            if(cnt[1]&cnt[2]&cnt[3])
            {
                if(ans>j-i+1)
                ans=j-i+1;

            }
            if(ans==n)
            ans=0;
            printf("%d\n",ans);
        }
    }
}


#include <stdio.h>
#include <string.h>

int diff(int a,int b,int c)
{
	int Max,Min;
	if(a>b && a>c)
		Max=a;
	else if(b>a && b>c)
		Max=b;
	else if(c>a && c>b)
		Max=c;
	if(a<b && a<c)
		Min=a;
	else if(b<a && b<c)
		Min=b;
	else if(c<a && c<b)
		Min=c;
	return Max-Min+1;
}

void Ter(char *s)
{
	int c1=-1,c2=-1,c3=-1;
	int i,ans=200001;
	for(i=0;i<=strlen(s);i++)
	{
		if(c1==-1 || c2==-1 || c3==-1)
		{
			if(s[i]==49)
				c1=i;
			if(s[i]==50)
				c2=i;
			if(s[i]==51)
				c3=i;
		}
		else
		{
			if(ans>diff(c1,c2,c3))
				ans=diff(c1,c2,c3);
			if(s[i]==49)
				c1=i;
			if(s[i]==50)
				c2=i;
			if(s[i]==51)
				c3=i;
		}
	}
	if(ans==200001)
		printf("0\n");
	else
		printf("%d\n",ans);
}

int main()
{
	int t;
	scanf("%d",&t);
	int i;
	for(i=0;i<t;i++)
	{
		char s[200000];
		scanf("%s",s);
		Ter(s);
	}
}


#include <stdio.h>
#include <string.h>

int diff(int a,int b,int c)
{
	int Max,Min;
	if(a>b && a>c)
		Max=a;
	else if(b>a && b>c)
		Max=b;
	else if(c>a && c>b)
		Max=c;
	if(a<b && a<c)
		Min=a;
	else if(b<a && b<c)
		Min=b;
	else if(c<a && c<b)
		Min=c;
	return Max-Min+1;
}

void Ter(char *s)
{
	int c1=-1,c2=-1,c3=-1;
	int i,ans=200001;
	for(i=0;i<=strlen(s);i++)
	{
		if(c1==-1 || c2==-1 || c3==-1)
		{
			if(s[i]==49)
				c1=i;
			if(s[i]==50)
				c2=i;
			if(s[i]==51)
				c3=i;
		}
		else
		{
			if(ans>diff(c1,c2,c3))
				ans=diff(c1,c2,c3);
			if(s[i]==49)
				c1=i;
			if(s[i]==50)
				c2=i;
			if(s[i]==51)
				c3=i;
		}
	}
	if(ans==200001)
		printf("0\n");
	else
		printf("%d\n",ans);
}

int main()
{
	int t;
	scanf("%d",&t);
	int i;
	for(i=0;i<t;i++)
	{
		char *s;
		scanf("%s",s);
		Ter(s);
	}
}


#include<stdio.h>
#include<limits.h>
#include<string.h>
typedef long long int ll;
int diff(ll m1,ll m2,ll m3);
int main()
{
    ll t;
    scanf("%lld",&t);
    while(t--){
        ll ans=INT_MAX,i,m1=-1,m2=-1,m3=-1;
        char s[200000];
        scanf("%s",s);
        for(i=0;i<=strlen(s);i++){
            if(m1==-1 || m2==-1 || m3==-1){
                if(s[i]=='1')   m1=i;
                if(s[i]=='2')   m2=i;
                if(s[i]=='3')   m3=i;
            }
            else{
                if(ans>diff(m1,m2,m3))  ans = diff(m1,m2,m3);
                if(s[i]=='1')   m1=i;
                if(s[i]=='2')   m2=i;
                if(s[i]=='3')   m3=i;
            }
        }
        if(ans == INT_MAX)    printf("0\n");
        else    printf("%lld\n",ans);
    }
}
int diff(ll m1,ll m2,ll m3)
{
    ll min,max;
    if(m1>m2 && m1>m3)  max = m1;
    else if(m2>m1 && m2>m3) max = m2;
    else if(m3>m1 && m3>m2) max = m3;
    if(m1<m2 && m1<m3)  min = m1;
    else if(m2<m1 && m2<m3) min = m2;
    else if(m3<m1 && m3<m2) min = m3;
    return (max-min)+1;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define sum(x) (x[0]>0) +(x[1]>0) +(x[2]>0)

void solve()
{
    char s[200000];
    int hash[3]={0,0,0},n,left=0,right=0,min=200004;
    scanf("%s",s);
    n=strlen(s);
    while(1){
        if(sum(hash)<3){
            hash[s[right]-'1']++;
            right++;
        }
        else{
            hash[s[left]-'1']--;
            left++;
        }
        if(sum(hash)==3 && (right-left)<min){
            min=right-left;
        }
        if(right==n && sum(hash)<3){
            break;
        }
    }
    if(min==200004){
        printf("0\n");
    }
    else
        printf("%d\n",min);
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        solve();
    }
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int min(int a,int b)
{
    return a>b?a:b;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
    {
		char  s[200002];
		scanf("%s",s);
		int ans=2147483647,cnt[3]={0,0,0};
		for(int i=0,j=0;i<strlen(s);i++)
        {
			while((!cnt[0]||!cnt[1]||!cnt[2])&&j<strlen(s))
			cnt[s[j++]-'1']++;
			if(cnt[0]&&cnt[1]&&cnt[2])
			ans=min(ans,j-i);
			cnt[s[i]-'1']--;
		}
		if(ans==2147483647)
			printf("0\n");
		else
			printf("%d\n",ans);
	}
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int min(int a,int b)
{
    return a>b?b:a;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
    {
		char  s[200002];
		scanf("%s",s);
		int ans=2147483647,cnt[3]={0,0,0};
		for(int i=0,j=0;i<strlen(s);i++)
        {
			while((!cnt[0]||!cnt[1]||!cnt[2])&&j<strlen(s))
			cnt[s[j++]-'1']++;
			if(cnt[0]&&cnt[1]&&cnt[2])
			ans=min(ans,j-i);
			cnt[s[i]-'1']--;
		}
		if(ans==2147483647)
			printf("0\n");
		else
			printf("%d\n",ans);
	}
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int min(int a,int b)
{
    return a>b?b:a;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
    {
		char  s[200002];
		scanf("%s",s);
		int l=strlen(s);
		int ans=2147483647,cnt[3]={0,0,0};
		for(int i=0,j=0;i<l;i++)
        {
			while((!cnt[0]||!cnt[1]||!cnt[2])&&j<l)
			cnt[s[j++]-'1']++;
			if(cnt[0]&&cnt[1]&&cnt[2])
			ans=min(ans,j-i);
			cnt[s[i]-'1']--;
		}
		if(ans==2147483647)
			printf("0\n");
		else
			printf("%d\n",ans);
	}
}


//����dp[i][0/1]��ʾ��i��ͷ�ĳ���2(3)���Ӵ��ܷ�����
#include <bits/stdc++.h>
using namespace std;
int dp[10009][3];
char a[10009];
set<string>s;
int main()
{
	cin >> (a+1);
	int len=strlen(a+1);
	dp[len+1][0]=dp[len+1][1]=1;
	for(int i=len-1;i>=6;i--)
	{
		if( a[i]!=a[i+2] || a[i+1]!=a[i+3] )
			dp[i][0]|=dp[i+2][0];
		dp[i][0]|=dp[i+2][1];//ǰ����2������3
		if( a[i]!=a[i+3] || a[i+1]!=a[i+4] || a[i+2]!=a[i+5] )
			dp[i][1]|=dp[i+3][1];
		dp[i][1]|=dp[i+3][0];//ǰ����3������2
	}
	for(int i=6;i<=len-1;i++)
	{
		string w="";
		w+=a[i]; w+=a[i+1];
		if( dp[i][0] )	s.insert( w );
		if( dp[i][1] )
		{
			w+=a[i+2];
			s.insert( w );
		}
	}
	cout << s.size() << endl;
	set<string>::iterator it=s.begin();
	for( ;it!=s.end();it++)
		cout << *it << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int Gcd(int a,int b){if (b == 0) return a; return Gcd(b , a%b);}
int Lcm(int a, int b){ return a/Gcd(a,b)*b;}
inline long long read(){
   long long f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e6 + 10;
char s[maxn];
int ok[maxn];
int n,len;
vector<string> ans;
void solve(int len,int i){
    string res1 = "",res2 = "";
    for(int j=0; j<len; j++){
        res1 += s[i + j];
        res2 += s[i + j + len];
    }
    if ((res1 != res2 && ok[i + len])){
        ok[i] = 1;
        ans.push_back(res1);
    }
    if (ok[i + 5] && i + 5 <= n){
        ok[i] = 1;
        ans.push_back(res1);
    }
}
int main(){
    scanf("%s",s);
    n = strlen(s),len = 0;
    ok[n] = 1;
    for(int i=n; i>=5; i--){
        len = 2;
        solve(len,i);
        len = 3;
        solve(len,i);
    }
    sort(ans.begin(),ans.end());
    cout << ans.size() << endl;
    for(auto tmp : ans){
        cout << tmp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int Gcd(int a,int b){if (b == 0) return a; return Gcd(b , a%b);}
int Lcm(int a, int b){ return a/Gcd(a,b)*b;}
inline long long read(){
   long long f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e6 + 10;
char s[maxn];
int ok[maxn];
int n,len;
vector<string> ans;
void solve(int len,int pos){
    string res1 = "",res2 = "";
    for(int j=0; j<len; j++){
        res1 += s[pos + j];
        res2 += s[pos + j + len];
    }
    if ((res1 != res2 && ok[pos + len]) && !res1.empty() && !res2.empty()){
        ok[pos] = 1;
        ans.push_back(res1);
    }
    if (ok[pos + 5] && !res1.empty() && !res2.empty()){
        ok[pos] = 1;
        ans.push_back(res1);
    }
}
int main(){
    scanf("%s",s);
    n = strlen(s),len = 0;
    ok[n] = 1;
    for(int i=n-1; i>=5; i--){
        len = 2;
        solve(len,i);
        len = 3;
        solve(len,i);
    }
    sort(ans.begin(),ans.end());
    ans.erase(unique(ans.begin(),ans.end()),ans.end());
    cout << ans.size() << endl;
    for(auto tmp : ans){
        cout << tmp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int Gcd(int a,int b){if (b == 0) return a; return Gcd(b , a%b);}
int Lcm(int a, int b){ return a/Gcd(a,b)*b;}
inline long long read(){
   long long f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e6 + 10;
char s[maxn];
int ok[maxn];
int n,len;
vector<string> ans;
void solve(int len,int pos){
    string res1 = "",res2 = "";
    for(int j=0; j<len; j++){
        res1 += s[pos + j];
        res2 += s[pos + j + len];
    }
    if (ok[pos + len]){
        int tag = 1;
        if (res1 != res2){
            ok[pos] = 1;
            ans.push_back(res1);
            tag = 0;
        }
        if (ok[pos + 5] && tag){
            ok[pos] = 1;
            ans.push_back(res1);
        }
    }
}
int main(){
    scanf("%s",s);
    n = strlen(s),len = 0;
    ok[n] = 1;
    for(int i=n-1; i>=5; i--){
        len = 2;
        solve(len,i);
        len = 3;
        solve(len,i);
    }
    sort(ans.begin(),ans.end());
    ans.erase(unique(ans.begin(),ans.end()),ans.end());
    cout << ans.size() << endl;
    for(auto tmp : ans){
        cout << tmp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int Gcd(int a,int b){if (b == 0) return a; return Gcd(b , a%b);}
int Lcm(int a, int b){ return a/Gcd(a,b)*b;}
inline long long read(){
   long long f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e6 + 10;
char s[maxn];
int ok[maxn];
int main(){
    scanf("%s",s);
    int n = strlen(s);
    vector<string> ans;
    ok[n] = 1;
    for(int i=n-1; i>=5; i--){
        for(int len=2; len<=3; len++){
            string res1 = "",res2 = "";
            for(int j=0; j<len; j++){
                res1 += s[i + j];
                res2 += s[i + j + len];
            }
            // cout << i << ' ' << res1 << ' ' << res2 << endl;
            if (res1 != res2 && ok[i + len]){
                ok[i] = 1;
                ans.push_back(res1);
            }
        }
    }
    sort(ans.begin(),ans.end());
    cout << ans.size() << endl;
    for(auto tmp : ans){
        cout << tmp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int Gcd(int a,int b){if (b == 0) return a; return Gcd(b , a%b);}
int Lcm(int a, int b){ return a/Gcd(a,b)*b;}
inline long long read(){
   long long f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e6 + 10;
char s[maxn];
int ok[maxn];
int n,len;
vector<string> ans;
void solve(int len,int i){
    string res1 = "",res2 = "";
    for(int j=0; j<len; j++){
        res1 += s[i + j];
        res2 += s[i + j + len];
    }
    if ((res1 != res2 && ok[i + len])){
        ok[i] = 1;
        ans.push_back(res1);
    }
}
int main(){
    scanf("%s",s);
    n = strlen(s),len = 0;
    ok[n] = 1;
    for(int i=n; i>=5; i--){
        len = 2;
        solve(len,i);
        len = 3;
        solve(len,i);
    }
    sort(ans.begin(),ans.end());
    cout << ans.size() << endl;
    for(auto tmp : ans){
        cout << tmp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int Gcd(int a,int b){if (b == 0) return a; return Gcd(b , a%b);}
int Lcm(int a, int b){ return a/Gcd(a,b)*b;}
inline long long read(){
   long long f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e6 + 10;
char s[maxn];
int ok[maxn];
int n,len;
vector<string> ans;
void solve(int len,int pos){
    string res1 = "",res2 = "";
    for(int j=0; j<len; j++){
        res1 += s[pos + j];
        res2 += s[pos + j + len];
    }
    if ((res1 != res2 && ok[pos + len])){
        ok[pos] = 1;
        ans.push_back(res1);
    }
    if (ok[pos + 5]){
        ok[pos] = 1;
        ans.push_back(res1);
    }
}
int main(){
    scanf("%s",s);
    n = strlen(s),len = 0;
    ok[n] = 1;
    for(int i=n-1; i>=5; i--){
        len = 2;
        solve(len,i);
        len = 3;
        solve(len,i);
    }
    sort(ans.begin(),ans.end());
    ans.erase(unique(ans.begin(),ans.end()),ans.end());
    cout << ans.size() << endl;
    for(auto tmp : ans){
        cout << tmp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int Gcd(int a,int b){if (b == 0) return a; return Gcd(b , a%b);}
int Lcm(int a, int b){ return a/Gcd(a,b)*b;}
inline long long read(){
   long long f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e6 + 10;
char s[maxn];
int ok[maxn];
int n,len;
vector<string> ans;
void solve(int len,int i){
    string res1 = "",res2 = "";
    for(int j=0; j<len; j++){
        res1 += s[i + j];
        res2 += s[i + j + len];
    }
    if ((res1 != res2 && ok[i + len])){
        ok[i] = 1;
        ans.push_back(res1);
    }
    if (ok[i + 5] && i + 5 <= n){
        ok[i] = 1;
        ans.push_back(res1);
    }
}
int main(){
    scanf("%s",s);
    n = strlen(s),len = 0;
    ok[n] = 1;
    for(int i=n; i>=5; i--){
        len = 2;
        solve(len,i);
        len = 3;
        solve(len,i);
    }
    sort(ans.begin(),ans.end());
    ans.erase(unique(ans.begin(),ans.end()),ans.end());
    cout << ans.size() << endl;
    for(auto tmp : ans){
        cout << tmp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int dp[maxn][2];
int main() {
	string str;
	cin >> str;
	set<string> st;
	int n = str.length();
	for (int i = n - 1; i >= 5; i--) {
		if (i + 2 == n) dp[i][0] = 1;
		if (i + 3 == n) dp[i][1] = 1;
		if (i + 2 < n) {
			if (dp[i + 2][1]) dp[i][0] = 1;
			if (dp[i + 2][0] && (str[i + 1] != str[i + 3] || str[i] != str[i + 2])) dp[i][0] = 1;
		}
		if (i + 3 < n) {
			if (dp[i + 3][0]) dp[i][1] = 1;
			if (dp[i + 3][1] && (str[i + 1] != str[i + 4] || str[i + 2] != str[i + 5] || str[i] != str[i + 3])) dp[i][1] = 1;
		}
	}
	for (int i = n - 1; i >= 5; i--) {
		if (dp[i][0]) st.insert(str.substr(i, 2));
		if (dp[i][1]) st.insert(str.substr(i, 3));
	}
	cout << st.size() << endl;
	for (set<string> :: iterator iter = st.begin(); iter != st.end(); iter++) {
		cout << *iter <<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int dp[maxn][2];
int main() {
	string str;
	cin >> str;
	set<string> st;
	int n = str.size();
	for (int i = n - 1; i >= 5; i--) {
		if (i + 2 == n) dp[i][0] = 1;
		if (i + 3 == n) dp[i][1] = 1;
		if (i + 2 < n) {
			if (dp[i + 2][1]) dp[i][0] = 1;
			if (dp[i + 2][0] && str[i + 1] != str[i + 3] || str[i] != str[i + 2]) dp[i][0] = 1;
		}
		if (i + 3 < n) {
			if (dp[i + 3][0]) dp[i][1] = 1;
			if (dp[i + 3][1] && (str[i + 1] != str[i + 4] || str[i + 2] != str[i + 5] || str[i] != str[i + 3])) dp[i][1] = 1;
		}
	}
	for (int i = n - 1; i >= 5; i--) {
		if (dp[i][0]) st.insert(str.substr(i, 2));
		if (dp[i][1]) st.insert(str.substr(i, 3));
	}
	cout << st.size() << endl;
	for (set<string> :: iterator iter = st.begin(); iter != st.end(); iter++) {
		cout << *iter <<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
using namespace std;
typedef long long ll;
const int maxn = 305000;

int n=0,sz;
int vis[maxn][2];
string str;

bool dfs(int pos,string t){
    if(pos==0)return true;
    bool ok1=false,ok2=false;
    if((pos>=5||pos==2||pos==4)&&vis[pos][0]){
        string tmp=str.substr(sz-pos,2);
        if(tmp!=t)ok1=dfs(pos-2,t);
        if(ok1)return true;
    }
    if((pos>=5||pos==3)&&vis[pos][1]){
        string tmp=str.substr(sz-pos,3);
        if(tmp!=t)ok2=dfs(pos-3,t);
        if(ok2)return true;
    }
    return false;
}

set<string>st;

int main(){
    cin>>str;
    sz=str.size();
    if(sz<=6)puts("0");
    else if(sz==7){
        puts("1");
        cout<<str[5]<<str[6]<<endl;
    }
    else{
        for(int i=2;i<=sz-5;i++){
            string tmp;
            if(i>=5||i==4||i==2){
                tmp=str.substr(sz-i,2);
                //cout<<i<<" "<<tmp<<" "<<i-2<<" "<<dfs(i-2,tmp)<<endl;
                if(dfs(i-2,tmp))st.insert(tmp),vis[i][0]=1;
            }
            if(i>=5||i==3){
                tmp=str.substr(sz-i,3);
                if(dfs(i-3,tmp))st.insert(tmp),vis[i][1]=1;
            }
        }
        cout<<st.size()<<endl;
        for(auto x:st)cout<<x<<endl;
    }
}
//aaaaabbccd


#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
using namespace std;
typedef long long ll;
const int maxn = 305000;

int n=0,sz;
string str;

bool dfs(int pos,string t){
    if(pos==0)return true;
    bool ok1=false,ok2=false;
    if(pos>=5||pos==2||pos==4){
        string tmp=str.substr(sz-pos,2);
        if(tmp!=t)ok1=dfs(pos-2,t);
        if(ok1)return true;
    }
    if(pos>=5||pos==3){
        string tmp=str.substr(sz-pos,3);
        //cout<<tmp<<" "<<t<<endl;
        if(tmp!=t)ok2=dfs(pos-3,t);
        if(ok2)return true;
    }
    return false;
}

set<string>st;

int main(){
    cin>>str;
    sz=str.size();
    if(sz<=6)puts("0");
    else if(sz==7){
        puts("1");
        cout<<str[5]<<str[6]<<endl;
    }
    else{
        for(int i=2;i<=sz-5;i++){
            string tmp;
            if(i>=5||i==4||i==2){
                tmp=str.substr(sz-i,2);
                //cout<<i<<" "<<tmp<<" "<<i-2<<" "<<dfs(i-2,tmp)<<endl;
                if(dfs(i-2,tmp))st.insert(tmp);
            }
            if(i>=5||i==3){
                tmp=str.substr(sz-i,3);
                if(dfs(i-3,tmp))st.insert(tmp);
            }
        }
        cout<<st.size()<<endl;
        for(auto x:st)cout<<x<<endl;
    }
}
//aaaaabbccd


#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
using namespace std;
typedef long long ll;
const int maxn = 305000;

int n=0,sz;
string str;

bool dfs(int pos,string t){
    if(pos==0)return true;
    bool ok1=false,ok2=false;
    if(pos>=5||pos==2||pos==4){
        string tmp=str.substr(sz-pos,2);
        if(tmp!=t)ok1=dfs(pos-2,t);
        if(ok1)return true;
    }
    if(pos>=5||pos==3){
        string tmp=str.substr(sz-pos,3);
        if(tmp!=t)ok2=dfs(pos-2,t);
        if(ok2)return true;
    }
    return false;
}

set<string>st;

int main(){
    cin>>str;
    sz=str.size();
    if(sz<=6)puts("0");
    else if(sz==7){
        puts("1");
        cout<<str[5]<<str[6]<<endl;
    }
    else{
        for(int i=2;i<=sz-5;i++){
            string tmp;
            if(i>=5||i==4||i==2){
                tmp=str.substr(sz-i,2);
                if(dfs(i-2,tmp))st.insert(tmp);
            }
            if(i>=5||i==3){
                tmp=str.substr(sz-i,3);
                if(dfs(i-3,tmp))st.insert(tmp);
            }
        }
        cout<<st.size()<<endl;
        for(auto x:st)cout<<x<<endl;
    }
}


#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
using namespace std;
typedef long long ll;
const int maxn = 305000;

int n=0,sz;
bool vis[maxn][2];
string str;

bool check(int pos,string t){
    if(pos==0)return true;
    if((pos>=5||pos==2||pos==4)&&vis[pos][0]){
        string tmp=str.substr(sz-pos,2);
        if(tmp!=t)return true;
    }
    if((pos>=5||pos==3)&&vis[pos][1]){
        string tmp=str.substr(sz-pos,3);
        if(tmp!=t)return true;
    }
    return false;
}

set<string>st;

int main(){
    cin>>str;
    sz=str.size();
    if(sz<=6)puts("0");
    else if(sz==7){
        puts("1");
        cout<<str[5]<<str[6]<<endl;
    }
    else{
        for(int i=2;i<=sz-5;i++){
            string tmp;
            if(i>=5||i==4||i==2){
                tmp=str.substr(sz-i,2);
                if(check(i-2,tmp))st.insert(tmp),vis[i][0]=true;
            }
            if(i>=5||i==3){
                tmp=str.substr(sz-i,3);
                if(check(i-3,tmp))st.insert(tmp),vis[i][1]=true;
            }
        }
        cout<<st.size()<<endl;
        for(auto x:st)cout<<x<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  set<string> sset;
  for (int i = s.length() - 2; i >= 5; --i) {
    if (s.length() - i >= 4 or i + 2 == s.length()) {
      sset.insert(s.substr(i, 2));
    }
    if (s.length() - i >= 5 or i + 3 == s.length()) {
      sset.insert(s.substr(i, 3));
    }
  }
  cout << sset.size() << '\n';
  if (sset.size()) {
    for (auto i : sset) {
      cout << i << '\n';
    }
  }
}


#include <bits/stdc++.h>
using namespace std;

const int N = 10010;
bool dp[N][2];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen ("input.txt", "r", stdin);
  freopen ("output.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  dp[s.length() - 2][0] = true;
  dp[s.length() - 3][1] = true;
  for (int i = s.length() - 4; i >= 5; --i) {
    if (dp[i + 2][1]) dp[i][0] = true;
    if (dp[i + 3][0]) dp[i][1] = true;
    if (dp[i + 2][0] and s.substr(i, 2) != s.substr(i + 2, 2)) dp[i][0] = true;
    if (dp[i + 3][1] and s.substr(i, 3) != s.substr(i + 3, 3)) dp[i][1] = true;
  }
  set<string> sol;
  for (int i = 5; i < s.length(); ++i) {
    if (dp[i][0]) sol.insert(s.substr(i, 2));
    if (dp[i][1]) sol.insert(s.substr(i, 3));
  }
  cout << sol.size() << '\n';
  for (auto i : sol) {
    cout << i << '\n';
  }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  map<string, int> sset;
  for (int i = s.length() - 2; i >= 5; --i) {
    if (s.length() - i >= 4 or i + 2 == s.length()) {
      sset[s.substr(i, 2)]++;
      if (sset[s.substr(i, 2)] > 1) {
        if (i - 1 >= 5) {
          sset[s.substr(i - 1, 2)]++;
          sset[s.substr(i - 1, 3)]++;
        }
        if (i - 2 >= 5)
          sset[s.substr(i - 2, 3)];
        break;
      }
    }
    if (s.length() - i >= 5 or i + 3 == s.length()) {
      sset[s.substr(i, 3)]++;
    }
  }
  cout << sset.size() << '\n';
  if (sset.size()) {
    for (auto i : sset) {
      cout << i.first << '\n';
    }
  }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;

#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif

#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>

template <class L, class R> istream& operator>> (istream& is, pair<L, R>& P) { return is >> P.ff >> P.ss; }
template <class T> istream& operator>> (istream& is, vector<T>& V) { for (auto& e : V) is >> e; return is; }
template <class L, class R> ostream& operator<< (ostream& os, pair<L, R>& P) { return os << P.ff << " " << P.ss; }
template <class T> ostream& operator<< (ostream& os, vector<T>& V) { for (auto& e : V) os << e << " "; return os; }
template <class T = int> int bsl(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] <= x) { req = mid; lo = mid + 1; } else { hi = mid - 1; } mid = lo + (hi - lo) / 2; } return req; }
template <class T = int> int bsg(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] >= x) { req = mid; hi = mid - 1; } else { lo = mid + 1; } mid = lo + (hi - lo) / 2; } return req; }

template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }
inline double randn() { normal_distribution<double> dis; return dis(rng); }
inline double rdn (double std_dev, double mean) { return std_dev * randn() + mean; }

const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;

inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline bool imax (long long& a, long long b) { bool res = (b > a); a = max(a, b); return res;}
inline bool emax (long long& a, long long b) { bool res = (b >= a); a = max(a, b); return res;}
inline bool imin (long long& a, long long b) { bool res = (b < a); a = min(a, b); return res;}
inline bool emin (long long& a, long long b) { bool res = (b <= a); a = min(a, b); return res;}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

const int p = 67;
const int maxn = 1e4 + 100;
long long rdx[maxn], rdxi[maxn];

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
template <class key, class val>
using hash_table = gp_hash_table<key, val, custom_hash>;

hash_table<char, int> M;

void pre() {
  int i = 1;
  for (char j = '0'; j <= '9'; j++) M[j] = i++;
  for (char j = 'A'; j <= 'Z'; j++) M[j] = i++;
  for (char j = 'a'; j <= 'z'; j++) M[j] = i++;

  rdx[0] = 1;
  rdxi[0] = inv(rdx[0]);
  for (int i = 1; i < maxn; i++) {
    rdx[i] = rdx[i - 1] * p % md;
    rdxi[i] = inv(rdx[i]);
  }
}

struct StringHasher {
  string s;
  int length;
  vector<long long> pref, suf;

  void fillprefHash() {
    long long h = 0;
    for (int i = 0; i < length; i++) {
      h = (h + (M[s[i]]) * rdx[i]) % md;
      pref[i] = h;
    }
  }

  void fillsufHash() {
    long long h = 0;
    for (int i = 0; i < length; i++) {
      h = (h + (M[s[length - 1 - i]]) * rdx[i]) % md;
      suf[i] = h;
    }
  }

  StringHasher(const string& _s) : s(_s){
    length = sz(s);
    pref.assign(length, 0);
    suf.assign(length, 0);
    fillprefHash();
    fillsufHash();
  }

  long long fwd (int i, int j) {
    long long left = (i != 0)? pref[i - 1]: 0;
    long long right = pref[j];
    long long h = right - left;
    if (h < 0) h += md;
    return (h * rdxi[i] % md);
  }

  long long bwd (int i, int j) {
    swap(i, j);
    i = length - 1 - i;
    j = length - 1 - j;
    long long left = (i != 0)? suf[i - 1]: 0;
    long long right = suf[j];
    long long h = right - left;
    if (h < 0) h += md;
    return (h * rdxi[i] % md);
  }
};

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  pre();
  string s;
  cin >> s;
  int n = sz(s);
  StringHasher h(s);
  int dp[n][4][2];
  memset(dp, -1, sizeof(dp));
  function<bool (int, int, int)> Dfs = [&] (int st, int len, int ref) -> bool {

    if (st + len - 1 > n - 1) return 0;
    if (st + len - 1 == n -2) return 0;
    int& cur = dp[st][len][ref];
    if (~cur) {debug(st, st + len - 1, ref, cur); return cur;}
    if (ref) {
      if (h.fwd(st, st + len - 1) == h.fwd(st - len, st - 1)) return 0;
      cur = 1;
    } else {
      cur = 1;
    }

    if (st + len == n) {debug(st, st + len - 1, ref, cur); return cur;}
    int n_st = st + len;
    int n_l1 = len;
    int n_l2 = (len == 3)? 2 : 3;
    bool c1 = Dfs(n_st, n_l1, 1);
    bool c2 = Dfs(n_st, n_l2, 0);
    cur &= (c1 || c2);
    debug(st, st + len - 1, ref, cur);
    return cur;
  };
  for (int i = 5; i <= n - 2; i++) {
    Dfs(i, 2, 0);
    Dfs(i, 3, 0);
  }
  set<string> c;
  for (int i = 5; i <= n - 2; i++) {
    if ((dp[i][2][1] == 1 || dp[i][2][0] == 1)) c.insert(s.substr(i, 2));
    if ((dp[i][3][1] == 1 || dp[i][3][0] == 1)) c.insert(s.substr(i, 3));
  }
  cout << sz(c) << "\n";
  for (auto x : c) cout << x << "\n";
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;

#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif

#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>

template <class L, class R> istream& operator>> (istream& is, pair<L, R>& P) { return is >> P.ff >> P.ss; }
template <class T> istream& operator>> (istream& is, vector<T>& V) { for (auto& e : V) is >> e; return is; }
template <class L, class R> ostream& operator<< (ostream& os, pair<L, R>& P) { return os << P.ff << " " << P.ss; }
template <class T> ostream& operator<< (ostream& os, vector<T>& V) { for (auto& e : V) os << e << " "; return os; }
template <class T = int> int bsl(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] <= x) { req = mid; lo = mid + 1; } else { hi = mid - 1; } mid = lo + (hi - lo) / 2; } return req; }
template <class T = int> int bsg(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] >= x) { req = mid; hi = mid - 1; } else { lo = mid + 1; } mid = lo + (hi - lo) / 2; } return req; }

template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }
inline double randn() { normal_distribution<double> dis; return dis(rng); }
inline double rdn (double std_dev, double mean) { return std_dev * randn() + mean; }

const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;

inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline bool imax (long long& a, long long b) { bool res = (b > a); a = max(a, b); return res;}
inline bool emax (long long& a, long long b) { bool res = (b >= a); a = max(a, b); return res;}
inline bool imin (long long& a, long long b) { bool res = (b < a); a = min(a, b); return res;}
inline bool emin (long long& a, long long b) { bool res = (b <= a); a = min(a, b); return res;}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

const int p = 67;
const int maxn = 110;
long long rdx[maxn], rdxi[maxn];

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
template <class key, class val>
using hash_table = gp_hash_table<key, val, custom_hash>;

hash_table<char, int> M;

void pre() {
  int i = 1;
  for (char j = '0'; j <= '9'; j++) M[j] = i++;
  for (char j = 'A'; j <= 'Z'; j++) M[j] = i++;
  for (char j = 'a'; j <= 'z'; j++) M[j] = i++;

  rdx[0] = 1;
  rdxi[0] = inv(rdx[0]);
  for (int i = 1; i < maxn; i++) {
    rdx[i] = rdx[i - 1] * p % md;
    rdxi[i] = inv(rdx[i]);
  }
}

struct StringHasher {
  string s;
  int length;
  vector<long long> pref, suf;

  void fillprefHash() {
    long long h = 0;
    for (int i = 0; i < length; i++) {
      h = (h + (M[s[i]]) * rdx[i]) % md;
      pref[i] = h;
    }
  }

  void fillsufHash() {
    long long h = 0;
    for (int i = 0; i < length; i++) {
      h = (h + (M[s[length - 1 - i]]) * rdx[i]) % md;
      suf[i] = h;
    }
  }

  StringHasher(const string& _s) : s(_s){
    length = sz(s);
    pref.assign(length, 0);
    suf.assign(length, 0);
    fillprefHash();
    fillsufHash();
  }

  long long fwd (int i, int j) {
    long long left = (i != 0)? pref[i - 1]: 0;
    long long right = pref[j];
    long long h = right - left;
    if (h < 0) h += md;
    return (h * rdxi[i] % md);
  }

  long long bwd (int i, int j) {
    swap(i, j);
    i = length - 1 - i;
    j = length - 1 - j;
    long long left = (i != 0)? suf[i - 1]: 0;
    long long right = suf[j];
    long long h = right - left;
    if (h < 0) h += md;
    return (h * rdxi[i] % md);
  }
};

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  pre();
  string s;
  cin >> s;
  int n = sz(s);
  StringHasher h(s);
  int dp[n][4][2];
  memset(dp, 0, sizeof(dp));
  function<void (int, int, int)> Dfs = [&] (int st, int len, int ref) {
    if (st + len - 1 > n - 1) return;
    if (st + len - 1 == n -2) return;
    int& cur = dp[st][len][ref];
    if (cur) return;
    if (ref) {
      if (h.fwd(st, st + len - 1) == h.fwd(st - len, st - 1)) return;
      cur = 1;
    } else {
      cur = 1;
    }
    int n_st = st + len;
    int n_l1 = len;
    int n_l2 = (len == 3)? 2 : 3;
    Dfs(n_st, n_l1, 1);
    Dfs(n_st, n_l2, 0);
  };
  for (int i = 5; i <= n - 2; i++) {
    Dfs(i, 2, 0);
    Dfs(i, 3, 0);
  }
  set<string> c;
  for (int i = 5; i <= n - 2; i++) {
    if ((dp[i][2][1] || dp[i][2][0])) c.insert(s.substr(i, 2));
    if ((dp[i][3][1] || dp[i][3][0])) c.insert(s.substr(i, 3));
  }
  cout << sz(c) << "\n";
  for (auto x : c) cout << x << "\n";
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;

#ifdef LOCAL
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {return '"' + s + '"';}
string to_string(const char* s) {return to_string((string) s);}
string to_string(bool b) {return (b ? "true" : "false");}
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
void debug_out() { cerr << "\n"; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define trace(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#else
#define debug(...) 42
#define trace(a, n) 42
#endif

#define int long long
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define uint unsigned int
#define pi pair<int, int>
#define pl pair<ll, ll>

template <class L, class R> istream& operator>> (istream& is, pair<L, R>& P) { return is >> P.ff >> P.ss; }
template <class T> istream& operator>> (istream& is, vector<T>& V) { for (auto& e : V) is >> e; return is; }
template <class L, class R> ostream& operator<< (ostream& os, pair<L, R>& P) { return os << P.ff << " " << P.ss; }
template <class T> ostream& operator<< (ostream& os, vector<T>& V) { for (auto& e : V) os << e << " "; return os; }
template <class T = int> int bsl(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] <= x) { req = mid; lo = mid + 1; } else { hi = mid - 1; } mid = lo + (hi - lo) / 2; } return req; }
template <class T = int> int bsg(vector<T> a, T x) { int lo = 0; int hi = sz(a) - 1; int req = -1; int mid = lo + (hi - lo) / 2; while (lo <= hi) { if (a[mid] >= x) { req = mid; hi = mid - 1; } else { lo = mid + 1; } mid = lo + (hi - lo) / 2; } return req; }

template <typename Map>bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
inline long long randint (long long l, long long r) { uniform_int_distribution<long long> dis(l, r); return dis(rng); }
inline double random (double l, double r) { uniform_real_distribution<double> dis(l, r); return dis(rng); }
inline double randn() { normal_distribution<double> dis; return dis(rng); }
inline double rdn (double std_dev, double mean) { return std_dev * randn() + mean; }

const int infi = 2e9;
const long long infl = 4e18;
const int md = 998244353;
const double PI = 3.14159265358979323846;

inline long long modpow(long long x, long long n) { long long res = 1; while (n > 0) { if (n % 2) res = (res * x) % md; x = (x * x) % md; n /= 2; } return res; }
inline long long inv(long long x) {return modpow(x, md-2);}
inline bool imax (long long& a, long long b) { bool res = (b > a); a = max(a, b); return res;}
inline bool emax (long long& a, long long b) { bool res = (b >= a); a = max(a, b); return res;}
inline bool imin (long long& a, long long b) { bool res = (b < a); a = min(a, b); return res;}
inline bool emin (long long& a, long long b) { bool res = (b <= a); a = min(a, b); return res;}
inline long long log_2 (long long n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline long long log_10 (long long n) { return (n > 9ll)? 1ll + log_10(n/10): 0ll;}
inline void add(long long& a, long long b) {a = ((a % md) + (b % md)) % md;}
inline void sub(long long& a, long long b) {a = ((a % md) - (b % md) + md) % md;}
inline void mul(long long& a, long long b) {a = ((a % md) * (b % md)) % md;}
inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r;} return a; }

template <class T>
struct cmp {
  bool operator() (const T& x, const T& y) const {
    return x > y;
  }
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

const int p = 67;
const int maxn = 110;
long long rdx[maxn], rdxi[maxn];

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
template <class key, class val>
using hash_table = gp_hash_table<key, val, custom_hash>;

hash_table<char, int> M;

void pre() {
  int i = 1;
  for (char j = '0'; j <= '9'; j++) M[j] = i++;
  for (char j = 'A'; j <= 'Z'; j++) M[j] = i++;
  for (char j = 'a'; j <= 'z'; j++) M[j] = i++;

  rdx[0] = 1;
  rdxi[0] = inv(rdx[0]);
  for (int i = 1; i < maxn; i++) {
    rdx[i] = rdx[i - 1] * p % md;
    rdxi[i] = inv(rdx[i]);
  }
}

struct StringHasher {
  string s;
  int length;
  vector<long long> pref, suf;

  void fillprefHash() {
    long long h = 0;
    for (int i = 0; i < length; i++) {
      h = (h + (M[s[i]]) * rdx[i]) % md;
      pref[i] = h;
    }
  }

  void fillsufHash() {
    long long h = 0;
    for (int i = 0; i < length; i++) {
      h = (h + (M[s[length - 1 - i]]) * rdx[i]) % md;
      suf[i] = h;
    }
  }

  StringHasher(const string& _s) : s(_s){
    length = sz(s);
    pref.assign(length, 0);
    suf.assign(length, 0);
    fillprefHash();
    fillsufHash();
  }

  long long fwd (int i, int j) {
    long long left = (i != 0)? pref[i - 1]: 0;
    long long right = pref[j];
    long long h = right - left;
    if (h < 0) h += md;
    return (h * rdxi[i] % md);
  }

  long long bwd (int i, int j) {
    swap(i, j);
    i = length - 1 - i;
    j = length - 1 - j;
    long long left = (i != 0)? suf[i - 1]: 0;
    long long right = suf[j];
    long long h = right - left;
    if (h < 0) h += md;
    return (h * rdxi[i] % md);
  }
};

signed main() {
#ifdef READ_FILE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  pre();
  string s;
  cin >> s;
  int n = sz(s);
  StringHasher h(s);
  int dp[n][4][2];
  memset(dp, -1, sizeof(dp));
  function<bool (int, int, int)> Dfs = [&] (int st, int len, int ref) -> bool {

    if (st + len - 1 > n - 1) return 0;
    if (st + len - 1 == n -2) return 0;
    int& cur = dp[st][len][ref];
    if (~cur) {debug(st, st + len - 1, ref, cur); return cur;}
    if (ref) {
      if (h.fwd(st, st + len - 1) == h.fwd(st - len, st - 1)) return 0;
      cur = 1;
    } else {
      cur = 1;
    }

    if (st + len == n) {debug(st, st + len - 1, ref, cur); return cur;}
    int n_st = st + len;
    int n_l1 = len;
    int n_l2 = (len == 3)? 2 : 3;
    bool c1 = Dfs(n_st, n_l1, 1);
    bool c2 = Dfs(n_st, n_l2, 0);
    cur &= (c1 || c2);
    debug(st, st + len - 1, ref, cur);
    return cur;
  };
  for (int i = 5; i <= n - 2; i++) {
    Dfs(i, 2, 0);
    Dfs(i, 3, 0);
  }
  set<string> c;
  for (int i = 5; i <= n - 2; i++) {
    if ((dp[i][2][1] == 1 || dp[i][2][0] == 1)) c.insert(s.substr(i, 2));
    if ((dp[i][3][1] == 1 || dp[i][3][0] == 1)) c.insert(s.substr(i, 3));
  }
  cout << sz(c) << "\n";
  for (auto x : c) cout << x << "\n";
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define ms(a) memset(a,0,sizeof(a))
#define ms_inf(a) memset(a,0x3f,sizeof(a))
#define ms_ninf(a) memset(a,0xcf,sizeof(a))
#define pb push_back
#define mp make_pair
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lc (o<<1)
#define rc (o<<1|1)
typedef long long ll;
typedef unsigned long long ull;
int mod=1e9+7;
const ll INF=1ll<<60;
const int maxn=3e5+10;
const int maxl=100+10;
const double pi=acos(-1.0);
const double eps=1e-8;
bool dp[maxn][2];
string s;
int n;
inline bool va(int x)
{
	return x>=0&&x<n;
}
int main()
{
	fast;cin>>s;
	n=s.length();
	for(int i=n-1;i>=5;--i)
	{
		_for(j,0,2)
		{
			int nx=i+j+2;
			bool&ans=dp[i][j];
			if(nx>n)break;
			if(nx==n-1)continue;
			string cur=s.substr(i,j+2);
			int w=nx+j+2-1;
			if(va(w))
			{
				string t=s.substr(nx,j+2);
				if(t==cur)
				{
					ans=dp[nx][j^1];
					continue;
				}
			}
			dp[i][j]=1;
		}
	}
	set<string>ans;
	_for(i,5,n)_for(j,0,2)if(dp[i][j])ans.insert(s.substr(i,j+2));
	cout<<ans.size()<<"\n";
	for(auto&w:ans)cout<<w<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define ms(a) memset(a,0,sizeof(a))
#define ms_inf(a) memset(a,0x3f,sizeof(a))
#define ms_ninf(a) memset(a,0xcf,sizeof(a))
#define pb push_back
#define mp make_pair
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lc (o<<1)
#define rc (o<<1|1)
typedef long long ll;
typedef unsigned long long ull;
int mod=1e9+7;
const ll INF=1ll<<60;
const int maxn=3e5+10;
const int maxl=100+10;
const double pi=acos(-1.0);
const double eps=1e-8;
string s;
set<string>ans;
int main()
{
	fast;cin>>s;
	int n=s.length();
	_for(i,5,n)
	{
		int len[2]={2,3};
		_for(t,0,2)
		{
			int nx=i+len[t]-1;
			if(nx>=n)continue;
			if(nx+2==n)continue;
			string tmp=s.substr(i,len[t]);
			ans.insert(tmp);
		}
	}
	cout<<ans.size()<<"\n";
	for(auto&w:ans)cout<<w<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define ms(a) memset(a,0,sizeof(a))
#define ms_inf(a) memset(a,0x3f,sizeof(a))
#define ms_ninf(a) memset(a,0xcf,sizeof(a))
#define pb push_back
#define mp make_pair
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lc (o<<1)
#define rc (o<<1|1)
typedef long long ll;
typedef unsigned long long ull;
int mod=1e9+7;
const ll INF=1ll<<60;
const int maxn=3e5+10;
const int maxl=100+10;
const double pi=acos(-1.0);
const double eps=1e-8;
bool dp[maxn][2];
string s;
int n;
inline bool va(int x)
{
	return x>=0&&x<n;
}
int main()
{
	fast;cin>>s;
	n=s.length();
	for(int i=n-1;i>=5;--i)
	{
		_for(j,0,2)
		{
			int nx=i+j+2;
			bool&ans=dp[i][j];
			if(nx>n)break;
			if(nx==n-1)continue;
			if(nx==n)
			{
				ans=1;continue;
			}
			string cur=s.substr(i,j+2);
			int w=nx+j+2-1;
			if(va(w))
			{
				string t=s.substr(nx,j+2);
				if(t==cur)
				{
					ans=dp[nx][j^1];
					continue;
				}
			}
			dp[i][j]=dp[nx][0]||dp[nx][1];
		}
	}
	set<string>ans;
	_for(i,5,n)_for(j,0,2)if(dp[i][j])ans.insert(s.substr(i,j+2));
	cout<<ans.size()<<"\n";
	for(auto&w:ans)cout<<w<<"\n";
}


#include <iostream>
#include <string>
#include <stack>
#include <map>
#include <fstream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <cmath>
#include <set>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <math.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
typedef vector<vector<int>> vvi;
typedef vector<set<int>> vsi;
typedef unsigned int uint;
const int N = 2e5 + 10;
const ll INF = 1e18 + 10;
const int MOD = 1e9 + 7;

char get(string& s, int i, int j) {
	if (i < 0) return 1;
	if (j - i == 3) return (s.substr(i, 2) != s.substr(i + 2, 2));
	return (s.substr(i, 3) != s.substr(i + 3, 3));
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	string s; cin >> s; int n = s.size(); reverse(s.begin(), s.end());
	vector<vector<int>> dp(n + 1, vector<int>(2, 0));
	dp[1][0] = dp[2][1] = 1; set<string> ans;
	for (int i = 3; i < n - 5; i++) {
		dp[i][0] |= (dp[i - 2][0] && get(s, i - 3, i));
		dp[i][0] |= dp[i - 2][1];
		dp[i][1] |= (dp[i - 3][0] && get(s, i - 5, i));
		dp[i][1] |= dp[i - 3][1];
		if (dp[i][0]) ans.insert(s.substr(i - 1, 2));
		if (dp[i][1]) ans.insert(s.substr(i - 2, 3));
	}
	if (s.size() > 6) ans.insert(s.substr(0, 2));
	if (s.size() > 7) ans.insert(s.substr(0, 3));
	set<string> A;
	for (auto i : ans) {
		reverse(i.begin(), i.end());
		A.insert(i);
	}
	cout << ans.size() << '\n';
	for (auto i : A) {
		cout << i << '\n';
	}
}