#include <bits/stdc++.h>
struct node {
  int l, r;
} tmp[200005];
char a[200005], b[200005];
int tag[800005], sum[800005];
int tot[355], resL[200005], resR[200005], st[200005], p[200005], tmpL[200005],
    tmpR[200005];
inline bool cmp1(node x, node y) { return x.l < y.l; }
inline bool cmp2(node x, node y) { return x.r > y.r; }
inline void build(int p, int l, int r) {
  tag[p] = -1;
  sum[p] = 0;
  if (l == r) {
    sum[p] = 1;
    return;
  }
  int mid = l + r >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  sum[p] = sum[p << 1] + sum[p << 1 | 1];
}
inline void spread(int p, int l, int r) {
  int mid = l + r >> 1;
  if (tag[p] != -1) {
    tag[p << 1] = tag[p << 1 | 1] = tag[p];
    sum[p << 1] = (mid - l + 1) * tag[p];
    sum[p << 1 | 1] = (r - mid) * tag[p];
    tag[p] = -1;
  }
}
inline void change(int p, int l, int r, int L, int R, int val) {
  if (L <= l && r <= R) {
    sum[p] = (r - l + 1) * val;
    tag[p] = val;
    return;
  }
  spread(p, l, r);
  int mid = l + r >> 1;
  if (L <= mid) change(p << 1, l, mid, L, R, val);
  if (R > mid) change(p << 1 | 1, mid + 1, r, L, R, val);
  sum[p] = sum[p << 1] + sum[p << 1 | 1];
}
inline int ask(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return sum[p];
  spread(p, l, r);
  int mid = l + r >> 1, res = 0;
  if (L <= mid) res += ask(p << 1, l, mid, L, R);
  if (R > mid) res += ask(p << 1 | 1, mid + 1, r, L, R);
  return res;
}
inline int askPos(int p, int l, int r) {
  if (l == r) return l;
  int mid = l + r >> 1;
  if (sum[p << 1])
    return askPos(p << 1, l, mid);
  else
    return askPos(p << 1 | 1, mid + 1, r);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int top = 0, m = 0, num = 0, len = 0, ans = 0, res = 0, now, pos1, pos2;
    scanf("%s", a + 1);
    len = strlen(a + 1);
    for (register char ch = 'a'; ch <= 'z'; ++ch) tot[ch] = 0;
    for (register int i = 1; i <= len; ++i)
      if (a[i] == a[i + 1]) {
        ++tot[a[i]];
        p[++num] = i;
        b[num] = a[i];
      }
    m = num;
    pos1 = 1;
    pos2 = len;
    build(1, 1, len);
    for (now = 1; now <= num; ++now) {
      for (register char ch = 'a'; ch <= 'z'; ++ch)
        res = tot[res] > tot[ch] ? res : ch;
      if (2 * tot[res] >= m) break;
      if (!top) {
        st[++top] = now;
      } else {
        if (b[st[top]] == b[now]) {
          st[++top] = now;
        } else {
          --tot[b[st[top]]];
          --tot[b[now]];
          resL[++ans] = ask(1, 1, len, 1, p[st[top]] + 1);
          resR[ans] = ask(1, 1, len, 1, p[now]);
          tmpL[ans] = p[st[top]] + 1;
          tmpR[ans] = p[now];
          change(1, 1, len, p[st[top]] + 1, p[now], 0);
          --top;
          m -= 2;
        }
      }
    }
    if (m) {
      for (; now <= num; ++now) {
        if (!top || b[st[top]] == b[now] ||
            (b[st[top]] != res && b[now] != res)) {
          st[++top] = now;
        } else {
          --tot[b[st[top]]];
          --tot[b[now]];
          resL[++ans] = ask(1, 1, len, 1, p[st[top]] + 1);
          resR[ans] = ask(1, 1, len, 1, p[now]);
          tmpL[ans] = p[st[top]] + 1;
          tmpR[ans] = p[now];
          change(1, 1, len, p[st[top]] + 1, p[now], 0);
          --top;
          m -= 2;
        }
      }
    }
    for (register int t = 1; t <= top; ++t) {
      int pos = askPos(1, 1, len);
      --tot[b[st[t]]];
      resL[++ans] = ask(1, 1, len, 1, pos);
      resR[ans] = ask(1, 1, len, 1, p[st[t]]);
      tmpL[ans] = pos;
      tmpR[ans] = p[st[t]];
      change(1, 1, len, pos, p[st[t]], 0);
    }
    printf("%d\n", ans + 1);
    for (register int i = 1; i <= ans; ++i) {
      printf("%d %d\n", resL[i], resR[i]);
      tmp[i].l = tmpL[i];
      tmp[i].r = tmpR[i];
    }
    std::sort(tmp + 1, tmp + 1 + ans, cmp1);
    for (register int i = 1; i <= ans; ++i)
      pos1 = (tmp[i].l <= pos1
                  ? ((pos1) > (tmp[i].r + 1) ? (pos1) : (tmp[i].r + 1))
                  : pos1);
    std::sort(tmp + 1, tmp + 1 + ans, cmp2);
    for (register int i = 1; i <= ans; ++i)
      pos2 = (tmp[i].r >= pos2
                  ? ((pos2) < (tmp[i].l - 1) ? (pos2) : (tmp[i].l - 1))
                  : pos2);
    pos1 = ((pos1) > (1) ? (pos1) : (1));
    pos2 = ((pos2) < (len) ? (pos2) : (len));
    printf("%d %d\n", ask(1, 1, len, 1, pos1), ask(1, 1, len, 1, pos2));
  }
  return 0;
}
