#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n, t, k;
string s;
int main() {
  scanf("%d", &t);
  while (t--) {
    cin >> s;
    int len = 0;
    int minLen = 0x7fffffff;
    int Size = s.size();
    map<char, int> m;
    int st = 0, ed = 0;
    while (st < Size && ed < Size) {
      bool flag = 0;
      while (st < ed && st < Size && m['3'] && m['1'] && m['2'])
        minLen = min(minLen, ed - st), m[s[st++]]--;
      while (ed < Size && (!m['3'] || !m['1'] || !m['2'])) m[s[ed++]]++;
      if (ed <= Size && m['3'] && m['1'] && m['2'])
        minLen = min(minLen, ed - st);
    }
    while (st < ed && st < Size && m['3'] && m['1'] && m['2'])
      minLen = min(minLen, ed - st), m[s[st++]]--;
    cout << (minLen == 0x7fffffff ? 0 : minLen) << endl;
  }
  return 0;
}
