#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long qpow(long long a, long long n) {
  long long r = 1 % P;
  for (a %= P; n; a = a * a % P, n >>= 1)
    if (n & 1) r = r * a % P;
  return r;
}
long long inv(long long first) {
  return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P;
}
inline int rd() {
  int first = 0;
  char p = getchar();
  while (p < '0' || p > '9') p = getchar();
  while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar();
  return first;
}
const int N = 1e6 + 10;
int n, len, tot;
char s[N];
struct _ {
  int l, r, lid, rid, c;
} f[N];
set<int> g[30];
set<pair<int, int> > A[30], B[30];
vector<pair<int, int> > ans;
int BIT[N];
void add(int first, int v) {
  for (; first <= n; first += first & -first) BIT[first] += v;
}
int qry(int first) {
  int r = first;
  for (; first; first ^= first & -first) r += BIT[first];
  return r;
}
void gao(int l, int r) {
  int lx = qry(l), rx = qry(r);
  len -= rx - lx + 1;
  ans.push_back(pair<int, int>(lx, rx));
  add(r + 1, -rx + lx - 1);
}
void upd(int l, int r, int op) {
  if (op == 1) {
    if (f[l].c == f[r].c)
      B[f[l].c].insert(pair<int, int>(l, r));
    else {
      A[f[l].c].insert(pair<int, int>(l, r));
      A[f[r].c].insert(pair<int, int>(l, r));
    }
  } else {
    if (A[f[l].c].count(pair<int, int>(l, r)))
      A[f[l].c].erase(pair<int, int>(l, r));
    if (A[f[r].c].count(pair<int, int>(l, r)))
      A[f[r].c].erase(pair<int, int>(l, r));
    if (B[f[l].c].count(pair<int, int>(l, r)))
      B[f[l].c].erase(pair<int, int>(l, r));
  }
}
void del(int l, int r) {
  int lx = f[l].l, rx = f[r].r;
  if (lx) upd(lx, l, -1);
  if (rx) upd(r, rx, -1);
  f[lx].r = rx, f[rx].l = lx;
  if (lx && rx) upd(lx, rx, 1);
}
void work() {
  scanf("%s", s + 1);
  n = len = strlen(s + 1);
  for (int i = 1; i <= n; ++i) BIT[i] = 0;
  for (int i = 0; i <= 25; ++i) A[i].clear(), B[i].clear(), g[i].clear();
  ans.clear();
  tot = 0;
  for (int i = 1; i <= n - 1; ++i)
    if (s[i] == s[i + 1]) {
      ++tot;
      f[tot] = {tot - 1, tot + 1, i, i + 1, s[i] - 'a'};
      g[s[i] - 'a'].insert(tot);
      if (tot > 1) upd(tot - 1, tot, 1);
    }
  f[1].l = f[tot].r = 0;
  ans.clear();
  while (tot) {
    int mx = 0, first;
    for (int i = 0; i <= 25; ++i)
      if (g[i].size() > mx) mx = g[i].size(), first = i;
    if (A[first].size()) {
      pair<int, int> u = *A[first].begin();
      A[first].erase(A[first].begin());
      g[f[u.first].c].erase(u.first);
      g[f[u.second].c].erase(u.second);
      if (f[u.first].c == first)
        A[f[u.second].c].erase(u);
      else
        A[f[u.first].c].erase(u);
      del(u.first, u.second);
      gao(f[u.first].rid, f[u.second].lid);
      tot -= 2;
    } else if (B[first].size()) {
      pair<int, int> u = *B[first].begin();
      B[first].erase(B[first].begin());
      g[first].erase(u.second);
      del(u.second, u.second);
      gao(f[u.first].rid, f[u.second].lid);
      tot -= 1;
    } else {
      int u = *g[first].begin();
      g[first].erase(g[first].begin());
      del(u, u);
      gao(f[u].lid, f[u].lid);
      tot -= 1;
    }
  }
  ans.push_back(pair<int, int>(1, len));
  printf("%d\n", (int)ans.size());
  for (auto [first, second] : ans) printf("%d %d\n", first, second);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) work();
}
