/*gyt
Live up to every day*/#include#include#include#include#include#include#include`
#include#include#include#include#include
#define PI acos(-1)
using namespacestd;
typedeflong longll;
typedefdoubledb;const int maxn = 6000005;const ll maxm =1e7;const ll mod = 1e9 + 7;const int INF = 0x3f3f3f;const ll inf = 1e15 + 5;const db eps = 1e-9;const int kind=26;structnode{
node*fail;
node*next[kind];intcoun;voidnodee() {
fail=NULL;
coun=0;for (int i=0; i
next[i]=NULL;
}
}*root;charstr[maxn];chartmp[maxn];int ans=0;voidupdata() {
node*p=root;int len=strlen(str);for (int i=0; inext[pos]==NULL) {
p->next[pos]=newnode;
p->next[pos]->nodee();
p=p->next[pos];
}else p=p->next[pos];
}
p->coun++;
}voidgetfail() {
node*p=root, *son, *tmp;
queueque;
que.push(p);while(!que.empty()) {
tmp=que.front();
que.pop();for (int i=0; i<26; i++) {
son=tmp->next[i];if (son!=NULL) {if (tmp==root) {
son->fail=root;
}else{
p=tmp->fail;while(p) {if (p->next[i]) {
son->fail=p->next[i];break;
}
p=p->fail;
}if (!p) son->fail=root;
}
que.push(son);
}
}
}
}voidquery() {int len=strlen(str);
node*p, *tmp;
p=root;int cnt=0;for (int i=0; inext[pos]&& p!=root) p=p->fail;
p=p->next[pos];if (!p) p=root;
tmp=p;while(tmp!=root) {if (tmp->coun>=0) {
cnt+=tmp->coun;
tmp->coun=-1;
}else break;
tmp=tmp->fail;
}
}
ans+=cnt;
}voidsolve() {
ans=0;
root=newnode;
root->nodee();
root->fail=NULL;int n; scanf("%d", &n);
getchar();for (int i=0; i
gets(str); updata();
}
getfail();
gets(str);int len=strlen(str);int nn=0; charc;int now=0; int cnt=0;while(1) {if (now>=len) break;if (str[now]=='[') {
now++;while(str[now]>='0'&&str[now]<='9') {
nn= nn*10+(str[now]-'0');
now++;
}
c=str[now];for (int i=0; i
tmp[cnt++]=c;
nn=0;
now++;
now++;
}else{//if (str[now]==']') {//now++; continue;//}
tmp[cnt++]=str[now]; now++;
}//cout<
}
tmp[cnt]='\0';for (int i=0; i<=cnt; i++) str[i]=tmp[i];//cout<
query();
reverse(str, str+strlen(str));//cout<
query();
cout<
}intmain() {int t = 1;//freopen("in.txt", "r", stdin);
scanf("%d", &t);while(t--)
solve();return 0;
}