#include<iostream>#include<algorithm>#include<cmath>#include<cstring>#include<vector>#include<unordered_map>#include<unordered_set>#include<set>#include<map>#defineendl'\n'#defineIOSios::sync_with_stdio(false); cin.tie(0); cout.tie(0)usingnamespace std;constdouble pi =acos(-1);typedeflonglong ll;typedefunsignedlonglong ull;constint N =1e5+10, P =131;ull h[N], p[N];
ull a[N];// 記錄每個(gè)詢問串的哈希值char str[N];ull get(int l,int r){return h[r]- h[l -1]* p[r - l +1];}intmain(){int T;scanf("%d",&T);while(T --){scanf("%s", str +1);int len =strlen(str +1);p[0]=1;for(int i =1; i <= len; i ++){p[i]= p[i -1]* P;h[i]= h[i -1]* P + str[i];}int n;scanf("%d",&n);map<ull,int> ma;// 詢問串哈希值在母串中出現(xiàn)的不相交的次數(shù)map<ull,int> last;// 詢問串哈希值在母串中上次出現(xiàn)的位置的末端for(int i =1; i <= n; i ++){char buf[35];scanf("%s", buf +1);int size =strlen(buf +1);// 處理詢問串哈希值ull x =0;for(int j =1; j <= size; j ++)x = x * P + buf[j];a[i]= x;ma[a[i]]=0;last[a[i]]=0;}// 暴力枚舉母串中長度小于等于30的串的哈希值for(int i =1; i <= len; i ++){for(int j =0; j <30&& i + j <= len; j ++){ull x =get(i, i + j);// []包含了插入,count不插入,count查詢是lognif(ma.count(x)&& last[x]< i){ma[x]++;last[x]= i + j;}}}for(int i =1; i <= n; i ++)printf("%d\n", ma[a[i]]);}return0;}