#include <bits/stdc++.h>
struct {
  inline operator int() {
    int x;
    return scanf("%d", &x), x;
  }
} read;
const int maxn = 200005;
char s[maxn], t[maxn];
int pos[maxn];
int tot[26];
int ansx[maxn], ansy[maxn], ap;
int seg[maxn << 2], tag[maxn << 2];
int query(int now, int L, int R, int l, int r, bool c) {
  if (r < L or l > R) return 0;
  c |= tag[now];
  if (l <= L and R <= r) return c ? R - L + 1 : seg[now];
  int M = (L + R) >> 1;
  return query((now << 1), L, M, l, r, c) +
         query((now << 1 | 1), M + 1, R, l, r, c);
}
void modify(int now, int L, int R, int l, int r) {
  if (r < L or l > R) return;
  if (l <= L and R <= r) return tag[now] = 1, seg[now] = R - L + 1, void();
  int M = (L + R) >> 1;
  modify((now << 1), L, M, l, r);
  modify((now << 1 | 1), M + 1, R, l, r);
  seg[now] = tag[now] ? R - L + 1 : seg[(now << 1)] + seg[(now << 1 | 1)];
}
int n;
void erase(int l, int r) {
  ++ap;
  ansx[ap] = l - query(1, 1, n, 1, l - 1, 0);
  ansy[ap] = r - query(1, 1, n, 1, r, 0);
  modify(1, 1, n, l, r);
}
int f() {
  int sum = 0, max = 0;
  for (int c = 0; c < 26; c++) max = std::max(max, tot[c]);
  for (int c = 0; c < 26; c++) sum += tot[c];
  return std::max(max, (sum + 1) >> 1);
}
int main() {
  int T = read, shit = 0;
  while (T--) {
    ++shit;
    scanf("%s", s + 1);
    n = int(strlen(s + 1));
    ap = 0;
    std::fill(seg + 1, seg + n * 4 + 1, 0);
    std::fill(tag + 1, tag + n * 4 + 1, 0);
    std::fill(tot, tot + 26, 0);
    for (int i = 1; i < n; i++)
      if (s[i] == s[i + 1]) ++tot[s[i] - 'a'];
    int m = 0, now = f();
    for (int i = 1; i < n; i++)
      if (s[i] == s[i + 1]) {
        if (m and t[m] != s[i]) {
          --tot[t[m] - 'a'];
          --tot[s[i] - 'a'];
          int neo = f();
          if (neo == now) {
            ++tot[t[m] - 'a'];
            ++tot[s[i] - 'a'];
            goto ifelse;
          }
          now = neo;
          erase(pos[m--], i);
        } else {
        ifelse:
          ++m;
          t[m] = s[i];
          pos[m] = i + 1;
        }
      }
    pos[m + 1] = n + 1;
    for (int i = 1; i <= m; i++) erase(pos[i], pos[i + 1] - 1);
    erase(1, n);
    printf("%d\n", ap);
    for (int i = 1; i <= ap; i++) printf("%d %d\n", ansx[i], ansy[i]);
  }
}
