#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct BIT {
  int n;
  vector<T> dat;
  BIT(int n = 0) { initialize(n); }
  void initialize(int nin) {
    n = nin;
    dat.resize(n, 0);
  }
  T sum(int i) {
    T s = 0;
    while (i >= 0) {
      s += dat[i];
      i = (i & (i + 1)) - 1;
    }
    return s;
  }
  T sum_between(int i, int j) { return sum(j) - sum(i - 1); }
  void plus(int i, T x) {
    while (i < n) {
      dat[i] += x;
      i |= i + 1;
    }
  }
  int lower_bound(T x) {
    if (x < 0) return -1;
    if (sum(n - 1) < x) return n;
    int ret = -1;
    int k = 1;
    while (2 * k <= n) k <<= 1;
    for (; k > 0; k >>= 1) {
      if (ret + k < n && dat[ret + k] < x) {
        x -= dat[ret + k];
        ret += k;
      }
    }
    return ret + 1;
  }
};
void solve() {
  string S;
  cin >> S;
  vector<pair<int, int>> D;
  for (int i = 0; i < int(S.size()) - 1; i++)
    if (S[i] == S[i + 1]) {
      D.emplace_back(S[i] - 'a', i + 1);
    }
  int N = D.size();
  vector<int> num(26);
  BIT<int> bit[26], bit_sum(N);
  for (int k = 0; k < 26; k++) bit[k].initialize(N);
  for (int i = 0; i < N; i++) {
    int al = D[i].first;
    num[al]++;
    bit[al].plus(i, 1);
    bit_sum.plus(i, D[i].second - (i ? D[i - 1].second : 0));
  }
  int sum = N;
  int total = S.size();
  vector<pair<int, int>> ans;
  while (true) {
    int mx_al = max_element(num.begin(), num.end()) - num.begin();
    if (num[mx_al] == sum) break;
    int fst = bit[mx_al].lower_bound(1);
    int r = N, l = -1;
    for (int k = 0; k < 26; k++)
      if (k != mx_al) {
        int pos = bit[k].lower_bound(bit[k].sum(fst) + 1);
        r = min(r, pos);
      }
    if (r == N) {
      r = fst;
      for (int k = 0; k < 26; k++)
        if (k != mx_al) {
          int pos = bit[k].lower_bound(bit[k].sum(fst));
          l = max(l, pos);
        }
    } else {
      l = bit[mx_al].lower_bound(bit[mx_al].sum(r));
    }
    int LL = bit_sum.sum(l);
    int RR = bit_sum.sum(r);
    ans.emplace_back(LL, RR);
    bit_sum.plus(l + 1, LL - RR);
    total -= RR - LL;
    sum -= 2;
    num[D[l].first]--;
    num[D[r].first]--;
    bit[D[l].first].plus(l, -1);
    bit[D[r].first].plus(r, -1);
  }
  for (int i = 0; i < N; i++) {
    int s = bit[D[i].first].sum_between(i, i);
    if (s > 0) {
      int rem = bit_sum.sum(i);
      ans.emplace_back(0, rem);
      bit_sum.plus(0, -rem);
      total -= rem;
    }
  }
  ans.emplace_back(0, total);
  printf("%d\n", int(ans.size()));
  for (auto& p : ans) printf("%d %d\n", p.first + 1, p.second);
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
  return 0;
}
