#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10;
int cnt[26];
queue<int> poses[2][26];
set<int> st;
vector<pair<int, int> > ans;
string s;
int N;
struct fenwick {
  int fn[maxn];
  void reset() { fill(fn, fn + N + 5, 0); }
  void add(int pos) {
    for (pos += 3; pos < N + 5; pos += pos & -pos) fn[pos]++;
  }
  int ask(int pos) {
    int ans = 0;
    for (pos += 3; pos > 0; pos -= pos & -pos) ans += fn[pos];
    return ans;
  }
};
fenwick fn;
pair<int, int> fnd(int c) {
  while (int((poses[0][c]).size())) {
    int x = poses[0][c].front();
    auto it = st.upper_bound(x);
    if (st.count(x) && it != st.end() && s[x] != s[*it]) return {x, *it};
    poses[0][c].pop();
  }
  while (int((poses[1][c]).size())) {
    int x = poses[1][c].front();
    auto it = st.lower_bound(x);
    if (st.count(x) && it != st.begin() && s[x] != s[*prev(it)])
      return {*prev(it), x};
    poses[1][c].pop();
  }
  return {-1, -1};
}
void del(int a, int b) {
  auto it1 = st.find(a);
  auto it2 = st.find(b);
  if (it1 != st.begin() && next(it2) != st.end()) {
    int i = *prev(it1), j = *next(it2);
    if (s[i] != s[j])
      poses[0][s[i] - 'a'].push(i), poses[1][s[j] - 'a'].push(j);
  }
  cnt[s[a] - 'a']--, cnt[s[b] - 'a']--;
  ans.push_back({a, b});
  st.erase(it1), st.erase(it2);
}
void solve() {
  cin >> s;
  N = int((s).size());
  fn.reset();
  vector<pair<int, char> > vec;
  for (int i = 0; i < 26; i++) {
    cnt[i] = 0;
  }
  for (int i = 0; i < int((s).size()) - 1; i++) {
    if (s[i] == s[i + 1]) vec.push_back({i, s[i]}), cnt[s[i] - 'a']++;
  }
  for (int i = 0; i < 26; i++) {
    while (int((poses[0][i]).size())) poses[0][i].pop();
    while (int((poses[1][i]).size())) poses[1][i].pop();
  }
  st.clear();
  ans.clear();
  for (int i = 0; i < int((vec).size()); i++) {
    st.insert(vec[i].first);
  }
  for (int i = 0; i < int((vec).size()) - 1; i++) {
    if (vec[i].second != vec[i + 1].second) {
      poses[0][vec[i].second - 'a'].push(vec[i].first);
      poses[1][vec[i + 1].second - 'a'].push(vec[i + 1].first);
    }
  }
  while (true) {
    int mx = 0;
    for (int i = 1; i < 26; i++) {
      if (cnt[i] > cnt[mx]) mx = i;
    }
    pair<int, int> p = fnd(mx);
    if (p.first == -1) break;
    del(p.first, p.second);
  }
  vector<pair<int, int> > ANS;
  set<int> ST;
  for (int i = 0; i < N; i++) ST.insert(i);
  for (pair<int, int> p : ans) {
    auto L = ST.upper_bound(p.first), R = ST.upper_bound(p.second);
    ANS.push_back({*L - fn.ask(*L), *prev(R) - fn.ask(*prev(R))});
    for (auto it = L; it != R; it++) fn.add(*it);
    ST.erase(L, R);
  }
  string ss;
  for (auto i : ST) ss += s[i];
  int bef = 0;
  for (int i = 1; i < int((ss).size()); i++) {
    if (ss[i] == ss[i - 1]) ANS.push_back({0, i - 1 - bef}), bef = i;
  }
  ANS.push_back({0, int((ss).size()) - 1 - bef});
  cout << int((ANS).size()) << "\n";
  for (pair<int, int> p : ANS) {
    cout << p.first + 1 << " " << p.second + 1 << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie();
  int q;
  cin >> q;
  while (q--) {
    solve();
  }
  return 0;
}
