#include <bits/stdc++.h>
using namespace std;
template <typename T>
class BIT {
 public:
  int size;
  vector<T> arr;
  BIT(int n) {
    size = n + 5;
    arr.resize(size);
  }
  void add(int i, T v) {
    for (int x = i + 1; x < size; x += (x & (-x))) arr[x] += v;
  }
  T sum(int i) {
    i = min(i, size - 2);
    T res = 0;
    for (int x = i + 1; x > 0; x -= (x & (-x))) res += arr[x];
    return res;
  }
  int lower_bound(T v) {
    int b = 1;
    while (b < (size - 1)) b *= 2;
    b /= 2;
    int res = 0;
    for (int k = b; k > 0; k /= 2) {
      if ((res + k) < size && arr[res + k] < v) {
        v -= arr[res + k];
        res += k;
      }
    }
    return res;
  }
};
template <typename T>
class seg_update_sum {
 public:
  vector<T> arr;
  vector<bool> flag;
  int size;
  int n;
  seg_update_sum(int m) {
    n = 1;
    while (n < m) n *= 2;
    size = n * 2;
    arr.resize(size);
    flag.resize(size);
    fill(flag.begin(), flag.end(), false);
  }
  void range_update(int left, int right, T v) {
    left = max(left + n, n), right = min(right + n, size - 1);
    if (left > right) return;
    int le = left, ri = right;
    propagate(left);
    propagate(right);
    for (int k = 0; left <= right;
         k++, left = (left + 1) >> 1, right = (right - 1) >> 1) {
      if (left & 1) {
        arr[left] = (v << k);
        flag[left] = true;
      }
      if (!(right & 1)) {
        arr[right] = (v << k);
        flag[right] = true;
      }
    }
    pop_up(le);
    pop_up(ri);
  }
  void propagate(int i) {
    int k = 0;
    while (i >> k) k++;
    k--;
    for (; k >= 0; k--) {
      int j = i >> k;
      if (!flag[j]) continue;
      if (k > 0) {
        arr[2 * j] = arr[j] >> 1;
        flag[2 * j] = true;
        arr[2 * j + 1] = arr[j] >> 1;
        flag[2 * j + 1] = true;
      }
      flag[j] = false;
    }
  }
  void pop_up(int i) {
    for (int k = 0; i > 1; k++, i /= 2) {
      if (!flag[i >> 1]) arr[i >> 1] = arr[i] + arr[i ^ 1];
    }
  }
  T range_sum(int left, int right) {
    left = max(left + n, n), right = min(right + n, size - 1);
    if (left > right) return 0;
    propagate(left);
    propagate(right);
    T res = 0;
    for (; left <= right; left = (left + 1) >> 1, right = (right - 1) >> 1) {
      if (left & 1) {
        res += arr[left];
      }
      if (!(right & 1)) {
        res += arr[right];
      }
    }
    return res;
  }
};
vector<pair<int, int>> sol(vector<pair<int, int>> &a) {
  int n = (int)a.size();
  vector<int> b(n - 1);
  for (int i = 1; i < n; i++) {
    b[i - 1] = a[i].first;
  }
  int m = n - 1;
  vector<int> kos(26);
  int mx = 0;
  for (int i = 0; i < m; i++) {
    kos[b[i]]++;
    mx = max(mx, kos[b[i]]);
  }
  vector<BIT<int>> ta;
  for (int i = 0; i < 26; i++) {
    BIT<int> bit(n + 10);
    ta.emplace_back(bit);
  }
  for (int i = 0; i < m; i++) {
    ta[b[i]].add(i, 1);
  }
  vector<pair<int, int>> res;
  while (true) {
    int maxind = 0;
    for (int i = 0; i < 26; i++) {
      if (kos[maxind] < kos[i]) maxind = i;
    }
    if (kos[maxind] == 0) break;
    int pos = ta[maxind].lower_bound(1);
    int lefts = -1;
    int ii = -1;
    for (int i = 0; i < 26; i++) {
      if (i == maxind) continue;
      int v = ta[i].sum(pos);
      if (v == 0) continue;
      int pos2 = ta[i].lower_bound(v);
      if (lefts < pos2) {
        ii = i;
        lefts = pos2;
      }
    }
    if (lefts == -1) {
      int rights = n + 10;
      int jj = -1;
      for (int i = 0; i < 26; i++) {
        if (i == maxind) continue;
        int v = ta[i].sum(pos);
        if (v == kos[i]) continue;
        int pos2 = ta[i].lower_bound(v + 1);
        if (pos2 < rights) {
          rights = pos2;
          jj = i;
        }
      }
      if (rights < n) {
        int v = ta[maxind].sum(rights);
        pos = ta[maxind].lower_bound(v);
        lefts = rights;
        ii = jj;
      }
    }
    if (lefts == -1) {
      kos[maxind]--;
      ta[maxind].add(pos, -1);
      res.push_back(make_pair(pos, pos));
    } else {
      kos[maxind]--;
      ta[maxind].add(pos, -1);
      ta[ii].add(lefts, -1);
      kos[ii]--;
      res.push_back(make_pair(pos, lefts));
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  while (tt--) {
    string str;
    cin >> str;
    int n = (int)str.size();
    vector<pair<int, int>> ran;
    vector<pair<int, int>> inds;
    for (int i = 0; i < n; i++) {
      int j = i + 1;
      while (j < n && str[j] != str[j - 1]) j++;
      j--;
      ran.push_back(make_pair((int)(str[i] - 'a'), (int)(str[j] - 'a')));
      inds.push_back(make_pair(i, j));
      i = j;
    }
    vector<pair<int, int>> a = sol(ran);
    vector<pair<int, int>> res;
    for (int i = 0; i < (int)a.size(); i++) {
      int le = a[i].first;
      int ri = a[i].second;
      if (le > ri) swap(le, ri);
      if (le == ri) {
        int left = inds[le + 1].first;
        int right = inds[le + 1].second;
        res.push_back(make_pair(left, right));
      } else {
        int left = inds[le + 1].first;
        int right = inds[ri].second;
        res.push_back(make_pair(left, right));
      }
    }
    res.push_back(make_pair(0, n - 1));
    cout << res.size() << "\n";
    seg_update_sum<int> seg(n);
    seg.range_update(0, n - 1, 1);
    for (int i = 0; i < (int)res.size(); i++) {
      int le = res[i].first;
      int ri = res[i].second;
      int left = seg.range_sum(0, le);
      int right = seg.range_sum(0, ri);
      cout << left << " " << right << "\n";
      seg.range_update(le, ri, 0);
    }
  }
  return 0;
}
