#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <cassert>
#include <queue>
#include <deque>

using namespace std;

typedef deque<pair<char, int> > path_t;

path_t result_path;

struct elem
{
	char let;
	int count;
	bool changed;
	elem(char l, int c) { let = l; count = c; changed = false; }
};

typedef list<elem> list_t;

struct state
{
	list_t data;
	int length;

	state(int len)
	{
		length = len;
	}

	state()
	{
		int len = 0;
		length = len;
	}
};

bool operator < (const elem &a, const elem &b)
{
	if (a.let != b.let)
		return a.let < b.let;
	return a.count < b.count;
}

bool operator < (const state &a, const state &b)
{
	return a.data < b.data;
}

state to_state(string s)
{
	state res(s.length());
	res.data.push_back(elem(s[0], 1));
	for (int i = 1;i < s.length();i++)
	{
		list_t::iterator last = res.data.end();
		last--;
		if (s[i] == last->let)
			last->count++;
		else
			res.data.push_back(elem(s[i], 1));
	}
	return res;
}

void reduce_state(state &s)
{
	bool changed;
	do
	{
		changed = false;
		for (list_t::iterator it = s.data.begin();it != s.data.end();)
			if (it->count >= 3 && it->changed)
			{
				list_t::iterator next_it = it;
				next_it++;
				s.length -= it->count;
				s.data.erase(it);
				it = next_it;
				if (it != s.data.end())
					it->changed = true;
				changed = true;
			}
			else
				it++;

		for (list_t::iterator it = s.data.begin();it != s.data.end();)
		{
			list_t::iterator neighbour = it;
			neighbour++;
			if (neighbour == s.data.end())
				break;
			if (neighbour->let == it->let && (neighbour->changed || it->changed))
			{				
				neighbour->count += it->count;
				s.data.erase(it);
				it = neighbour;
				changed = true;
			}
			else
				it++;
		}
	}
	while (changed);

	for (list_t::iterator it = s.data.begin();it != s.data.end();it++)
		it->changed = false;
}

int bfs(state start)
{
	map<state, int> dist;
	map<state, path_t> path_to_state;
	dist[start] = 0;
	path_to_state[start] = path_t();
	queue<state> q;
	q.push(start);
	while (!q.empty())
	{
		state v = q.front();
		q.pop();
		int dv = dist[v];
		path_t pathv = path_to_state[v];
		if (v.length == 0)
		{
			result_path = pathv;
			return dv;
		}

		vector<state> new_states(v.data.size(), v);
		vector<list_t::iterator> state_iters(v.data.size());
		vector<int> state_pos(v.data.size(), 0);
		vector<path_t> state_path(v.data.size(), pathv);
		for (int i = 0;i < new_states.size();i++)
			state_iters[i] = new_states[i].data.begin();
		
		int state_num = 0;
		for (list_t::iterator it = v.data.begin();it != v.data.end();it++)
		{
			state_iters[state_num]->count++;
			state_iters[state_num]->changed = true;
			new_states[state_num].length++;
			state_path[state_num].push_back(make_pair(state_iters[state_num]->let, state_pos[state_num]));
			reduce_state(new_states[state_num]);			
			state_num++;
			for (int i = state_num + 1;i < new_states.size();i++)
			{
				state_pos[i] += state_iters[i]->count;
				state_iters[i]++;
			}
		}

		for (int i = 0;i < new_states.size();i++)
		{
			map<state, int>::iterator found = dist.find(new_states[i]);
			if (found != dist.end())
				continue;

			dist[new_states[i]] = dv + 1;
			q.push(new_states[i]);
			path_to_state[new_states[i]] = state_path[i];
		}
	}
	return -1;
}

int main()
{
	ifstream in("game.in");

	int tc;
	in >>tc;
	while (tc--)
	{
		string input;
		in >>input;
		assert(input.length() <= 14);
		cout <<bfs(to_state(input));
		for (int i = 0;i < result_path.size();i++)
			cout <<" " <<result_path[i].first <<result_path[i].second;
		cout <<endl;
	}

	return 0;
}
