#include <bits/stdc++.h>
using namespace std;
const int N = 202000;
struct seg_tree {
  int l, r, sum, lazy;
} tr[N << 2];
inline void pushdown(int p) {
  if (tr[p].lazy) {
    tr[p << 1].lazy = tr[p << 1 | 1].lazy = 1;
    tr[p << 1].sum = tr[p << 1].r - tr[p << 1].l + 1;
    tr[p << 1 | 1].sum = tr[p << 1 | 1].r - tr[p << 1 | 1].l + 1;
    tr[p].lazy = 0;
  }
}
void build(int p, int l, int r) {
  tr[p].l = l, tr[p].r = r;
  tr[p].sum = tr[p].lazy = 0;
  if (l == r) {
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
}
void update(int p, int L, int R) {
  if (L <= tr[p].l && tr[p].r <= R) {
    tr[p].sum = tr[p].r - tr[p].l + 1;
    tr[p].lazy = 1;
    return;
  }
  pushdown(p);
  int mid = (tr[p].l + tr[p].r) >> 1;
  if (L <= mid) update(p << 1, L, R);
  if (R > mid) update(p << 1 | 1, L, R);
  tr[p].sum = tr[p << 1].sum + tr[p << 1 | 1].sum;
}
int query(int p, int L, int R) {
  if (L <= tr[p].l && tr[p].r <= R) {
    return tr[p].sum;
  }
  pushdown(p);
  int mid = (tr[p].l + tr[p].r) >> 1, ret = 0;
  if (L <= mid) ret += query(p << 1, L, R);
  if (R > mid) ret += query(p << 1 | 1, L, R);
  return ret;
}
int T, len, t[N];
char str[N];
int pos[N], cnt[30], m;
int s[N], top;
int l[N], r[N], num;
inline bool check() {
  int mx = 0, sum = 0;
  for (int i = 0; i < 26; ++i) {
    mx = max(mx, cnt[i]);
    sum += cnt[i];
  }
  return 2 * mx <= sum;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    num = m = 0;
    for (int i = 0; i < 26; ++i) cnt[i] = 0;
    scanf("%s", str + 1);
    len = strlen(str + 1);
    for (int i = 2; i <= len; ++i)
      if (str[i] == str[i - 1]) {
        cnt[str[i] - 'a']++;
        t[++m] = str[i] - 'a';
        pos[m] = i;
      }
    int mx = 0;
    for (int i = 0; i < 26; ++i) {
      if (cnt[mx] < cnt[i]) mx = i;
    }
    if (2 * cnt[mx] <= m) {
      if (m & 1) --cnt[t[m]], --m;
      top = 0;
      for (int i = 1; i <= m; ++i) {
        if (!top || t[i] == t[s[top]])
          s[++top] = i;
        else {
          cnt[t[i]]--;
          cnt[t[s[top]]]--;
          if (check()) {
            ++num;
            l[num] = pos[s[top]], r[num] = pos[i] - 1;
            --top;
          } else {
            cnt[t[i]]++, cnt[t[s[top]]]++;
            s[++top] = i;
          }
        }
      }
    } else {
      top = 0;
      for (int i = 1; i <= m; ++i) {
        if (t[i] == mx) {
          if (!top || t[s[top]] == mx)
            s[++top] = i;
          else {
            ++num;
            l[num] = pos[s[top]], r[num] = pos[i] - 1;
            --top;
          }
        } else {
          if (top && t[s[top]] == mx) {
            ++num;
            l[num] = pos[s[top]], r[num] = pos[i] - 1;
            --top;
          } else
            s[++top] = i;
        }
      }
    }
    build(1, 1, len);
    for (int i = 1; i <= num; ++i) {
      int x = l[i] - query(1, 1, l[i]);
      int y = r[i] - query(1, 1, r[i]);
      update(1, l[i], r[i]);
      l[i] = x, r[i] = y;
    }
    m = 0;
    for (int i = 1; i <= len; ++i) {
      if (query(1, i, i)) continue;
      t[++m] = str[i] - 'a';
    }
    int L = 1;
    for (int R = 2; R <= m; ++R) {
      if (t[R] == t[R - 1]) {
        ++num;
        l[num] = 1, r[num] = R - L;
        L = R;
      }
    }
    ++num;
    l[num] = 1, r[num] = m - L + 1;
    printf("%d\n", num);
    for (int i = 1; i <= num; ++i) printf("%d %d\n", l[i], r[i]);
  }
  return 0;
}
