#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
char str[maxn];
int n, sta[maxn], c[30], s[maxn], sz[maxn * 4];
vector<pair<int, int> > ans;
void build(int p, int l, int r) {
  sz[p] = r - l + 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(p * 2, l, mid);
  build(p * 2 + 1, mid + 1, r);
}
void pushdown(int p) {
  if (sz[p]) return;
  sz[p * 2] = sz[p * 2 + 1] = 0;
}
void change(int p, int l, int r, int L, int R) {
  if (L <= l && r <= R) {
    sz[p] = 0;
    return;
  }
  pushdown(p);
  int mid = (l + r) >> 1;
  if (L <= mid) change(p * 2, l, mid, L, R);
  if (R > mid) change(p * 2 + 1, mid + 1, r, L, R);
  sz[p] = sz[p * 2] + sz[p * 2 + 1];
}
int query(int p, int l, int r, int x) {
  if (l == r) return sz[p];
  pushdown(p);
  int mid = (l + r) >> 1;
  if (x <= mid)
    return query(p * 2, l, mid, x);
  else
    return sz[p * 2] + query(p * 2 + 1, mid + 1, r, x);
}
int get(int x) { return x ? query(1, 1, n, x) : 0; }
void del(int l, int r) { change(1, 1, n, l, r); }
int getim() {
  int mx = 0, sum = 0, pos = 0;
  for (int j(0), j_END(25); j <= j_END; j++) {
    if (c[j] > mx) mx = c[j], pos = j;
    sum += c[j];
  }
  if (mx >= (sum + 1) >> 1) return pos;
  return -1;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%s", str + 1);
    n = strlen(str + 1);
    int tp = 0;
    build(1, 1, n);
    memset(c, 0, sizeof(c));
    for (int i(1), i_END(n - 1); i <= i_END; i++)
      if (str[i] == str[i + 1]) c[str[i] - 'a']++;
    int im = -1;
    for (int i(1), i_END(n - 1); i <= i_END; i++)
      if (str[i] == str[i + 1]) {
        if (tp && str[i] != str[sta[tp]]) {
          im = getim();
          if (im != -1) break;
          ans.push_back(make_pair(sta[tp] + 1, i));
          s[sta[tp] + 1]++;
          s[i + 1]--;
          c[str[sta[tp]] - 'a']--;
          c[str[i] - 'a']--;
          --tp;
        } else
          sta[++tp] = i;
        if (im != -1) break;
      }
    im = getim();
    tp = 0;
    for (int i(1), i_END(n); i <= i_END; i++) s[i] += s[i - 1];
    int bg = 1;
    while (s[bg]) ++bg;
    for (int i = bg, j; i <= n; i = j) {
      j = i + 1;
      while (j <= n && s[j]) ++j;
      if (j > n) break;
      if (str[i] != str[j]) continue;
      if (tp && ((str[i] - 'a' == im) ^ (str[sta[tp]] - 'a' == im)))
        ans.push_back(make_pair(sta[tp] + 1, i)), --tp;
      else
        sta[++tp] = i;
    }
    while (tp >= 2) ans.push_back(make_pair(sta[tp - 1] + 1, sta[tp])), --tp;
    if (tp)
      ans.push_back(make_pair(1, sta[tp])),
          ans.push_back(make_pair(sta[tp] + 1, n));
    else
      ans.push_back(make_pair(1, n));
    printf("%d\n", ans.size());
    for (int i(0), i_END(ans.size() - 1); i <= i_END; i++) {
      printf("%d %d\n", get(ans[i].first - 1) + 1, get(ans[i].second));
      del(ans[i].first, ans[i].second);
    }
    for (int i(1), i_END(n); i <= i_END; i++) s[i] = 0;
    ans.clear();
  }
  return 0;
}
