import java.util.Scanner;
public class Main{
static final int max=100005;
static int son[][]=new int[max][26];//son數組存儲樹節點;
static int cnt[]=new int[max]; //cnt[i]存儲如下標i爲結點的字符串的數量;
static int idx; //idx表示當前用到了那個下標,下標是0的點既是根節點又是空節點
static void insert(String s){
int p=0;
for(int i=0;i<s.length();i++){
int u=s.charAt(i)-'a';
if(son[p][u]==0) son[p][u]=++idx;
p=son[p][u];
}
cnt[p]++;
}
static int query(String s){
int p=0;
for(int i=0;i<s.length();i++){
int u=s.charAt(i)-'a';
if(son[p][u]==0) return 0;
p=son[p][u];
}
return cnt[p];
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
while(n-->0){
String s=scan.next();
if(s.equals("I")){
String str=scan.next();
insert(str);
}
else{
String str=scan.next();
System.out.println(query(str));
}
}
}
}