#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i2 = pair<int, int>;
using ll2 = pair<ll, ll>;
const int N = 2e5 + 9;
int nxt[4][N], prv[4][N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n = s.length();
    prv[1][0] = prv[2][0] = prv[3][0] = -1000000;
    nxt[1][n - 1] = nxt[2][n - 1] = nxt[3][n - 1] = 1000000;
    for (int i = 1; i < n; i++) {
      prv[1][i] = prv[1][i - 1];
      prv[2][i] = prv[2][i - 1];
      prv[3][i] = prv[3][i - 1];
      prv[s[i - 1] - '0'][i] = i - 1;
    }
    for (int i = n - 2; i >= 0; i--) {
      nxt[1][i] = nxt[1][i + 1];
      nxt[2][i] = nxt[2][i + 1];
      nxt[3][i] = nxt[3][i + 1];
      nxt[s[i + 1] - '0'][i] = i + 1;
    }
    int res = INT_MAX;
    for (int i = 1; i < n - 1; i++) {
      int x = s[i] - '0';
      int y = x % 3 + 1;
      int z = y % 3 + 1;
      res = min(res, min(nxt[y][i] - prv[z][i], nxt[z][i] - prv[y][i]) + 1);
    }
    cout << (res > n ? 0 : res) << "\n";
  }
  return 0;
}
