// eddy1021
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long ll;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<LD,LD> Pt;
typedef tuple<int,int,int> tiii;
typedef tuple<LL,LL,LL> tlll;
#define mod9 1000000009ll
#define mod7 1000000007ll
#define INF  1023456789ll
#define INF16 10000000000000000ll
#define FI first
#define SE second
#define X FI
#define Y SE
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else 
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
int __ = 1 , _cs;
/*********default*********/
#define N 400040
#define K 100010
struct Nd{
  int nxt[ 26 ] , id , sum[ 26 ];
  Nd(){
    id = 0;
    for( int i = 0 ; i < 26 ; i ++ )
      sum[ i ] = nxt[ i ] = 0;
  }
} nd[ N ];
void build(){

}
int n , nid , root , nxtid = 1 , len;
int st[ K ] , sl[ K ];
char c[ N ];
char cc[ N + K + K ];
void go( int now , int pos ){
  if( pos == len ){
    nd[ now ].id = nid;
    return;
  }
  if( !nd[ now ].nxt[ c[ pos ] - 'a' ] )
    nd[ now ].nxt[ c[ pos ] - 'a' ] = ++ nxtid;
  nd[ now ].sum[ c[ pos ] - 'a' ] ++;
  go( nd[ now ].nxt[ c[ pos ] - 'a' ] , pos + 1 );
}
int cnt[ K ][ 26 ][ 26 ] , bs[ K ];
void gogo( int now , int kth , int pos ){
  if( nd[ now ].id ) bs[ kth ] ++;
  if( nd[ now ].id == kth ) return;
  int chr = cc[ st[ kth ] + pos ] - 'a';
  for( int i = 0 ; i < 26 ; i ++ )
    if( i != chr )
      cnt[ kth ][ chr ][ i ] += nd[ now ].sum[ i ];
  gogo( nd[ now ].nxt[ chr ] , kth , pos + 1 );
}
void init(){
  scanf( "%d" , &n );
  root = 1;
  int nst = 0;
  for( int i = 1 ; i <= n ; i ++ ){
    nid = i;
    scanf( "%s" , c );
    len = strlen( c );
    go( root , 0 );
    sl[ i ] = len;
    st[ i ] = nst;
    for( int j = 0 ; j < len ; j ++ )
      cc[ nst + j ] = c[ j ];
    nst += len + 2;
  }
  for( int i = 1 ; i <= n ; i ++ )
    gogo( root , i , 0 );
}
int q , k , p[ 30 ];
char per[ 30 ];
void solve(){
  scanf( "%d" , &q ); while( q -- ){
    scanf( "%d%s" , &k , per );
    for( int i = 0 ; i < 26 ; i ++ )
      p[ i ] = per[ i ] - 'a';
    int ans = bs[ k ];
    for( int i = 0 ; i < 26 ; i ++ )
      for( int j = 0 ; j < i ; j ++ )
        ans += cnt[ k ][ p[ i ] ][ p[ j ] ];
    printf( "%d\n" , ans );
  }
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


Measured memory_bytes: 349568