#include <bits/stdc++.h>
using namespace std;
set<int> lst, now, st[26];
pair<int, int> ans[200010], h[200010];
int c[200010], cnt[26], num[200010], t, n, tt, tot;
char s[200010];
inline int rd() {
  int x = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x;
}
inline void print(int x) {
  static char s[233];
  if (!x) {
    putchar('0');
    return;
  }
  int tot = 0;
  for (; x; x /= 10) s[++tot] = x % 10 + '0';
  for (; tot; tot--) putchar(s[tot]);
}
inline void add(int x) {
  for (; x <= n; x += ((x) & (-(x)))) c[x]++;
}
inline int query(int x) {
  int res = 0;
  for (; x; x -= ((x) & (-(x)))) res += c[x];
  return res;
}
inline void modify(int l, int r) {
  if (lst.empty()) return;
  ans[++tot] = pair<int, int>(l - 1 - query(l - 1) + 1, r - query(r));
  for (set<int>::iterator it = lst.lower_bound(l); it != lst.end() && *it <= r;
       lst.erase(it++))
    add(*it);
}
inline void del(int id) {
  cnt[h[id].second]--;
  set<int>::iterator itt = now.find(id);
  itt++;
  if (itt == now.end()) {
    set<int>::iterator it = now.find(id);
    if (it != now.begin()) {
      it--;
      int l = *it;
      if (h[id].second != h[l].second)
        st[h[id].second].erase(l), st[h[l].second].erase(l);
    }
    now.erase(id);
    return;
  }
  int r = *itt;
  st[h[id].second].erase(id);
  st[h[r].second].erase(id);
  set<int>::iterator it = now.find(id);
  if (it != now.begin()) {
    it--;
    int l = *it;
    if (h[id].second != h[l].second)
      st[h[id].second].erase(l), st[h[l].second].erase(l);
    if (h[l].second != h[r].second)
      st[h[l].second].insert(l), st[h[r].second].insert(l);
  }
  now.erase(id);
}
inline void work() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  tt = 0;
  for (int i = 1; i < n; i++)
    if (s[i] == s[i + 1]) h[++tt] = pair<int, int>(i, s[i] - 'a');
  memset(cnt, 0, sizeof(cnt));
  for (int i = 1; i <= tt; i++) cnt[h[i].second]++;
  for (int i = 0; i < 26; i++) st[i].clear();
  now.clear();
  lst.clear();
  for (int i = 1; i <= tt; i++) now.insert(i);
  for (int i = 1; i <= n; i++) lst.insert(i), c[i] = 0;
  for (int i = 1; i < tt; i++)
    if (h[i].second != h[i + 1].second) {
      st[h[i].second].insert(i);
      st[h[i + 1].second].insert(i);
    }
  tot = 0;
  while (233) {
    int S = 0;
    for (int i = 0; i < 26; i++)
      if (cnt[i]) S++;
    if (S <= 1) break;
    int id = 0;
    for (int i = 0; i < 26; i++)
      if (cnt[i] > cnt[id]) id = i;
    int hh = *st[id].begin(), hhh = *now.upper_bound(hh);
    modify(h[hh].first + 1, h[hhh].first);
    del(hh);
    del(hhh);
  }
  int cnt = 0;
  for (set<int>::iterator it = now.begin(); it != now.end(); it++)
    num[++cnt] = h[*it].first;
  num[++cnt] = n;
  for (int i = 1; i <= cnt; i++) modify(num[i - 1] + 1, num[i]);
  print(tot);
  putchar('\n');
  for (int i = 1; i <= tot; i++)
    print(ans[i].first), putchar(' '), print(ans[i].second), putchar('\n');
}
int main() {
  t = rd();
  while (t--) work();
  return 0;
}
