#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool flag = false;
  while (!isdigit(c)) {
    if (c == '-') flag = true;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
  if (flag) x = -x;
}
using namespace std;
int n;
int tre[201000];
inline void add(int i, int x) {
  while (i <= n) tre[i] += x, i += ((i) & (-(i)));
}
inline int query(int i) {
  int res = 0;
  while (i) res += tre[i], i -= ((i) & (-(i)));
  return res;
}
pair<int, int> ans[201000];
int atot;
inline void Push(int l, int r) {
  int L = query(l), R = query(r);
  if (L >= R) {
    printf("QAQ : Push(%d, %d), L = %d, R = %d\n", l, r, L, R);
    exit(-1);
  }
  ans[++atot] = make_pair(L + 1, R);
  add(r, -(R - L));
}
inline void Print() {
  printf("%d\n", atot);
  for (int i = 1; i <= atot; ++i)
    printf("%d %d\n", ans[i].first, ans[i].second);
}
char s[201000];
int pos[201000], ptot;
int pre[201000], nxt[201000];
int cnt[27];
priority_queue<pair<int, int> > q;
set<int> st[27];
inline int Col(int p) { return s[pos[p]] - 'a'; }
inline void Link(int p) {
  int q = pre[p];
  if (!p || !q) return;
  if (Col(p) != Col(q)) {
    st[Col(q)].insert(p);
    st[Col(p)].insert(p);
  }
}
inline void Cancl(int p) {
  if (!p || !pre[p] || Col(p) == Col(pre[p])) return;
  st[Col(p)].erase(p);
  st[Col(pre[p])].erase(p);
}
inline void Cut(int p) {
  int q = pre[p];
  int pe = pre[q], nt = nxt[p];
  --cnt[Col(p)];
  if (cnt[Col(p)]) ::q.push(make_pair(cnt[Col(p)], Col(p)));
  --cnt[Col(q)];
  if (cnt[Col(q)]) ::q.push(make_pair(cnt[Col(q)], Col(q)));
  Cancl(q);
  Cancl(p);
  Cancl(nt);
  Push(pos[q], pos[p]);
  pre[nt] = pe;
  nxt[pe] = nt;
  if (nt <= ptot) Link(nt);
}
inline void calc() {
  for (int i = 2; i <= ptot; ++i) {
    pre[i] = i - 1, nxt[i - 1] = i;
    Link(i);
  }
  pre[1] = 0;
  nxt[ptot] = ptot + 1;
  nxt[0] = 1;
  pre[ptot + 1] = ptot;
  for (int i = 1; i <= ptot; ++i) ++cnt[s[pos[i]] - 'a'];
  for (int c = 0; c < 26; ++c)
    if (cnt[c]) q.push(make_pair(cnt[c], c));
  while (q.size()) {
    pair<int, int> pr = q.top();
    q.pop();
    int c = pr.second;
    if (cnt[c] != pr.first) continue;
    if (!st[c].size()) {
      for (int p = nxt[0]; p <= ptot; p = nxt[p]) {
        Push(0, pos[p]);
      }
      break;
    }
    int p = *st[c].begin();
    Cut(p);
  }
  Push(0, n);
}
inline void work() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i < n; ++i)
    if (s[i] == s[i + 1]) pos[++ptot] = i;
  for (int i = 1; i <= n; ++i) add(i, 1);
  calc();
  Print();
}
inline void clear() {
  atot = 0;
  memset(tre, 0, (n + 3) << 2);
  memset(pos, 0, (ptot + 3) << 2);
  memset(pre, 0, (ptot + 3) << 2);
  memset(nxt, 0, (ptot + 3) << 2);
  memset(cnt, 0, sizeof(cnt));
  while (q.size()) q.pop();
  for (int c = 0; c < 26; ++c) st[c].clear();
  ptot = 0;
}
int main() {
  int _;
  read(_);
  while (_--) work(), clear();
  return 0;
}
