a = int(input())
for i in range(a):
l = list(input())
ans = 18
for i in range(len(l)):
for j in range(i+1, len(l)):
if l[i] == '0' and l[j] == '0':
ans = min(ans, len(l)-1-i)
elif l[i] == '2' and l[j] == '5':
ans = min(ans, len(l)-1-i)
elif l[i] == '5' and l[j] == '0':
ans = min(ans, len(l)-1-i)
elif l[i] == '7' and l[j] == '5':
ans = min(ans, len(l)-1-i)
print(ans-1)
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
// Write C++ code here
string str[]={"00","25","50","75"};
int t;
cin>>t;
string *s1=new string[t];
for(int k=0;k<t;k++)
cin>>s1[k];
int t1=t-1;
while(t--)
{
string s=s1[t1-t];
int ans=INT_MAX;
for(int j=0;j<4;j++)
{ int c=0,i;
for(i=s.length()-1;i>=0;i--)
{
if(s[i]!=str[j].at(1))
c++;
else{i--;
break;
}
}
for(;i>=0;i--)
{
if(s[i]!=str[j].at(0))
c++;
else break;
}
ans=c<ans?c:ans;
}
cout<<ans<<endl;
ans=INT_MAX;
}
}
922A - Cloning Toys | 817A - Treasure Hunt |
1136B - Nastya Is Playing Computer Games | 1388A - Captain Flint and Crew Recruitment |
592B - The Monster and the Squirrel | 1081A - Definite Game |
721C - Journey | 1400A - String Similarity |
1734E - Rectangular Congruence | 1312D - Count the Arrays |
424C - Magic Formulas | 1730C - Minimum Notation |
1730B - Meeting on the Line | 1730A - Planets |
302B - Eugeny and Play List | 1730D - Prefixes and Suffixes |
1515C - Phoenix and Towers | 998A - Balloons |
1734F - Zeros and Ones | 1144B - Parity Alternated Deletions |
92B - Binary Number | 1144C - Two Shuffled Sequences |
1154B - Make Them Equal | 1272B - Snow Walking Robot |
522B - Photo to Remember | 608B - Hamming Distance Sum |
1408F - Two Different | 274B - Zero Tree |
1726H - Mainak and the Bleeding Polygon | 722A - Broken Clock |