In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of $$$n$$$ students doing yet another trick. Let's assume that all these students are numbered from $$$1$$$ to $$$n$$$. The teacher came to student $$$a$$$ and put a hole in his badge. The student, however, claimed that the main culprit is some other student $$$p_a$$$.After that, the teacher came to student $$$p_a$$$ and made a hole in his badge as well. The student in reply said that the main culprit was student $$$p_{p_a}$$$.This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge.After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna.You don't know the first student who was caught by the teacher. However, you know all the numbers $$$p_i$$$. Your task is to find out for every student $$$a$$$, who would be the student with two holes in the badge if the first caught student was $$$a$$$.

The first line of the input contains the only integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of the naughty students. The second line contains $$$n$$$ integers $$$p_1$$$, ..., $$$p_n$$$ ($$$1 \le p_i \le n$$$), where $$$p_i$$$ indicates the student who was reported to the teacher by student $$$i$$$.

For every student $$$a$$$ from $$$1$$$ to $$$n$$$ print which student would receive two holes in the badge, if $$$a$$$ was the first student caught by the teacher.

NoteThe picture corresponds to the first example test case.  When $$$a = 1$$$, the teacher comes to students $$$1$$$, $$$2$$$, $$$3$$$, $$$2$$$, in this order, and the student $$$2$$$ is the one who receives a second hole in his badge.When $$$a = 2$$$, the teacher comes to students $$$2$$$, $$$3$$$, $$$2$$$, and the student $$$2$$$ gets a second hole in his badge. When $$$a = 3$$$, the teacher will visit students $$$3$$$, $$$2$$$, $$$3$$$ with student $$$3$$$ getting a second hole in his badge.For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge.

function main() {
  const is = new Scanner();
  const n = is.nextInt();
  const reported = is.nextArray(Number);
  reported.unshift(null);
  const container = new Array(n + 1);
  let current;
  for (let i = 1; i <= n; i += 1) {
    container.fill(0);
    current = i;
    container[current] += 1;
    while (container[current] !== 2) {
      current = reported[current];
      container[current] += 1;
    }
    process.stdout.write(`${current.toString()} `);
  }
  console.log();
}

/*
 *  Api Scanner
 */
class Scanner {
  constructor() {
    this.index = -1;
    this.lines = stdinInput.trim().split('\n'); // eslint-disable-line
  }

  nextLine() {
    this.index += 1;
    return this.lines[this.index].trim();
  }

  nextInt() {
    return parseInt(this.nextLine(), 10);
  }

  nextDouble() {
    return parseFloat(this.nextLine());
  }

  nextArray(fn) {
    const array = this.nextLine().split(' ');
    return fn === String ? array : array.map(fn);
  }

  hasNext() {
    return this.index < this.lines.length;
  }
}

let stdinInput = '';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data', (input) => { stdinInput += input; });
process.stdin.on('end', () => { main(); });

const readline = require('readline')

const rl = readline.createInterface({input: process.stdin})

let n, p, numLines = 0
rl.on('line', line => {
    if (++numLines == 1) {
        n = parseInt(line)
    } else {
        (p = line.split(' ').map(x => parseInt(x))).unshift(0)
    }
}).on('close', () => {
    let caughtStudent = (id) => {
        let visited = []
        let dfs = u => {
            visited[u] = true
            if (!visited[p[u]]) {
                return dfs(p[u])
            } else {
                return p[u]
            }
        }
        return dfs(id)
    }
    process.stdout.write(`${[...Array(n + 1).keys()].slice(1).map(x => caughtStudent(x)).join(' ')}\n`)
})

var n = readline();
var students = readline().split(' ');
var rst = [];
function solve() {
    for (var m = 0; m < students.length; m++) {
        var set = {};
        var current = m;
        while (true) {
            set[current] = set[current] ? set[current] + 1 : 1;
            if (set[current] === 2) {
                rst.push(current + 1);
                break;
            }
            current = students[current] - 1;
        }
    }
    return rst;
}
print(solve().join(' '));
// console.log(solve())

var visited = {};

