/***********Template Starts Here***********/
#include <bits/stdc++.h>
#define pb push_back
#define nl puts ("")
#define sp printf ( " " )
#define phl printf ( "hello\n" )
#define ff first
#define ss second
#define POPCOUNT __builtin_popcountll
#define RIGHTMOST __builtin_ctzll
#define LEFTMOST(x) (63-__builtin_clzll((x)))
#define MP make_pair
#define FOR(i,x,y) for(vlong i = (x) ; i <= (y) ; ++i)
#define ROF(i,x,y) for(vlong i = (y) ; i >= (x) ; --i)
#define CLR(x,y) memset(x,y,sizeof(x))
#define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define NUMDIGIT(x,y) (((vlong)(log10((x))/log10((y))))+1)
#define SQ(x) ((x)*(x))
#define ABS(x) ((x)<0?-(x):(x))
#define FABS(x) ((x)+eps<0?-(x):(x))
#define ALL(x) (x).begin(),(x).end()
#define LCM(x,y) (((x)/gcd((x),(y)))*(y))
#define SZ(x) ((vlong)(x).size())
#define NORM(x) if(x>=mod)x-=mod;
using namespace std;
typedef long long vlong;
/***********Template Ends Here***********/
#define SIZE 6000000
#define RSIZE 6000000
char buf[1010];
struct node {
int child[27];
void clear () {
CLR(child,-1);
}
}tnQ[SIZE+10], tnS[RSIZE+10];
int freeIndexQ, freeIndexS;
void clearQ () {
freeIndexQ = 1;
tnQ[0].clear();
}
void clearS() {
freeIndexS = 1;
tnS[0].clear();
}
int res[SIZE+10]; ///Okay
char luffy[100];
///Inserts into the trie for query
int insertQ ( ){
int cur = 0;
int len = strlen(luffy);
FOR(i,0,len-1) {
char c = luffy[i];
if ( c == '#' ) c = 26; ///Okay
else c -= 'a';
if ( tnQ[cur].child[c] == -1 ) { ///No child
tnQ[freeIndexQ].clear(); ///Clear it first
tnQ[cur].child[c] = freeIndexQ++;
}
cur = tnQ[cur].child[c];
}
return cur;
}
char zoro[100];
///Update each end of query string in query trie
void queryQ ( ){
int cur = 0;
int len = strlen ( zoro );
FOR(i,0,len-1) {
char c = zoro[i];
if ( c == '#' ) c = 26;
else c -= 'a';
if ( tnQ[cur].child[c] == -1 ) {
return;
}
cur = tnQ[cur].child[c];
}
res[cur]++;
}
///Create all possible query strings from unique substring
void queryAll ( int st, int en ) {
int len = en - st;
int l = MIN(len,10);
int zind = 0;
FOR(i,0,l-1){
zoro[zind++] = buf[st+i]; ///First put the prefix
int nzind = zind; ///Now start a new index of suffix
zoro[nzind++] = '#'; ///First of # to separate them
FOR(j,0,l-1){
zoro[nzind++] = buf[en-1-j];
zoro[nzind] = 0; ///A possible query is finished
queryQ();
}
}
}
///Insert suffix into second trie for main string
void insertS ( int st, int len ){
int cur = 0;
FOR(i,0,len-1) {
char c = buf[st+i];
c -= 'a';
bool newNode = false;
if ( tnS[cur].child[c] == -1 ) {
tnS[freeIndexS].clear();
tnS[cur].child[c] = freeIndexS++;
newNode = true;
}
cur = tnS[cur].child[c];
if ( newNode ) { ///New node found
queryAll ( st, st+i+1 );
}
}
}
char query[50010][25];
int main () {
int kase, cnt = 0;
scanf ( "%d", &kase );
while ( kase-- ) {
CLR(res,0); ///clear res to 0
clearQ(); ///Clear trie for query
clearS(); ///Clear trie for String
scanf ( "%s", buf ); ///Take main string in buf
int q;
scanf ( "%d", &q ); ///Number of query
FOR(i,0,q-1) {
scanf ( "%s", luffy ); ///Take first string of in luffy
int luf = 0;
luf = strlen ( luffy );
luffy[luf] = '#'; ///Attach a # at end
luf++;
scanf ( "%s", luffy + luf ); ///take input from end of luffy
reverse ( luffy + luf, luffy + strlen ( luffy ) ); ///reverse as needed.
insertQ ();
strcpy ( &query[i][0], luffy ); ///Keep a copy of query to process later
}
int len = strlen ( buf );
FOR(i,0,len-1) {
insertS( i, len - i ); ///Start and length
}
printf ( "Case %d:\n", ++cnt );
FOR(i,0,q-1){
strcpy ( luffy, &query[i][0] );
int t = insertQ ( );
printf ( "%d\n", res[t] );
}
}
return 0;
}
LyoqKioqKioqKioqVGVtcGxhdGUgU3RhcnRzIEhlc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m9bbnppbmQrK10gPSAnIyc7IC8vL0ZpcnN0IG9mICMgdG8gc2VwYXJhdGUgdGhlbQogICAgICAgIEZPUihqLDAsbC0xKXsKICAgICAgICAgICAgem9yb1tuemluZCsrXSA9IGJ1Zltlbi0xLWpdOwogICAgICAgICAgICB6b3JvW256aW5kXSA9IDA7IC8vL0EgcG9zc2libGUgcXVlcnkgaXMgZmluaXNoZWQKICAgICAgICAgICAgcXVlcnlRKCk7CiAgICAgICAgfQogICAgfQp9CgovLy9JbnNlcnQgc3VmZml4IGludG8gc2Vjb25kIHRyaWUgZm9yIG1haW4gc3RyaW5nCnZvaWQgaW5zZXJ0UyAoIGludCBzdCwgaW50IGxlbiApewogICAgaW50IGN1ciA9IDA7CgogICAgRk9SKGksMCxsZW4tMSkgewogICAgICAgIGNoYXIgYyA9IGJ1ZltzdCtpXTsKICAgICAgICBjIC09ICdhJzsKCiAgICAgICAgYm9vbCBuZXdOb2RlID0gZmFsc2U7CiAgICAgICAgaWYgKCB0blNbY3VyXS5jaGlsZFtjXSA9PSAtMSApIHsKICAgICAgICAgICAgdG5TW2ZyZWVJbmRleFNdLmNsZWFyKCk7CiAgICAgICAgICAgIHRuU1tjdXJdLmNoaWxkW2NdID0gZnJlZUluZGV4UysrOwogICAgICAgICAgICBuZXdOb2RlID0gdHJ1ZTsKICAgICAgICB9CgogICAgICAgIGN1ciA9IHRuU1tjdXJdLmNoaWxkW2NdOwoKICAgICAgICBpZiAoIG5ld05vZGUgKSB7IC8vL05ldyBub2RlIGZvdW5kCiAgICAgICAgICAgIHF1ZXJ5QWxsICggc3QsIHN0K2krMSApOwogICAgICAgIH0KICAgIH0KfQoKY2hhciBxdWVyeVs1MDAxMF1bMjVdOwoKaW50IG1haW4gKCkgewoJCiAgICBpbnQga2FzZSwgY250ID0gMDsKICAgIHNjYW5mICggIiVkIiwgJmthc2UgKTsKCiAgICB3aGlsZSAoIGthc2UtLSApIHsKICAgICAgICBDTFIocmVzLDApOyAvLy9jbGVhciByZ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