#include<bits/stdc++.h>
#define MAX 400400
#define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1)
#define FORD(i,b,a) for (int i=(b),_a=(a);i>=_a;i=i-1)
#define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1)
#define FORE(i,v) for (__typeof((v).begin()) i=(v).begin();i!=(v).end();i++)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MASK(i) (1LL<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define div ___div
#define next ___next
#define prev ___prev
#define left ___left
#define right ___right
#define __builtin_popcount __builtin_popcountll
using namespace std;
template < class X,class Y>
void minimize( X & x,const Y & y) {
if ( x> y) x= y;
}
template < class X,class Y>
void maximize( X & x,const Y & y) {
if ( x< y) x= y;
}
template < class T>
T Abs( const T & x) {
return ( x< 0 ? - x: x) ;
}
/* Author: Van Hanh Pham - skyvn97 */
/** END OF TEMPLATE - ACTUAL SOLUTION COMES HERE **/
char input[ MAX] ;
string readString( void ) {
scanf ( "%s" , input) ;
return string( input) ;
}
struct Node {
int cnt;
bool haveString;
Node* child[ 26 ] ;
Node( ) {
cnt = 0 ;
haveString = false ;
REP( i, 26 ) child[ i] = NULL ;
}
} ;
struct Query {
int pos, answer, id;
string perm;
Query( ) {
pos = answer = id = 0 ;
}
void input( int id) {
this- > id = id;
scanf ( "%d" , & pos) ;
perm = readString( ) ;
}
} ;
/*** LOOK AT MY CODE. MY CODE IS AMAZING :D ***/
Node* root;
Query queries[ MAX] ;
vector< int > queriesAt[ MAX] ;
int numStrings, numQueries;
int numPrefix[ MAX] ;
string str[ MAX] ;
void init( void ) {
scanf ( "%d" , & numStrings) ;
FOR( i, 1 , numStrings) str[ i] = readString( ) ;
scanf ( "%d" , & numQueries) ;
FOR( i, 1 , numQueries) queries[ i] .input ( i) ;
}
int addString( const string & s) {
int res = 0 ;
Node * p = root;
FORE( it, s) {
if ( p- > child[ * it - 'a' ] == NULL ) p- > child[ * it - 'a' ] = new Node( ) ;
p = p- > child[ * it - 'a' ] ;
p- > cnt++ ;
if ( p- > haveString) res++ ;
}
p- > haveString = true ;
return res;
}
int cnt[ 30 ] [ 30 ] ;
void answerQueries( int strID) {
memset ( cnt, 0 , sizeof cnt) ;
Node * p = root;
FORE( it, str[ strID] ) {
int j = * it - 'a' ;
REP( i, 26 ) if ( i ! = j && p- > child[ i] ! = NULL ) cnt[ j] [ i] + = p- > child[ i] - > cnt;
p = p- > child[ j] ;
}
FORE( it, queriesAt[ strID] ) {
int id = * it;
queries[ id] .answer = numPrefix[ strID] + 1 ;
string & perm = queries[ id] .perm ;
REP( i, 26 ) FOR( j, i + 1 , 25 ) queries[ id] .answer + = cnt[ perm[ j] - 'a' ] [ perm[ i] - 'a' ] ;
}
}
bool CompLength( const int & a, const int & b) {
return str[ a] .size ( ) < str[ b] .size ( ) ;
}
void process( void ) {
root = new Node( ) ;
vector< int > strIDs;
FOR( i, 1 , numStrings) strIDs.push_back ( i) ;
sort( ALL( strIDs) , CompLength) ;
FORE( it, strIDs) numPrefix[ * it] = addString( str[ * it] ) ;
FOR( i, 1 , numQueries) queriesAt[ queries[ i] .pos ] .push_back ( i) ;
FOR( i, 1 , numStrings) answerQueries( i) ;
FOR( i, 1 , numQueries) printf ( "%d\n " , queries[ i] .answer ) ;
}
int main( void ) {
init( ) ;
process( ) ;
return 0 ;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBNQVggICA0MDA0MDAKI2RlZmluZSBGT1IoaSxhLGIpIGZvciAoaW50IGk9KGEpLF9iPShiKTtpPD1fYjtpPWkrMSkKI2RlZmluZSBGT1JEKGksYixhKSBmb3IgKGludCBpPShiKSxfYT0oYSk7aT49X2E7aT1pLTEpCiNkZWZpbmUgUkVQKGksbikgZm9yIChpbnQgaT0wLF9uPShuKTtpPF9uO2k9aSsxKQojZGVmaW5lIEZPUkUoaSx2KSBmb3IgKF9fdHlwZW9mKCh2KS5iZWdpbigpKSBpPSh2KS5iZWdpbigpO2khPSh2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