#include <bits/stdc++.h>
using std::make_pair;
using std::pair;
using std::set;
using std::vector;
const int N = 1e6 + 5, M = 30;
int n;
char s[N];
set<int> S;
int cnt[M];
set<pair<int, int> > e[M];
vector<pair<int, int> > V;
int bit[N];
inline void add(int x, int k) {
  while (x <= n) bit[x] += k, x += x & -x;
  return;
}
inline int ask(int x) {
  int res = 0;
  while (x) res += bit[x], x &= x - 1;
  return res;
}
inline int find(int k) {
  int res = 0;
  register int i;
  for (i = 20; i >= 0; i--)
    if ((res | 1 << i) <= n && bit[res | 1 << i] <= k)
      res |= 1 << i, k -= bit[res];
  return res;
}
inline int color(int k) { return s[k] - 'a' + 1; }
inline void insert(int k) {
  int p1 = 0, p2 = 0;
  set<int>::iterator it, jt;
  S.insert(k);
  it = S.find(k);
  cnt[color(k)]++;
  if (it != S.begin()) {
    jt = it;
    jt--;
    p1 = *jt;
    if (color(*jt) != color(k))
      e[color(*jt)].insert(make_pair(*jt, k)),
          e[color(k)].insert(make_pair(*jt, k));
  }
  jt = it;
  jt++;
  if (jt != S.end()) {
    p2 = *jt;
    if (color(*jt) != color(k))
      e[color(*jt)].insert(make_pair(k, *jt)),
          e[color(k)].insert(make_pair(k, *jt));
  }
  if (p1 && p2 && color(p1) != color(p2))
    e[color(p1)].erase(make_pair(p1, p2)),
        e[color(p2)].erase(make_pair(p1, p2));
}
inline void erase(int k) {
  int p1 = 0, p2 = 0;
  set<int>::iterator it, jt;
  it = S.find(k);
  if (it != S.begin()) {
    jt = it;
    jt--;
    p1 = *jt;
    if (color(*jt) != color(k))
      e[color(*jt)].erase(make_pair(*jt, k)),
          e[color(k)].erase(make_pair(*jt, k));
  }
  jt = it;
  jt++;
  if (jt != S.end()) {
    p2 = *jt;
    if (color(*jt) != color(k))
      e[color(*jt)].erase(make_pair(k, *jt)),
          e[color(k)].erase(make_pair(k, *jt));
  }
  if (p1 && p2 && color(p1) != color(p2))
    e[color(p1)].insert(make_pair(p1, p2)),
        e[color(p2)].insert(make_pair(p1, p2));
  S.erase(k);
  cnt[color(k)]--;
  return;
}
inline void run(int l, int r) {
  int d;
  printf("%d %d\n", ask(l - 1) + 1, ask(r));
  while (ask(r) != (d = ask(l - 1))) add(find(d) + 1, -1);
  return;
}
inline void solve() {
  int x;
  pair<int, int> p;
  register int i;
  scanf("%s", s + 1);
  for (i = 1; i <= 26; i++) cnt[i] = 0, e[i].clear();
  V.clear();
  S.clear();
  n = strlen(s + 1);
  for (i = 2; i <= n; i++)
    if (s[i] == s[i - 1]) insert(i);
  for (i = 1; i <= 26; i++)
    if (cnt[i] * 2 > (int)S.size()) {
      while (!e[i].empty())
        p = *e[i].begin(), V.push_back(make_pair(p.first, p.second - 1)),
        erase(p.first), erase(p.second);
      while (!S.empty()) x = *S.begin(), V.push_back(make_pair(x, x)), erase(x);
      goto end;
    }
  while ((int)S.size() > 1) {
    for (x = 1, i = 2; i <= 26; i++)
      if (cnt[i] > cnt[x]) x = i;
    p = *e[x].begin();
    V.push_back(make_pair(p.first, p.second - 1));
    erase(p.first);
    erase(p.second);
  }
  while (!S.empty()) x = *S.begin(), V.push_back(make_pair(x, x)), erase(x);
end:;
  V.push_back(make_pair(1, n));
  printf("%d\n", (int)V.size());
  memset(bit + 1, 0, sizeof(int) * n);
  for (i = 1; i <= n; i++) add(i, 1);
  for (auto p : V) run(p.first, p.second);
  return;
}
signed main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}
