#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int T, n;
char s[N];
vector<pair<char, int> > vc, sta;
vector<pair<int, int> > pr;
struct Counter {
  int cnt[127], ccnt[N], mx;
  void init() {
    for (int i = 'a'; i <= 'z'; ++i) cnt[i] = 0;
    for (int i = 1; i <= mx; ++i) ccnt[i] = 0;
    mx = 0;
    ccnt[0] = 26;
  }
  void modify(int x, int v) {
    --ccnt[cnt[x]];
    ++ccnt[cnt[x] += v];
    if (ccnt[mx + 1]) ++mx;
    if (!ccnt[mx]) --mx;
  }
} ct;
int d[N << 2], tag[N << 2];
void build(int l, int r, int o) {
  d[o] = 0, tag[o] = 0;
  if (l < r) {
    const int mid = (l + r) / 2;
    build(l, mid, o << 1);
    build(mid + 1, r, o << 1 | 1);
  }
}
void pushdown(int o, int len) {
  if (tag[o]) {
    tag[o << 1] = tag[o << 1 | 1] = 1;
    tag[o] = 0;
    d[o << 1] = (len + 1) / 2, d[o << 1 | 1] = len / 2;
  }
}
void modify(int l, int r, int o, const int& L, const int& R) {
  if (L <= l && r <= R) {
    d[o] = r - l + 1, tag[o] = 1;
  } else {
    pushdown(o, r - l + 1);
    const int mid = (l + r) / 2;
    if (L <= mid) modify(l, mid, o << 1, L, R);
    if (mid < R) modify(mid + 1, r, o << 1 | 1, L, R);
    d[o] = d[o << 1] + d[o << 1 | 1];
  }
}
int query(int l, int r, int o, const int& L, const int& R) {
  if (L <= l && r <= R) return d[o];
  pushdown(o, r - l + 1);
  const int mid = (l + r) / 2;
  int res = 0;
  if (L <= mid) res = query(l, mid, o << 1, L, R);
  if (mid < R) res += query(mid + 1, r, o << 1 | 1, L, R);
  return res;
}
void work() {
  cin >> (s + 1), n = strlen(s + 1);
  pr.clear();
  vc.clear();
  ct.init();
  for (int i = 2; i <= n; ++i)
    if (s[i] == s[i - 1]) vc.emplace_back(s[i - 1], i - 1);
  for (auto it : vc) ct.modify(it.first, 1);
  sta.clear();
  int m = vc.size();
  for (auto it : vc)
    if (sta.empty() || it.first == sta.back().first)
      sta.emplace_back(it);
    else {
      int cf = sta.back().first, cs = it.first;
      ct.modify(cf, -1);
      ct.modify(cs, -1);
      if (ct.mx * 2 <= m - 2) {
        pr.emplace_back(sta.back().second + 1, it.second);
        sta.pop_back();
        m -= 2;
      } else {
        ct.modify(cf, 1);
        ct.modify(cs, 1);
        sta.emplace_back(it);
      }
    }
  vc = sta;
  sta.clear();
  bool flg = 2 * ct.mx > m;
  for (auto it : vc)
    if (sta.empty() || it.first == sta.back().first || flg)
      sta.emplace_back(it);
    else {
      int cf = sta.back().first, cs = it.first;
      ct.modify(cf, -1);
      ct.modify(cs, -1);
      pr.emplace_back(sta.back().second + 1, it.second);
      sta.pop_back();
      m -= 2;
      flg = 1;
    }
  vc = sta;
  sta.clear();
  for (auto it : vc) {
    if (sta.empty() || it.first == sta.back().first)
      sta.emplace_back(it);
    else {
      int cf = sta.back().first, cs = it.first;
      if (ct.cnt[cf] == ct.mx || ct.cnt[cs] == ct.mx) {
        ct.modify(cf, -1);
        ct.modify(cs, -1);
        pr.emplace_back(sta.back().second + 1, it.second);
        sta.pop_back();
        m -= 2;
      } else
        sta.emplace_back(it);
    }
  }
  sta.emplace_back(s[n], n);
  for (auto it : sta) pr.emplace_back(1, it.second);
  cout << pr.size() << '\n';
  build(1, n, 1);
  for (auto it : pr) {
    int l = it.first, r = it.second;
    if (l > 1) l -= query(1, n, 1, 1, l - 1);
    if (r > 1) r -= query(1, n, 1, 1, r - 1);
    cout << l << ' ' << r << '\n';
    modify(1, n, 1, it.first, it.second);
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  for (cin >> T; T--;) work();
  return 0;
}