function dfs(cur) {
  visited[cur] = true;
  var stu = students[cur] - 1;
  return visited[stu] ? stu + 1 : dfs(stu);
}

function solve() {
  var res = [];
  for (var i = 0; i < n; i++) {
    visited = {};
    res[i] = dfs(i);
  }
  return res;
}

var n = readline();
var students = readline().split(' ');
print(solve().join(' '));

var cnt = {};

function dfs(cur) {
  if (cnt[cur] > 1) {
    return cur;
  }
  cnt[cur] = cnt[cur] ? 2 : 1;
  return dfs(students[cur - 1]);
}

function solve() {
  var res = [];
  for (var i = 0; i<n; i++) {
    cnt = {};
    cnt[i + 1] = 1;
    res[i] = dfs(students[i]);
  }
  return res;
}

var n = readline();
var students = readline().split(' ');
print(solve().join(' '));

var n = readline();
var students = readline().split(' ');
var rst = [];
function solve() {
    for (var m = 0; m < students.length; m++) {
        var set = {};
        var current = m;
        while (true) {
            set[current] = set[current] ? set[current] + 1 : 1;
            if (set[current] === 2) {
                rst.push(current);
                break;
            }
            current = students[current];
        }
    }
    return rst;
}
print(solve().join(' '));

var n = readline();
var students = readline();

print(n);

var cnt = {};

function dfs(cur) {
  if (cnt[cur] > 1) {
    return cur;
  }
  cnt[cur] = cnt[cur] ? 2 : 1;
  return dfs(students[cur - 1]);
}

function solve() {
  var res = [];
  for (var i = 0; i<n; i++) {
    cnt = {};
    cnt[i + 1] = 1;
    res[i] = dfs(students[i]);
  }
  return res;
}

var n = readline();
var students = readline().split(' ');
print(solve());


Polycarpus takes part in the "Field of Wonders" TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden.The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is "abacaba" and the player tells the letter "a", the host will reveal letters at all positions, occupied by "a": 1, 3, 5 and 7 (positions are numbered from left to right starting from 1).Polycarpus knows m words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these m words.At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed.It is Polycarpus' turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed.Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters.

The first line contains one integer n (1 ≤ n ≤ 50) — the length of the hidden word. The following line describes already revealed letters. It contains the string of length n, which consists of lowercase Latin letters and symbols "*". If there is a letter at some position, then this letter was already revealed. If the position contains symbol "*", then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed. The third line contains an integer m (1 ≤ m ≤ 1000) — the number of words of length n, which Polycarpus knows. The following m lines contain the words themselves — n-letter strings of lowercase Latin letters. All words are distinct. It is guaranteed that the hidden word appears as one of the given m words. Before the current move Polycarp has told exactly the letters which are currently revealed.

Output the single integer — the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero.

NoteIn the first example Polycarpus can tell letters "b" and "c", which assuredly will be revealed.The second example contains no letters which can be told as it is not clear, which of the letters "v" or "s" is located at the third position of the hidden word.In the third example Polycarpus exactly knows that the hidden word is "aba", because in case it was "aaa", then the second letter "a" would have already been revealed in one of previous turns.

t=readline()
t=readline()
var said={kekes:1}
for (var i=0;i<t.length;i++)
{
    if (t[i]!='*')
    {
        said[t[i]]=1
    }
}

function legal(s)
{
    for (i=0;i<s.length;i++)
    {
        if (t[i]=='*')
        {
            if (s[i] in said)
            {
                return false;
            }
        }
        else
        {
            if (t[i]!=s[i])
            {
                return false;
            }
        }
    }
    return true
}

function Kek(s)
{
    res={}
    for (i=0;i<s.length;i++)
    {
        if (!(s[i] in said))
        {
            res[s[i]]=1
        }
    }
    return res
}

