#include <bits/stdc++.h>
using namespace std;
const int N = 200003;
template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) return a = b, 1;
  return 0;
}
int T, n, m, cnt, mx, sum, wei, pos[N], bu[26], bbu[N], stk[N], tp, seg[N << 2],
    ansl[N], ansr[N];
char str[N];
void del(int c) {
  c -= 'a';
  --bbu[bu[c]];
  ++bbu[--bu[c]];
  --sum;
  mx -= !bbu[mx];
  wei = sum - (mx << 1);
}
void add(int c) {
  c -= 'a';
  --bbu[bu[c]];
  ++bbu[++bu[c]];
  ++sum;
  chmax(mx, bu[c]);
  wei = sum - (mx << 1);
}
bool check(int l, int r) {
  if (str[l] == str[r]) return false;
  int tmp = wei;
  del(str[l]);
  del(str[r]);
  if (wei < tmp && wei < -1) {
    add(str[l]);
    add(str[r]);
    return false;
  }
  return true;
}
void pup(int x) { seg[x] = seg[x << 1] + seg[x << 1 | 1]; }
void build(int x = 1, int L = 1, int R = n) {
  seg[x] = R - L + 1;
  if (L == R) return;
  int mid = L + R >> 1;
  build(x << 1, L, mid);
  build(x << 1 | 1, mid + 1, R);
}
void upd(int l, int r, int x = 1, int L = 1, int R = n) {
  if (l <= L && R <= r) {
    seg[x] = 0;
    return;
  }
  int mid = L + R >> 1;
  if (l <= mid) upd(l, r, x << 1, L, mid);
  if (mid < r) upd(l, r, x << 1 | 1, mid + 1, R);
  pup(x);
}
int qry(int l, int r, int x = 1, int L = 1, int R = n) {
  if (!seg[x] || (l <= L && R <= r)) return seg[x];
  int mid = L + R >> 1, res = 0;
  if (l <= mid) res = qry(l, r, x << 1, L, mid);
  if (mid < r) res += qry(l, r, x << 1 | 1, mid + 1, R);
  return res;
}
void solve() {
  scanf("%s", str + 1);
  n = strlen(str + 1);
  m = mx = sum = tp = cnt = 0;
  memset(bbu, 0, n + 1 << 2);
  memset(bu, 0, sizeof bu);
  for (int i = 2; i <= n; ++i)
    if (str[i] == str[i - 1]) {
      pos[++m] = i;
      ++sum;
      ++bu[str[i] - 'a'];
    }
  for (int i = 0; i < 26; ++i) {
    ++bbu[bu[i]];
    chmax(mx, bu[i]);
  }
  printf("%d\n", max(sum + 1 >> 1, mx) + 1);
  wei = sum - (mx << 1);
  for (int i = 1; i <= m; ++i)
    if (tp && check(pos[stk[tp]], pos[i])) {
      ansl[++cnt] = pos[stk[tp--]];
      ansr[cnt] = pos[i] - 1;
    } else
      stk[++tp] = i;
  while (tp) {
    ansl[++cnt] = pos[stk[tp--]];
    ansr[cnt] = n;
  }
  ansl[++cnt] = 1;
  ansr[cnt] = n;
  build();
  for (int i = 1; i <= cnt; ++i) {
    int L = ansl[i], R = ansr[i];
    printf("%d %d\n", (L > 1 ? qry(1, L - 1) : 0) + 1, qry(1, R));
    upd(L, R);
  }
}
int main() {
  scanf("%d", &T);
  while (T--) solve();
}
