#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353, N = 210000;
int t, n, f[30];
char s[N];
set<pair<int, int> > blo, col[30];
vector<pair<int, int> > ans;
struct BIT {
  int tree[N];
  inline void init() {
    for (register int i = 1; i <= n; ++i) tree[i] = 0;
  }
  inline void modify(int x, int w) {
    while (x <= n) tree[x] += w, x += x & -x;
    return;
  }
  inline int query(int x) {
    int ret = 0;
    while (x) ret += tree[x], x -= x & -x;
    return ret;
  }
} bit;
struct DSU {
  int fa[N];
  inline void init() {
    for (register int i = 1; i <= n; ++i) fa[i] = i;
    return;
  }
  int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
  inline void unite(int x, int y) {
    fa[find(x)] = find(y);
    return;
  }
  inline void color(int x, int y) {
    ans.emplace_back(x - bit.query(x), y - bit.query(y));
    for (register int i = find(y); i >= x; i = find(i))
      bit.modify(i, 1), unite(i, i - 1);
    return;
  }
} dsu;
inline int addMod(int a, int b) { return (a += b) >= mod ? a - mod : a; }
inline long long quickpow(long long base, long long pw) {
  long long ret = 1;
  while (pw) {
    if (pw & 1) ret = ret * base % mod;
    base = base * base % mod, pw >>= 1;
  }
  return ret;
}
template <class T>
inline void read(T &x) {
  x = 0;
  char ch = getchar(), w = 0;
  while (!isdigit(ch)) w = (ch == '-'), ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  x = w ? -x : x;
  return;
}
int main() {
  read(t);
  while (t--) {
    scanf("%s", s + 1), n = strlen(s + 1);
    dsu.init(), bit.init(), ans.clear();
    for (register int i = 1; i < n; ++i)
      if (s[i] == s[i + 1]) {
        int c = s[i] - 'a', d;
        if (blo.size() && (d = blo.rbegin()->second) != c) {
          pair<int, int> tmp = make_pair(blo.rbegin()->first, i);
          col[c].insert(tmp), col[d].insert(tmp);
        }
        ++f[c], blo.insert(make_pair(i, c));
      }
    while (blo.size()) {
      int maxP = 0;
      for (register int i = 1; i <= 25; ++i)
        if (f[i] > f[maxP]) maxP = i;
      if (col[maxP].empty()) {
        --f[maxP];
        auto it = blo.begin();
        dsu.color(it->first, it->first);
        blo.erase(it);
      } else {
        auto l = blo.lower_bound(make_pair(col[maxP].begin()->first, 0)),
             r = next(l);
        pair<int, int> tmp = make_pair(l->first, r->first);
        dsu.color(l->first + 1, r->first);
        --f[l->second], --f[r->second];
        col[l->second].erase(tmp), col[r->second].erase(tmp);
        if (l != blo.begin() && l->second != prev(l)->second) {
          tmp = make_pair(prev(l)->first, l->first);
          col[prev(l)->second].erase(tmp), col[l->second].erase(tmp);
        }
        if (next(r) != blo.end() && r->second != next(r)->second) {
          tmp = make_pair(r->first, next(r)->first);
          col[r->second].erase(tmp), col[next(r)->second].erase(tmp);
        }
        if (l != blo.begin() && next(r) != blo.end() &&
            prev(l)->second != next(r)->second) {
          tmp = make_pair(prev(l)->first, next(r)->first);
          col[prev(l)->second].insert(tmp), col[next(r)->second].insert(tmp);
        }
        blo.erase(l), blo.erase(r);
      }
    }
    ans.emplace_back(1, n - bit.query(n));
    printf("%d\n", ans.size());
    for (auto &k : ans) printf("%d %d\n", k.first, k.second);
  }
  return 0;
}