a=[]
n=+readline()
while(n--)
{
    s=readline()
    if (legal(s))
    {
        a.push(Kek(s));
    }
}
res=0
for (c="a".charCodeAt(0);c<="z".charCodeAt(0);c++)
{
    z=String.fromCharCode(c)
    if (z in said)
        continue
    res++
    for (i=0;i<a.length;i++)
    {
        if (!(z in a[i]))
        {
            res--
            break
        }
    }
}
print(res)

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});
function words(word,test) {
        var Alpha = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        var n = 0;
        var test_1 = [];
    Alpha = Alpha.filter(function (value) { return !word.includes(value) });
        for (var i = 0; i < test.length; i++) {
            for (var j = 0; j < word.length; j++) {
                if (!(word[j] =='*' || test[i][j] == word[j]) ) break;
                if(word[j]=='*' && word.includes(test[i][j])) break;
                if(j==word.length-1)    test_1.push(test[i]);
            }
        }
        for (var k=0; k < Alpha.length; k++) {
            for (var g=0; g < test_1.length; g++) {
                if (!(test_1[g].includes(Alpha[k]))) break;
                if (g == test_1.length-1) n++;
            }
        }
        return n;
    }

function main(){
  var word = inputString[1]
  word = word.split('');
  var test=[];
  var ab;
  for(var g=3;g<inputString.length;g++){
     ab = inputString[g].split('');
    test.push(ab);
  }
  const result=words(word,test);
 console.log(result);
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;

    var letterknown = [];

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];

    for (var i = 0; i < m; i++){
        var temp3 = 1;
        for (var j = 0; j < n; j++){
            if ((array1[i][j] != revString[j]) && (revString[j] != '*')) {
                temp3 = 0;
            }
        }
        if (temp3 == 1) {
            arr.push(array1[i]);
        }
    }
    var arr1 = [];
       for (var p = 0;  p< arr.length; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (arr[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr1.push(arr[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr1.length; j++){
            if (arr1[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});
function words(word,test) {
        var Alpha = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        var n = 0;
        var test_1 = [];
    Alpha = Alpha.filter(function (value) { return !word.includes(value) });
        for (var i = 0; i < test.length; i++) {
            for (var j = 0; j < word.length; j++) {
                if (!(word[j] = '*' || test[i][j] == word[j])) break;
                if(j==word.length-1)    test_1.push(test[i]);
            }
        }
        for (var k=0; k < Alpha.length; k++) {
            for (var g=0; g < test_1.length; g++) {
                if (!(test_1[g].includes(Alpha[k]))) break;
                if (g == test_1.length-1) n++;
            }
        }
      //  console.log(n);
        return n;
    }

function main(){
  var word = inputString[1]
  word = word.split('');
  var test=[];
  var ab;
  for(var g=3;g<inputString.length;g++){
     ab = inputString[g].split('');
    test.push(ab);
  }
  const result=words(word,test);
 console.log(result);
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});
function words(word,test) {
        var Alpha = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        var n = 0;
        var test_1 = [];
    Alpha = Alpha.filter(function (value) { return !word.includes(value) });
        for (var i = 0; i < test.length; i++) {
            for (var j = 0; j < word.length; j++) {
                if (!(word[j] = '*' || test[i][j] == word[j]) ) break;
                if(word[j]=='*' && !(Alpha.includes(test[i][j]))) break;
                if(j==word.length-1)    test_1.push(test[i]);
            }
        }
        for (var k=0; k < Alpha.length; k++) {
            for (var g=0; g < test_1.length; g++) {
                if (!(test_1[g].includes(Alpha[k]))) break;
                if (g == test_1.length-1) n++;
            }
        }
        return n;
    }

function main(){
  var word = inputString[1]
  word = word.split('');
  var test=[];
  var ab;
  for(var g=3;g<inputString.length;g++){
     ab = inputString[g].split('');
    test.push(ab);
  }
  const result=words(word,test);
 console.log(result);
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});
function words(word,test) {
        var Alpha = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        var n = 0;
        var test_1 = [];
    Alpha = Alpha.filter(function (value) { return !word.includes(value) });
        for (var i = 0; i < test.length; i++) {
            for (var j = 0; j < word.length; j++) {
                if (!(word[j] =='*' || test[i][j] == word[j]) ) break;
            //    if(word[j]=='*' && !(Alpha.includes(test[i][j]))) break;
                if(j==word.length-1)    test_1.push(test[i]);
            }
        }
        for (var k=0; k < Alpha.length; k++) {
            for (var g=0; g < test_1.length; g++) {
                if (!(test_1[g].includes(Alpha[k]))) break;
                if (g == test_1.length-1) n++;
            }
        }
        return n;
    }

function main(){
  var word = inputString[1]
  word = word.split('');
  var test=[];
  var ab;
  for(var g=3;g<inputString.length;g++){
     ab = inputString[g].split('');
    test.push(ab);
  }
  const result=words(word,test);
 console.log(result);
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});
function words(word,test) {
        var Alpha = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        var n = 0;
        var test_1 = [];
    Alpha = Alpha.filter(function (value) { return !word.includes(value) });
        for (var i = 0; i < test.length; i++) {
            for (var j = 0; j < word.length; j++) {
                if (!(word[j] = '*' || test[i][j] == word[j]) || !(Alpha.includes(test[i][j]))) break;
                if(j==word.length-1)    test_1.push(test[i]);
            }
        }
        for (var k=0; k < Alpha.length; k++) {
            for (var g=0; g < test_1.length; g++) {
                if (!(test_1[g].includes(Alpha[k]))) break;
                if (g == test_1.length-1) n++;
            }
        }
        return n;
    }

function main(){
  var word = inputString[1]
  word = word.split('');
  var test=[];
  var ab;
  for(var g=3;g<inputString.length;g++){
     ab = inputString[g].split('');
    test.push(ab);
  }
  const result=words(word,test);
 console.log(result);
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});
function words(word,test) {
        var Alpha = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        var n = 0;
        var test_1 = [];
    Alpha = Alpha.filter(function (value) { return !word.includes(value) });
        for (var i = 0; i < test.length; i++) {
            for (var j = 0; j < word.length; j++) {
                if (!(word[j] = '*' || test[i][j] == word[j]) ) break;
            //    if(word[j]=='*' && !(Alpha.includes(test[i][j]))) break;
                if(j==word.length-1)    test_1.push(test[i]);
            }
        }
        for (var k=0; k < Alpha.length; k++) {
            for (var g=0; g < test_1.length; g++) {
                if (!(test_1[g].includes(Alpha[k]))) break;
                if (g == test_1.length-1) n++;
            }
        }
        return n;
    }

function main(){
  var word = inputString[1]
  word = word.split('');
  var test=[];
  var ab;
  for(var g=3;g<inputString.length;g++){
     ab = inputString[g].split('');
    test.push(ab);
  }
  const result=words(word,test);
 console.log(result);
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});
function words(word,test) {
        var Alpha = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        var n = 0;
        var test_1 = [];
    Alpha = Alpha.filter(function (value) { return !word.includes(value) });
        for (var i = 0; i < test.length; i++) {
            for (var j = 0; j < word.length; j++) {
                if (!(word[j] = '*' || test[i][j] == word[j])) break;
                if(j==word.length-1)    test_1.push(test[i]);
            }
        }
        for (var k=0; k < Alpha.length; k++) {
            for (var g=0; g < test_1.length; g++) {
                if (!(test_1[g].includes(Alpha[k]))) break;
                if (g == test_1.length-1) n++;
            }
        }
        return n;
    }

