E. Masha-forgetful
/*
Name: CF1624E. Masha-forgetful
Copyright:
Author:
Date: 23/01/22 14:35
Description:
该问题解法的核心是,一个长度大于3的字符串总能分解为若干个长度为2或3的字符串。
*/
#ifndef DEBUG
#pragma GCC optimize("O2")
#endif
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<limits.h>
#include<assert.h>
using namespace std;
#define all(X) (X).begin(), (X).end()
#define rep(I,A,B) for(int I=(A);I<(B);I++)
#define repd(I,A,B) for(int I=(A);I>(B);I--)
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<LL> VLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &&head, U &&... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W("{"); _W(x.first); putchar(','); _W(x.second); _W("}");}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() { }
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
typedef pair<PII,int> P;
void output(map<string,PII> &d, string &s, int i, int j, VI &prev, vector<P> &ans) {
if(j==0)
return;
output(d,s,prev[i],i,prev,ans);
string t;
rep(k,i+1,j+1)
t += s[k];
int l=t.length();
ans.push_back({{d[t].second, d[t].second+l-1}, d[t].first});
}
void solve(int n, int m, vector<string> &a, string s) {
map<string,PII> d; // d['aaa'] = {所属的电话号码,起始下标}
rep(i,1,n+1) {
rep(j,1,m+1) {
string t;t+=a[i][j];
if(j+1<=m) {
t+=a[i][j+1];
d[t] = {i,j};
}
if(j+2<=m) {
t+=a[i][j+2];
d[t] = {i,j};
}
}
}
VI f(m+1),prev(m+1); // f[i]表示能否拼出s[0~m]
f[0]=true;
rep(i,2,m+1) {
string t;t=s[i];
if(i-1>=0) {
t=s[i-1]+t;
if(d.find(t) != d.end()) {
f[i]=(i-2>=0?f[i-2]:false);
if(f[i]) prev[i]=i-2;
}
}
if(i-2>=0) {
t=s[i-2]+t;
if(d.find(t) != d.end() && !f[i]) {
f[i]=(i-3>=0?f[i-3]:false);
if(f[i]) prev[i]=i-3;
}
}
}
if(f[m]==0)
W(-1);
else {
vector<P> ans;
output(d,s,prev[m],m,prev,ans); // (i,j]
W(ans.size());
rep(i,0,ans.size()) {
W(ans[i].first.first, ans[i].first.second, ans[i].second);
}
}
}
int main()
{
#ifdef DEBUG
freopen("1.in","r",stdin);
//freopen("tmp.out","w",stdout);
#endif
int T; R(T);
while(T--) {
int n,m; // 电话号码的数量和长度
R(n,m);
vector<string> a(n+1);
rep(i,1,n+1) {
string t; R(t);
a[i]='0'+t; // 添加一个0方便处理
}
string s;R(s); // 要分解的串
solve(n,m,a,'0'+s);
//break;
}
return 0;
}