#include <cstdio>
#include <vector>
using namespace std;
const char ch[3] = {'w', 'i', 'n'};
int t, m, id[256];
char s[4];
vector<int> vec[3][3];
struct node{
int a, b, c, d;
node(int a, int b, int c, int d) : a(a), b(b), c(c), d(d){}
};
vector<node> ans;
void f(int i, int x, int y){
if(vec[y][x].size()){
ans.push_back(node(i, x, vec[y][x].back(), y));
vec[y][x].pop_back();
}
else
vec[x][y].push_back(i);
}
int main(){
id['w'] = 0;
id['i'] = 1;
id['n'] = 2;
scanf("%d", &t);
while(t--){
ans.clear();
scanf("%d", &m);
for (int i = 1; i <= m; i++){
scanf("%s", s);
bool have[3] = {};
for (char *c = s; *c; c++)
have[id[*c]] = true;
bool havehave[3] = {};
for (char *c = s; *c; c++){
if(!havehave[id[*c]])
havehave[id[*c]] = true;
else
for (int j = 0; j < 3; j++)
if(!have[j]){
f(i, id[*c], j);
have[j] = true;
break;
}
}
}
while(vec[0][1].size()){
ans.push_back(node(vec[0][1].back(), 0, vec[1][2].back(), 1));
ans.push_back(node(vec[1][2].back(), 0, vec[2][0].back(), 2));
vec[0][1].pop_back();
vec[1][2].pop_back();
vec[2][0].pop_back();
}
while(vec[0][2].size()){
ans.push_back(node(vec[0][2].back(), 0, vec[2][1].back(), 2));
ans.push_back(node(vec[2][1].back(), 0, vec[1][0].back(), 1));
vec[0][2].pop_back();
vec[2][1].pop_back();
vec[1][0].pop_back();
}
printf("%d\n", (int)ans.size());
for(auto nd : ans)
printf("%d %c %d %c\n", nd.a, ch[nd.b], nd.c, ch[nd.d]);
}
return 0;
}
17. Letter Combinations of a Phone Number | 5. Longest Palindromic Substring |
3. Longest Substring Without Repeating Characters | 1312. Minimum Insertion Steps to Make a String Palindrome |
1092. Shortest Common Supersequence | 1044. Longest Duplicate Substring |
1032. Stream of Characters | 987. Vertical Order Traversal of a Binary Tree |
952. Largest Component Size by Common Factor | 212. Word Search II |
174. Dungeon Game | 127. Word Ladder |
123. Best Time to Buy and Sell Stock III | 85. Maximal Rectangle |
84. Largest Rectangle in Histogram | 60. Permutation Sequence |
42. Trapping Rain Water | 32. Longest Valid Parentheses |
Cutting a material | Bubble Sort |
Number of triangles | AND path in a binary tree |
Factorial equations | Removal of vertices |
Happy segments | Cyclic shifts |
Zoos | Build a graph |
Almost correct bracket sequence | Count of integers |