#include <bits/stdc++.h>
using namespace std;
int fastio() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  cin.tie(nullptr);
  return 0;
}
int __fastio = fastio();
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& p);
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  string sep = "";
  for (auto e : v) os << sep << e, sep = ", ";
  return os << "}";
}
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
const int inf = 1000 << 20;
const int MAXN = 1 << 18;
const int MAXC = 222;
int cnts[MAXC];
int val() {
  int tot = 0;
  int mx = 0;
  for (char c = 'a'; c <= 'z'; ++c) {
    mx = max(mx, cnts[int(c)]);
    tot += cnts[int(c)];
  }
  return max((tot + 1) / 2, mx);
}
int v1;
bool can(char a, char b) {
  if (a == b) return false;
  --cnts[int(a)];
  --cnts[int(b)];
  int v2 = val();
  ++cnts[int(a)];
  ++cnts[int(b)];
  return v2 + 1 == v1;
}
int seg[MAXN * 2];
int SEGZ;
int ask(int x) {
  int ans = 0;
  for (int i = x + SEGZ, j = SEGZ; i > j; i /= 2, j /= 2) {
    if (i % 2 == 1) {
      ans += seg[i - 1];
    }
  }
  return ans;
}
int main() {
  int T;
  cin >> T;
  for (int t = 1; t <= T; ++t) {
    memset(cnts, 0, sizeof(cnts));
    string s;
    cin >> s;
    int n = (int((s).size()));
    for (int i = 0; i + 1 < n; ++i) {
      if (s[i] == s[i + 1]) ++cnts[int(s[i])];
    }
    vector<pair<int, int> > stk;
    vector<pair<int, int> > ans;
    v1 = val();
    for (int i = 0; i + 1 < n; ++i) {
      if (s[i] == s[i + 1]) {
        if (!stk.empty() && can(stk.back().first, s[i])) {
          --cnts[stk.back().first];
          --cnts[int(s[i])];
          v1 = val();
          ans.push_back(pair<int, int>(stk.back().second, i + 1));
          stk.pop_back();
        } else {
          stk.push_back(pair<int, int>(s[i], i + 1));
        }
      }
    }
    for (auto k : stk) {
      ans.push_back(pair<int, int>(0, k.second));
    }
    ans.push_back(pair<int, int>(0, n));
    ;
    SEGZ = 1;
    while (SEGZ <= n) SEGZ *= 2;
    for (int i = 0; i < n; ++i) {
      seg[SEGZ + i] = 1;
    }
    for (int i = SEGZ - 1; i > 0; --i) {
      seg[i] = seg[i * 2] + seg[i * 2 + 1];
    }
    cout << (int((ans).size())) << "\n";
    for (auto p : ans) {
      int a = ask(p.first);
      int b = ask(p.second);
      seg[p.first + SEGZ] -= b - a;
      for (int i = (p.first + SEGZ) / 2; i > 0; i /= 2) {
        seg[i] = seg[i * 2] + seg[i * 2 + 1];
      }
      cout << a + 1 << " " << b << "\n";
    }
  }
}