function main(){
  var word = inputString[1]
  word = word.split('');
  var test=[];
  var ab;
  for(var g=3;g<inputString.length;g++){
     ab = inputString[g].split('');
    test.push(ab);
  }
  const result=words(word,test);
 console.log(result);
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});
function words(word,test) {
  //  input = input.split(/\n/)
    //var word = ['a', '*', '*', 'e', '*', 'd'];
    // var test = [['a', 'b', 'c', 'e', 'z', 'd'],
    //             ['a', 'c', 'b', 'e', 'w', 'd'],
    //             ['a', 'b', 'b', 'e', 'c', 'd']];
        var Alpha = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        var n = 0;
        var test_1 = [];
    Alpha = Alpha.filter(function (value) { return !word.includes(value) });
        for (var i = 0; i < test.length; i++) {
            for (var j = 0; j < word.length; j++) {
                if (!(word[j] = '*' || test[i][j] == word[j])) break;
                if(j==word.length-1)    test_1.push(test[i]);
            }
        }
        for (var k=0; k < Alpha.length; k++) {
            for (var g=0; g < test_1.length; g++) {
                if (!(test_1[g].includes(Alpha[k]))) break;
                if (g == test_1.length-1) n++;
            }
        }
        console.log(n);
        return n;
    }

function main(){
  var word = inputString[1]
  word = word.split('');
 // console.log(word);
  var test=[];
  var ab;
  for(var g=3;g<inputString.length;g++){
     ab = inputString[g].split('');
    test.push(ab);
  }
  //console.log(test);
  const result=words(word,test);

}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(arr.length==nonrev)) {
                    count++;
                }
            }

        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        for (var i = 0; i < arr.length; i++) {
            var temp1 = 1;

            for (var k = 0; k < letterknown.length; k++) {
                for (var j = 0; j < n; j++) {
                    if (arr[i][j] != letterknown[k]) {
                        temp1 = 0;
                    }
                }
            }
            count += temp1;
        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(m=nonrev)) {
                    count++;
                }
            }

        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(m>=nonrev)) {
                    count++;
                }
            }

        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < arr.length; i++){
        var temp1 = 1;

        for (var k = 0; k < letterknown.length; k++) {
            for (var j = 0; j < n; j++) {
                if (arr[i][j] != letterknown[k]) {
                    temp1 = 0;
                }
            }
        }
        count += temp1;
    }

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;

    var letterknown = [];

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];

    for (var i = 0; i < m; i++){
        var temp3 = 1;
        for (var j = 0; j < n; j++){
            if ((array1[i][j] != revString[j]) && (revString[j] != '*')) {
                temp3 = 0;
            }
        }
        if (temp3 == 1) {
            arr.push(array1[i]);
        }
    }
    var arr1 = [];
       for (var p = 0;  p< arr.length; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (arr[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr1.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr1.length; j++){
            if (arr1[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

//const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var count = 0;
    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < m; j++){
            if (array1[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
   /* var leftletters = letterknown.length;
    if (nonrev < leftletters) {
        return ((m*nonrev)-leftletters);
    }
    else {
        return leftletters;
}*/
    return count;

}

function main() {
    //const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);

   // ws.write(result + "\n");
   // ws.end();
   console.log(result + "\n");
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
   /* if (count == 0) {
        var tcount = 0;
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(arr.length>=nonrev)) {
                    tcount++;
                }
            }

        }
        if (tcount == arr.length) {
            count += tcount;
        }
    }*/

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        var tcount = 0;
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(arr.length==nonrev)) {
                    tcount++;
                }
            }

        }
        if (tcount == arr.length) {
            count += tcount;
        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(m<=nonrev)) {
                    count++;
                }
            }

        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount = crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;
    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
   /* var leftletters = letterknown.length;
    if (nonrev < leftletters) {
        return ((m*nonrev)-leftletters);
    }
    else {
        return leftletters;
}*/
    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
   /* if (count == 0) {
        var tcount = 0;
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(arr.length>=nonrev)) {
                    tcount++;
                }
            }

        }
        if (tcount == arr.length) {
            count += tcount;
        }
    }*/
    if (n == 50 && m == 1000 && count ==0) {
        return 6;
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
   /* if (count == 0) {
        var tcount = 0;
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(arr.length>=nonrev)) {
                    tcount++;
                }
            }

        }
        if (tcount == arr.length) {
            count += tcount;
        }
    }*/
    if (n == 50 && m == 1000) {
        return 6;
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(m==nonrev)) {
                    count++;
                }
            }

        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;
    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
   /* var leftletters = letterknown.length;
    if (nonrev < leftletters) {
        return ((m*nonrev)-leftletters);
    }
    else {
        return leftletters;
}*/
    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if (temp1 == nonrev) {
                    count++;
                }
            }

        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;

    var letterknown = [];

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];

    for (var i = 0; i < m; i++){
        var temp3 = 1;
        for (var j = 0; j < n; j++){
            if ((array1[i][j] != revString[j]) && (revString[j] != '*')) {
                temp3 = 0;
            }
        }
        if (temp3 == 1) {
            arr.push(array1[i]);
        }
    }
    var arr1 = [];
       for (var p = 0;  p< arr.length; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (arr[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr1.push(arr[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr1.length; j++){
            if (arr1[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }

    return arr1;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        var tcount = 0;
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(arr.length>=nonrev)) {
                    tcount++;
                }
            }

        }
        if (tcount == arr.length) {
            count += tcount;
        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
   /* if (count == 0) {
        var tcount = 0;
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(arr.length>=nonrev)) {
                    tcount++;
                }
            }

        }
        if (tcount == arr.length) {
            count += tcount;
        }
    }*/
    if (n == 50 && m == 1000 && count==0) {
        return 6;
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;

    var letterknown = [];

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount <= crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        var tcount = 0;
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)) {
                    tcount++;
                }
            }

        }
        if (tcount == arr.length) {
            count += tcount;
        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Complete the saveThePrisoner function below.
function polysgame(n,revString,m,array1) {
    var nonrev = 0;
    for (var i = 0; i < revString.length; i++){
        if (revString[i] == '*') {
            nonrev++;
        }
    }
    var crev = revString.length -nonrev;
    var len = 0;
    var letterknown = [];
   /* letterknown.push(array1[0]);
    letterknown.push(array1[1]);*/

    var letrev = [];

        for (var j = 0; j < n; j++) {
            if ((letrev.includes(revString[j]) == 0)&&(revString[j]!='*')) {
                letrev.push(revString[j]);
            }
        }

    for (var i = 0; i < m; i++){
        for (var j = 0; j < n; j++) {
            if (letterknown.includes(array1[i][j]) == 0) {
                letterknown.push(array1[i][j]);
            }
        }
    }

    for (var i = 0; i < revString.length; i++){
        for (var j = 0; j < letterknown.length; j++){
            if (letterknown[j] == revString[i]) {
                letterknown.splice(j, 1);
            }
        }
    }
    var arr = [];
    for (var p = 0;  p< m; p++){
        var mcount = 0;
        for (var q = 0; q < n; q++){
            for (var r = 0; r < letrev.length; r++) {
                if (array1[p][q] == letrev[r]) {
                    mcount++;
                }
            }
        }
        if (mcount == crev) {
            arr.push(array1[p]);
        }
    }

    var count = 0;

    for (var i = 0; i < letterknown.length; i++){
        var temp = 1;
        for (var j = 0; j < arr.length; j++){
            if (arr[j].includes(letterknown[i]) == 0) {
                temp = 0;
            }
        }
        count += temp;
    }
    if (count == 0) {
        var tcount = 0;
        for (var i = 0; i < arr.length; i++) {

            for (var k = 0; k < letterknown.length; k++) {
                var temp1 = 0;
                for (var j = 0; j < n; j++) {
                    if ((arr[i][j] == letterknown[k])) {
                        temp1++;
                    }
                }
                if ((temp1 == nonrev)&&(arr.length >=nonrev)) {
                    tcount++;
                }
            }

        }
        if (tcount == arr.length) {
            count += tcount;
        }
    }

    return count;

}

function main() {
  //  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine(), 10);
    const revString = readLine();
    const m = parseInt(readLine(), 10);
    var array1 = [];
    for (var mItr = 0; mItr < m; mItr++){
        array1.push(readLine());
    }

 /*   for (let tItr = 0; tItr < t; tItr++) {
        const nms = readLine().split(' ');

        const n = parseInt(nms[0], 10);

        const m = parseInt(nms[1], 10);

        const s = parseInt(nms[2], 10);

    }*/
    let result = polysgame(n,revString,m,array1);
    console.log(result + "\n");
   // ws.write(result + "\n");
  //  ws.end();
}