#include <bits/stdc++.h>
using namespace std;
const int oo = 2139063143;
const int N = 1010000;
const int P = 1000000007;
const double eps = 1e-7;
template <typename T>
inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-') p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T>
inline void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
inline void pr(T x) {
  print(x), putchar('\n');
}
template <typename T1, typename T2>
void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2>
void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}
struct TREE {
  int w, d;
} t[N << 2];
void build(int p, int l, int r) {
  t[p].w = t[p].d = 0;
  if (l == r) return;
  int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1;
  build(ls, l, mid), build(rs, mid + 1, r);
}
void pushd(int p, int l, int r) { t[p].d = 1, t[p].w = r - l + 1; }
void push(int p, int l, int r) {
  if (t[p].d) {
    int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1;
    pushd(ls, l, mid), pushd(rs, mid + 1, r);
    t[p].d = 0;
  }
}
void change(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return pushd(p, l, r), void();
  int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1;
  push(p, l, r);
  if (L <= mid) change(ls, l, mid, L, R);
  if (R > mid) change(rs, mid + 1, r, L, R);
  t[p].w = t[ls].w + t[rs].w;
}
int query(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) return t[p].w;
  if (L > R || l > R || L > r) return 0;
  int mid = (l + r) >> 1, ls = p << 1, rs = ls | 1, ans = 0;
  push(p, l, r);
  if (L <= mid) ans = query(ls, l, mid, L, R);
  if (R > mid) ans += query(rs, mid + 1, r, L, R);
  return ans;
}
int n, m, k;
int a[N], b[N], c[N], cnt[30], st[N], z = 0;
int ans[N][2];
void del(int l, int r) {
  ans[++k][0] = l - query(1, 1, n, 1, l - 1);
  ans[k][1] = r - query(1, 1, n, 1, r);
  change(1, 1, n, l, r);
}
int getma() {
  int p = 1;
  for (int i = 2; i <= 26; i++)
    if (cnt[i] > cnt[p]) p = i;
  return p;
}
bool check(int x, int y) {
  --cnt[x], --cnt[y], cnt[0] -= 2;
  int p = getma();
  if (cnt[p] * 2 > cnt[0]) return ++cnt[x], ++cnt[y], cnt[0] += 2, 0;
  return 1;
}
char s[N];
int main() {
  int T;
  sc(T);
  while (T--) {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for (int i = 1; i <= n; i++) a[i] = s[i] - 'a' + 1;
    m = 0, k = 0;
    for (int i = 1; i < n; i++)
      if (a[i] == a[i + 1]) b[++m] = i, c[m] = a[i];
    build(1, 1, n);
    memset(cnt, 0, sizeof(cnt));
    for (int i = 1; i <= m; i++) ++cnt[c[i]];
    cnt[0] = m;
    int p = getma();
    z = 0;
    if (cnt[p] * 2 > m) {
      for (int i = 1; i <= m; i++) {
        if (z && (c[st[z]] == p) + (c[i] == p) == 1) {
          del(b[st[z]] + 1, b[i]), --z;
        } else
          st[++z] = i;
      }
      for (int i = 1; i < z; i++) del(b[st[i]] + 1, b[st[i + 1]]);
      if (z & 1)
        del(1, b[st[z]]), del(b[st[z]] + 1, n);
      else
        del(b[st[z]], n), del(1, n);
    } else {
      if (m & 1) del(b[m] + 1, n), --cnt[c[m]], --m;
      for (int i = 1; i <= m; i++) {
        if (z && c[st[z]] != c[i] && check(c[st[z]], c[i])) {
          del(b[st[z]] + 1, b[i]), --z;
        } else
          st[++z] = i;
      }
      del(1, n);
    }
    pr(k);
    for (int i = 1; i <= k; i++) printf("%d %d\n", ans[i][0], ans[i][1]);
  }
  return 0;
}
