#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string str;
    cin >> str;
    vector<int> vec1;
    vector<int> vec2;
    vector<int> vec3;
    bool oo, tt, th;
    for (int i = 0; i < str.size(); i++) {
      if (str[i] == '1') vec1.push_back(i);
      if (str[i] == '2') vec2.push_back(i);
      if (str[i] == '3') vec3.push_back(i);
    }
    int ans = INT_MAX;
    for (auto x : vec1) {
      oo = true;
      tt = th = false;
      int cnt = 1;
      for (int i = x + 1; i < str.size(); i++) {
        if (str[i] == '1') break;
        if (str[i] == '2') tt = true;
        if (str[i] == '3') th = true;
        cnt++;
        if (oo && tt && th) break;
      }
      if (oo && tt && th) ans = min(ans, cnt);
      oo = true;
      tt = th = false;
      cnt = 1;
      for (int i = x - 1; i >= 0; i--) {
        if (str[i] == '1') break;
        if (str[i] == '2') tt = true;
        if (str[i] == '3') th = true;
        cnt++;
        if (oo && tt && th) break;
      }
      if (oo && tt && th) ans = min(ans, cnt);
    }
    for (auto x : vec2) {
      tt = true;
      oo = th = false;
      int cnt = 1;
      for (int i = x + 1; i < str.size(); i++) {
        if (str[i] == '2') break;
        if (str[i] == '1') oo = true;
        if (str[i] == '3') th = true;
        cnt++;
        if (oo && tt && th) break;
      }
      if (oo && tt && th) ans = min(ans, cnt);
      tt = true;
      oo = th = false;
      cnt = 1;
      for (int i = x - 1; i >= 0; i--) {
        if (str[i] == '2') break;
        if (str[i] == '1') oo = true;
        if (str[i] == '3') th = true;
        cnt++;
        if (oo && tt && th) break;
      }
      if (oo && tt && th) ans = min(ans, cnt);
    }
    for (auto x : vec3) {
      th = true;
      oo = tt = false;
      int cnt = 1;
      for (int i = x + 1; i < str.size(); i++) {
        if (str[i] == '3') break;
        if (str[i] == '1') oo = true;
        if (str[i] == '2') tt = true;
        cnt++;
        if (oo && tt && th) break;
      }
      if (oo && tt && th) ans = min(ans, cnt);
      th = true;
      oo = tt = false;
      cnt = 1;
      for (int i = x - 1; i >= 0; i--) {
        if (str[i] == '3') break;
        if (str[i] == '1') oo = true;
        if (str[i] == '2') tt = true;
        cnt++;
        if (oo && tt && th) break;
      }
      if (oo && tt && th) ans = min(ans, cnt);
    }
    if (ans == INT_MAX) ans = 0;
    cout << ans << '\n';
  }
  return 0;
}
