#include <bits/stdc++.h>
using namespace std;
const int sMAX = 10010;
const int MAX = 114514;
const long long bMAX = 1919180;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string zs;
    cin >> zs;
    int ans = zs.size() + 1;
    int m[4] = {0, 0, 0, 0};
    for (int i = 0; i < zs.size(); i++) {
      m[zs[i] - '0'] = i + 1;
      if (m[1] && m[2] && m[3]) {
        int gsb = min(m[1], m[2]);
        ans = min(ans, max(m[1], max(m[2], m[3])) - min(gsb, m[3]) + 1);
      }
    }
    if (ans == zs.size() + 1) {
      cout << 0 << endl;
    } else
      cout << ans << endl;
  }
  return 0;
}
