#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const unsigned gen_seed =
    std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);
class stree {
 public:
  std::vector<int> t;
  int s;
  void build(int n, int v, int tl, int tr) {
    if (v == 1) {
      t.resize(n * 4);
      s = n;
    }
    if (tl == tr)
      t[v] = 0;
    else {
      int tm = (tl + tr) / 2;
      build(n, v * 2, tl, tm);
      build(n, v * 2 + 1, tm + 1, tr);
      t[v] = t[v * 2] + t[v * 2 + 1];
    }
  }
  int get(int v, int tl, int tr, int l, int r) {
    if (l > r) return 0;
    if (l == tl && r == tr) return t[v];
    int tm = (tl + tr) / 2;
    return (get(v * 2, tl, tm, l, min(r, tm)) +
            get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
  }
  void update(int v, int tl, int tr, int pos, int new_val) {
    if (tl == tr)
      t[v] = new_val;
    else {
      int tm = (tl + tr) / 2;
      if (pos <= tm)
        update(v * 2, tl, tm, pos, new_val);
      else
        update(v * 2 + 1, tm + 1, tr, pos, new_val);
      t[v] = (t[v * 2] + t[v * 2 + 1]);
    }
  }
};
set<int> let;
stree st;
int n;
std::pair<int, int> get(int l, int r) {
  auto it = let.lower_bound(l);
  std::vector<int> todel;
  while (it != let.end() && *it <= r) {
    todel.push_back(*it);
    it++;
  }
  int l1 = l - st.get(1, 0, n - 1, 0, l - 1);
  int r1 = l1 + r - l - st.get(1, 0, n - 1, l, r);
  for (auto x : todel) {
    st.update(1, 0, n - 1, x, 1);
    let.erase(x);
  }
  return make_pair(l1, r1);
}
int k;
std::vector<std::pair<int, int>> bad;
std::vector<std::pair<int, int>> ans;
std::vector<int> tol, tor;
std::vector<int> cbad;
set<std::pair<int, int>> has;
map<std::pair<int, int>, set<std::pair<int, int>>> w;
int sum;
std::vector<int> bi;
void rem(int i, int j) {
  if (i < 0 || j >= k) return;
  int mn = min(bi[i], bi[j]);
  int mx = max(bi[i], bi[j]);
  std::pair<int, int> u = make_pair(mn, mx);
  w[u].erase(make_pair(i, j));
  if (mn != mx)
    if (w[u].empty()) has.erase(u);
}
void app(int i, int j) {
  if (i < 0 || j >= k) return;
  int mn = min(bi[i], bi[j]);
  int mx = max(bi[i], bi[j]);
  std::pair<int, int> u = make_pair(mn, mx);
  if (mn != mx)
    if (w[u].empty()) has.insert(u);
  w[u].insert(make_pair(i, j));
}
void cap(int i) {
  int lx = tol[i];
  int rx = tor[i];
  if (lx >= 0) tor[lx] = rx;
  if (rx < k) tol[rx] = lx;
  rem(lx, i);
  rem(i, rx);
  app(lx, rx);
}
void go1(int i) {
  ans.push_back(get(bad[i].second, bad[i].second));
  bad[i].second--;
  cbad[bi[i]]--;
  sum--;
  if (bad[i].second == bad[i].first) {
    cap(i);
  }
}
void go(int i, int j) {
  ans.push_back(get(bad[i].second, bad[j].first));
  bad[i].second--;
  cbad[bi[i]]--;
  cbad[bi[j]]--;
  sum -= 2;
  if (bad[i].second == bad[i].first) {
    cap(i);
  }
  bad[j].first++;
  if (bad[j].second == bad[j].first) {
    cap(j);
  }
}
int main() {
  char c[200100];
  int t;
  scanf("%d", &t);
  while (t--) {
    ans.clear();
    scanf("%s", c);
    string s(c);
    n = s.size();
    st.build(n, 1, 0, n - 1);
    let.clear();
    for (int i = 0; i < n; i++) let.insert(i);
    int pt = 0;
    bad.clear();
    cbad = std::vector<int>(26, 0);
    bi.clear();
    while (pt < n) {
      int ptr = pt;
      while (ptr < n && s[ptr] == s[pt]) ptr++;
      if (ptr > pt + 1) {
        bad.push_back(make_pair(pt, ptr - 1));
        bi.push_back(int(s[pt] - 'a'));
      }
      int xc = s[pt] - 'a';
      cbad[xc] += ptr - pt - 1;
      pt = ptr;
    }
    has.clear();
    k = bad.size();
    tor = std::vector<int>(k, 0);
    tol = std::vector<int>(k, 0);
    sum = 0;
    for (int i = 0; i < 26; i++) sum += cbad[i];
    for (int i = 0; i < k; i++) tor[i] = i + 1;
    for (int i = 0; i < k; i++) tol[i] = i - 1;
    for (int i = 0; i < k; i++) app(i, i + 1);
    if (sum % 2 == 1) {
      int pt = 0;
      for (int i = 0; i < 26; i++)
        if (cbad[i] > cbad[pt]) pt = i;
      for (int i = 0; i < k; i++)
        if (bi[i] == pt) {
          go1(i);
          break;
        }
    }
    int mark = -1;
    while (1) {
      int pt = 0;
      for (int i = 0; i < 26; i++)
        if (2 * cbad[i] >= sum) {
          mark = i;
          break;
        }
      if (mark >= 0) break;
      auto u = *has.begin();
      go(w[u].begin()->first, w[u].begin()->second);
    }
    while (cbad[mark] != sum) {
      for (int i = 0; i < 26; i++) {
        if (i == mark) continue;
        std::pair<int, int> u = make_pair(min(i, mark), max(i, mark));
        while (!w[u].empty()) {
          go(w[u].begin()->first, w[u].begin()->second);
        }
      }
    }
    for (int i = 0; i < k; i++)
      while (bad[i].first != bad[i].second) go1(i);
    ans.push_back(get(0, n - 1));
    printf("%d\n", ans.size());
    for (auto x : ans) {
      printf("%d %d\n", x.first + 1, x.second + 1);
    }
  }
}
