#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int a[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int m = s.length();
    int fa = 0, fb = 0, fc = 0;
    for (int i = 0; i < m; i++) {
      if (s[i] == '1' && fa == 0) fa = i + 1;
      if (s[i] == '2' && fb == 0) fb = i + 1;
      if (s[i] == '3' && fc == 0) fc = i + 1;
    }
    if (fa == 0 || fb == 0 || fc == 0) {
      cout << "0" << endl;
      continue;
    }
    int x = max(fa, max(fb, fc));
    int ans = x - min(fa, min(fb, fc)) + 1;
    int sum = ans;
    for (int i = 0; i < m; i++) {
      if (s[i] == '1') {
        int u = min(fb, fc);
        int v = max(fb, fc);
        if (i + 1 >= v)
          sum = i + 1 - u + 1;
        else if (i + 1 <= u)
          sum = v - i;
        else
          sum = v - u + 1;
        ans = min(ans, sum);
        fa = i + 1;
      }
      if (s[i] == '2') {
        int u = min(fa, fc);
        int v = max(fa, fc);
        if (i + 1 >= v)
          sum = i + 1 - u + 1;
        else if (i + 1 <= u)
          sum = v - i;
        else
          sum = v - u + 1;
        ans = min(ans, sum);
        fb = i + 1;
      }
      if (s[i] == '3') {
        int u = min(fb, fa);
        int v = max(fb, fa);
        if (i + 1 >= v)
          sum = i + 1 - u + 1;
        else if (i + 1 <= u)
          sum = v - i;
        else
          sum = v - u + 1;
        ans = min(ans, sum);
        fc = i + 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
