#include <bits/stdc++.h>
using namespace std;
size_t pos,len,n;
string str;
vector<string>var[40010];
map<string,vector<int> >fun;
map<string,string>mp;
bool ff(char ch)
{
	if (ch=='_') return true;
	if (ch>='0' && ch<='9') return true;
	if (ch>='a' && ch<='z') return true;
	if (ch>='A' && ch<='Z') return true;
	return false;
}
void print(pair< string,vector<string> > f)
{
	cout<<"===="<<f.first<<"====="<<endl;
	for (auto i:f.second)
		cout<<i<<endl;
}
pair< string,vector<string> > get()
{
	size_t lpos=str.find("(",pos),rpos=str.find(")",pos);
	string name=str.substr(pos,lpos-pos),var;
	vector<string>f;
	pos=lpos+1;
	for (size_t p=str.find(",",pos);pos<rpos;pos=p+1,p=min(str.find(",",pos),rpos))
	{
		if (p==string::npos || p>rpos) p=rpos;
		f.push_back(str.substr(pos,p-pos));
	}
	return make_pair(name,f);
}
bool check(string a,string b)
{
	if (b[0]!='_') return a==b;
	if (b.size()==1) return true;
	if (mp.count(b)) return a==mp[b];
	mp[b]=a;
	return true;
}
bool cmp(vector<string> a,vector<string> b)
{
	mp.clear();
	int n=a.size(),m=b.size();
	if (n!=m) return false;
	for (int i=0;i<n;i++)
		if (!check(a[i],b[i]))
			return false;
	return true;
}
int solve(pair< string,vector<string> >f)
{
	if (!fun.count(f.first)) return 0;
	int ans=0;
	for (auto i:fun[f.first])
		ans+=cmp(var[i],f.second);
	return ans;
}
int main()
{
	while (getline(cin,str) && str.size()!=0)
	{
		str.erase(remove(str.begin(), str.end(), ' '), str.end());
		len=str.size();
		for (pos=0;pos<len;pos++)
			if (ff(str[pos]))
			{
				auto res=get();pos--;
				if (!fun.count(res.first))
					fun[res.first]=vector<int>();
				fun[res.first].push_back(++n);
				var[n]=res.second;
			}
	}
	while (getline(cin,str) && str.size()!=0)
	{
		str.erase(remove(str.begin(), str.end(), ' '), str.end());
		len=str.size();
		for (pos=0;pos<len;pos++)
			if (ff(str[pos]))
				printf("%d\n",solve(get())),pos--;
	}
}

