import java.io.*; import java.util.*; public class word_jm { public static int nextInt(StreamTokenizer tokens) { try { int ttype = tokens.nextToken(); if (ttype != StreamTokenizer.TT_NUMBER ) throw new IOException(); return (int)(Math.round(tokens.nval)); } catch(IOException e) { System.err.println("Error reading input"); } return 0; } public static void main(String[] s) { Reader rdr = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer tokens = new StreamTokenizer(rdr); boolean i1[] = new boolean[27]; boolean i2[][] = new boolean[27][27]; boolean i3[][][] = new boolean[27][27][27]; long nowords[][][] = new long[21][27][27]; int N = nextInt(tokens); while (N-->0) { int nn = nextInt(tokens); int m = nextInt(tokens); for(int i=0; i<27; i++) { i1[i] = false; for(int j=0; j<27; j++) { i2[i][j] = false; for(int k=0; k<27; k++) i3[i][j][k] = false; } } int let[] = new int[3]; for(int i=0; i0 ? 0 : 1; if (n>0) for(int i=0;i<26;i++) if (!i1[i] && !i2[y][i] && !i3[x][y][i]) cnt += nowords[n-1][y][i]; nowords[n][x][y] = cnt; } while (m-->0) { int ttype = 0; try { ttype = tokens.nextToken(); } catch(IOException e) { System.err.println("Error reading input"); return; } if (ttype == tokens.TT_NUMBER) { int k = (int)(Math.round(tokens.nval)) - 1; int n = 1, x = 26, y = 26; String word = ""; while (nowords[n][x][y] <= k) k -= nowords[n++][x][y]; for(int j=0; j