#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct Index
{
	static bool cmp(const char* a, const char* b)
	{
		return strncmp(a, b, 1000) < 0;
	}

	Index( const char* p, int N )
	{
		for(int i=0; i!=N; ++i)
			sary.push_back(p+i);
		sort( sary.begin(), sary.end(), &cmp );
	}

	int count( string& q )
	{
		vector<const char*>::iterator beg =
			lower_bound( sary.begin(), sary.end(), q.c_str(), &cmp );
		q[q.size()-1]++;
		vector<const char*>::iterator end =
			lower_bound( beg, sary.end(), q.c_str(), &cmp );
		return end-beg;
	}

	vector<const char*> sary;
};

int main()
{
	int N=0;
	for(string str; getline(cin,str);)
	{
		if(N++) cout << endl;
		cout << "Case " << N << ":" << endl;

		// reference text
		string text;
		while( getline(cin,str), str!="%%%%%" )
			text+=str, text+=" ";

		// indexing
		Index idx(text.c_str(), text.size());

		// query
		while( getline(cin,str), str!="%%%%%" )
			cout << idx.count(str) << endl;

		getline(cin,str);
	}
}