#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string p;
    cin >> p;
    int a = p.length();
    int one = 0, two = 0, three = 0;
    for (int i = 0; i < a; i++) {
      if (p[i] == '1') one++;
      if (p[i] == '2') two++;
      if (p[i] == '3') three++;
    }
    int o, t, th, ans = INT_MAX;
    o = t = th = -1;
    if (one && two && three) {
      for (int i = 0; i < a; i++) {
        if (p[i] == '1') o = i;
        if (p[i] == '2') t = i;
        if (p[i] == '3') th = i;
        if (o > -1 && t > -1 && th > -1) {
          ans = min(ans, max(o, max(t, th)) - min(o, min(t, th)) + 1);
        }
      }
      cout << ans << "\n";
    } else
      cout << 0 << "\n";
  }
}
