//Submit by hsuppr - ID 1502
#pragma warning(disable:4996)

#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <bitset>
#include <utility>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

//using namespace std;

const int kMaxn = 1000010;

int n, cnt, dep;
char ss[kMaxn];
std::string s[kMaxn];
std::vector<std::string> ans[kMaxn];

class Edge {
public:
	int v;
	Edge *next;
	Edge(int V, Edge *ne) : v(V), next(ne) {}
} *g[kMaxn];

void add(int x, int y) {
	g[x] = new Edge(y, g[x]);
	g[y] = new Edge(x, g[y]);
}

char doit(int lev) {
	if (scanf("%[^,]s,", ss) <= 0) return 0;
	getchar();
	s[++cnt] = ss;
	int i, num;
	scanf("%d", &num);
	getchar();
	//printf("%s %d\n", ss, num);
	dep = std::max(dep, lev);
	ans[lev].push_back(ss);
	for (i = 0; i < num; ++i)
		doit(lev + 1);
	return 1;
}

int main() {
	static int i, j;

	cnt = 0;
	dep = 0;
	while(doit(0));
	printf("%d\n", dep + 1);
	for (i = 0; i <= dep; ++i) {
		if (!ans[i].size())
			break;
		for (j = 0; j < (int)ans[i].size(); ++j)
			printf("%s ", ans[i][j].c_str());
		puts("");
	}

	return 0;
}


//Submit by 于金鹭 - ID 1613
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <fstream>
#include <cctype>
#include <ctime>
#include <iomanip>
#include <sstream>

#define fi for (int i = 1; i <= n; ++i)
#define fj for (int j = 1; j <= m; ++j)
#define INF 0x3fffffff
#define pau system("pause")
#define printclock printf("Time used = %.2fs\n", (double) clock() / CLOCKS_PER_SEC);
#define eps 1e-5
#define cin std::cin
#define cout std::cout
#define endl std::endl
#define pair std::pair

const double pi = acos(-1.0);
const int max_n = 1e5 + 5;
typedef long long ll;
typedef pair<int, int> pii;

template<class Q> void getint(Q &res, char ch = getchar()) {
    while (!isdigit(ch) && ch != '-')    ch = getchar();
    bool flag = true;
    if ('-' == ch)  flag = false, ch = getchar();
    for (res = 0; isdigit(ch); ch = getchar())  res = res * 10 + ch - 48;
    if (!flag)  res = -res;
}
void putint(int x) {
    int i = 0, a[10];
    bool flag = true;
    if (x < 0) {
        flag = false;
        x = -x;
    } else if (x == 0) {
        a[i++] = 0;
    }
    while (x > 0) {
        a[i++] = x % 10;
        x /= 10;
    }
    if (flag == false)  putchar('-');
    for (int j = i - 1; j >= 0; --j)    putchar(a[j] + 48);
}
int mmax(int a, int b)  {return a > b ? a : b;}
int mmin(int a, int b)  {return a < b ? a : b;}
int gcd(int a, int b)   {while (b) b ^= a ^= b ^= a %= b; return a;}

/*int parent[max_n], rank[max_n];
void ini() {
    for (int i = 1; i < 10001; ++i) {
        parent[i] = i;
        rank[i] = 0;
    }
}
int find(int x) {
    return x == parent[x] ? x : parent[x] = find(parent[x]);
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (rank[x] < rank[y]) {
        parent[x] = y;
    } else {
        parent[y] = x;
        if (rank[x] == rank[y]) rank[x]++;
    }
}*/

//#define LOCAL
std::vector<std::string> vec[100005];
std::string in, t;
std::stack<int> sta;
int main() {
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif // LOCAL
    /*t.resize(1000005);*/
    //t += 'a';
    //t += '\0';
    cout << t;
    cin >> in;
    int l = in.length(), dep = 1, i = 0;

    while (i < l) {
        //int j = i;
        t.clear();
        while (i < l && in[i] != ',') {
            t += in[i];
            i++;
        }
        //t[j - i] = '\0';
        //cout << t[0] << ' ' << t[1] << endl;
        //cout << t << endl;
        vec[sta.size() + 1].push_back(t);
        //cout << j << ' ' << i << endl;
        //cout << t.length() << endl;
        //pau;
        char t2[10];
        ++i;
        int j2 = i;
        //cout << i << ' ' << in[i] << endl;
        while (i < l && in[i] != ',') {
            t2[i - j2] = in[i];
            i++;
        }
        ++i;
        t2[i - j2] = 0;
        int tnum = atoi(t2);
        //cout << t2 << ' ' << tnum << endl;
        if (tnum == 0) {
            while (sta.size()) {
                int tt = sta.top();
                tt--;
                sta.pop();
                if (tt > 0) {
                    sta.push(tt);
                    break;
                }
            }
        } else {
            sta.push(tnum);
            dep = mmax(dep, sta.size() + 1);
        }
        if (sta.empty()) {
            continue;
        }
        /*else {
            int tt = sta.top();
            tt--;
            sta.push(tt);
        }*/
    }
    cout << dep << endl;
    for (int i = 1; i <= dep; ++i) {
        for (int j = 0; j < vec[i].size() - 1; ++j)
            cout << vec[i][j] << ' ';
        cout << vec[i][vec[i].size() - 1] << endl;
    }
    return 0;
}


//Submit by 1160300318 - ID 1529
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define fi first
#define se second

typedef long long ll;
typedef double ld;
typedef pair <int, int> pii;

const int maxn = 2e5+50, maxm = 1e5+50;
const int inf = 0x3f3f3f3f, mod = (int)1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;

typedef int arrn[maxn];
typedef ll arrm[maxn];

template<class Q> void gint(Q &ret, int sig = 1, char ch = getchar()) {
	while (!isdigit(ch) && ch != '-') ch = getchar();
	if (ch == '-') sig = -1, ch = getchar();
	for (ret = 0; isdigit(ch); ch = getchar()) ret = ret * 10 + ch - '0';
	ret *= sig;
}
vector<string> v[101010];
vector<string>::iterator it;
string str;
char s[101010];
int d;

bool dfs(int md) {
	int t = scanf("%[A-Za-z]s",s);
	if (t != 1) return 0;
	int c;
	gint(c);
	d = max(d, md);
	str = s;
	v[md].push_back(str);
	for (int i = 1; i <= c; ++i)
		dfs(md+1);
	return 1;
}
int main() {
#ifdef LOCAL
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
#endif

	while (dfs(1));

	cout << d << endl;
	for (int i = 1; i <= d; ++i, puts(""))
			for (it = v[i].begin(); it != v[i].end(); ++it)
				cout << *it << " ";
}


//Submit by hxttkl - ID 1601
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define maxn 200009
vector <string> mp[maxn];
string s;
int deep=1;
int max_deep=0;
int x;
void dfs(int deep){
    if(x>=s.length())
        return;
    int y;
    max_deep=max(max_deep,deep);
    string ss;
    for(y = x ; s[y]!=','&&s[y]!='\0' ; y++);
    //printf("x %d , y %d\n",x,y);
    int key=0;
    ss=s.substr(x,y-x);
    //cout<<ss<<endl;
    y++;
    int sum=0;
    for(y ; s[y]!=','&&s[y]!='\0' ; y++){
        sum=sum*10+(s[y]-'0');
    }
    y++;
    x=y;
    mp[deep].push_back(ss);
    for(int i = 0 ; i<sum ; i++){
        dfs(deep+1);
    }
    if(deep==1)
        dfs(deep);
}
int main(){
    cin>>s;
    x=0;
    dfs(1);
    printf("%d\n",max_deep);
    for(int i = 1 ; i<=max_deep ; i++){
        for(int j = 0  ; j<mp[i].size();j++){
            cout<<mp[i][j]<<" ";
        }
        cout<<endl;
    }
}


//Submit by waj19971112 - ID 1635
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<string>
#include<vector>
using namespace std;
int L, num, i, j, k, l, r, t1, t2, p, tmp, ans, a[500001], f[500001], ll[500001], rr[500001], q[500001];
char S[1000010];
vector <int> son[500001];
int main()
{
	gets(S);
	L = strlen(S);
	S[L++] = ',';
	a[0] = INT_MAX;
	for (i = 0; i < L; i++)
	{
		num++;
		ll[num] = i;
		for (; S[i] != ','; i++);
		rr[num] = i - 1;
		for (i++; S[i] != ','; i++)
			a[num] = a[num] * 10 + S[i] - 48;
		son[p].push_back(num);
		f[num] = p;
		a[p]--;
		p = num;
		tmp++;
		ans = max(ans, tmp);
		while (a[p] == 0)
		{
			p = f[p];
			tmp--;
		}
	}
	printf("%d\n", ans);
	l = r = 1;
	while (ans--)
	{
		t1 = l;
		t2 = r;
		l = r + 1;
		for (i = t1; i <= t2; i++)
			for (j = 0; j < son[q[i]].size(); j++)
			{
				for (k = ll[son[q[i]][j]]; k <= rr[son[q[i]][j]]; k++)
					putchar(S[k]);
				putchar(' ');
				q[++r] = son[q[i]][j];
			}
		puts("");
	}
	return 0;
}


//Submit by waj19971112 - ID 1625
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<string>
#include<vector>
using namespace std;
int L, num, i, j, l, r, t, t1, t2, p, tmp, ans, a[500001], f[500001], q[500001];
char S[1000010], s[500001][101];
bool flag1, flag2;
vector <int> son[500001];
int main()
{
	freopen("G.in", "r", stdin);
	freopen("G.out", "w", stdout);
	gets(S);
	L = strlen(S);
	S[L++] = ',';
	a[0] = INT_MAX;
	for (i = 0; i < L; i++)
	{
		num++;
		t = 0;
		for (; S[i] != ','; i++)
			s[num][t++] = S[i];
		for (i++; S[i] != ','; i++)
			a[num] = a[num] * 10 + S[i] - 48;
		son[p].push_back(num);
		f[num] = p;
		a[p]--;
		p = num;
		tmp++;
		ans = max(ans, tmp);
		while (a[p] == 0)
		{
			p = f[p];
			tmp--;
		}
	}
	printf("%d\n", ans);
	l = r = 1;
	while (l <= r)
	{
		t1 = l;
		t2 = r;
		l = r + 1;
		flag1 = flag2 = false;
		for (i = t1; i <= t2; i++)
			for (j = 0; j < son[q[i]].size(); j++)
			{
				flag2 = true;
				//if (flag1)
					//putchar(' ');
				printf("%s ", s[son[q[i]][j]]);
				flag1 = true;
				q[++r] = son[q[i]][j];
			}
		if (flag2)
			puts("");
	}
	return 0;
}


//Submit by empire troop - ID 1621
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 1e6 + 15;
struct Edge{
	string str;
	int num;
} edge[maxn];
vector<string> pos[maxn];
int cnt, depth;
void init(){
	for(int i = 1; i <= depth; i++) pos[i].clear();
	cnt = 0, depth = 0;
}
int dfs(int index, int dep){
	depth = max(dep, depth);
	pos[dep].push_back(edge[index].str);
	int res = index;
	for(int i = 1; i <= edge[index].num; i++) res = dfs(res + 1, dep + 1);
	return res;
}

int main(){
	string ss, buf;
	int tem;
	while(cin >> ss){
		init();
		for(int i = 0; i < ss.size(); i++) if(ss[i]==',') ss[i] = ' ';
		stringstream line(ss);
		while(line >> buf >> tem) edge[++cnt] = Edge{buf, tem};
		int t = 0;
		while(t < cnt) t = dfs(t + 1, 1);
		cout << depth << "\n";
		for(int i = 1; i <= depth; i++){
			int j = 0;
			for(;j<pos[i].size()-1;j++) cout<<pos[i][j]<<" ";
			cout << pos[i][j] << "\n";
		}
	}
	return 0;
}


//Submit by waj19971112 - ID 1624
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<string>
#include<vector>
using namespace std;
int L, num, i, j, l, r, t, t1, t2, p, tmp, ans, a[500001], f[500001], q[500001];
char S[1000010], s[500001][101];
bool flag1, flag2;
vector <int> son[500001];
int main()
{
	freopen("G.in", "r", stdin);
	freopen("G.out", "w", stdout);
	gets(S);
	L = strlen(S);
	S[L++] = ',';
	a[0] = INT_MAX;
	for (i = 0; i < L; i++)
	{
		num++;
		t = 0;
		for (; S[i] != ','; i++)
			s[num][t++] = S[i];
		for (i++; S[i] != ','; i++)
			a[num] = a[num] * 10 + S[i] - 48;
		son[p].push_back(num);
		f[num] = p;
		a[p]--;
		p = num;
		tmp++;
		ans = max(ans, tmp);
		while (a[p] == 0)
		{
			p = f[p];
			tmp--;
		}
	}
	printf("%d\n", ans);
	q[l = r = 1] = 0;
	while (l <= r)
	{
		t1 = l;
		t2 = r;
		l = r + 1;
		flag1 = flag2 = false;
		for (i = t1; i <= t2; i++)
			for (j = 0; j < son[q[i]].size(); j++)
			{
				flag2 = true;
				if (flag1)
					putchar(' ');
				printf("%s", s[son[q[i]][j]]);
				flag1 = true;
				q[++r] = son[q[i]][j];
			}
		if (flag2)
			puts("");
	}
	return 0;
}


//Submit by hsuppr - ID 1496
#pragma warning(disable:4996)

#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <bitset>
#include <utility>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

//using namespace std;

const int kMaxn = 1000010;

int n, cnt, dep;
char ss[kMaxn];
std::string s[kMaxn];
std::vector<std::string> ans[kMaxn];

class Edge {
public:
	int v;
	Edge *next;
	Edge(int V, Edge *ne) : v(V), next(ne) {}
} *g[kMaxn];

void add(int x, int y) {
	g[x] = new Edge(y, g[x]);
	g[y] = new Edge(x, g[y]);
}

char doit(int lev) {
	if (scanf("%[^,]s,", ss) <= 0) return 0;
	getchar();
	s[++cnt] = ss;
	int i, num;
	scanf("%d", &num);
	getchar();
	//printf("%s %d\n", ss, num);
	dep = std::max(dep, lev);
	ans[lev].push_back(ss);
	for (i = 0; i < num; ++i)
		doit(lev + 1);
	return 1;
}

int main() {
	static int i, j;

	cnt = 0;
	dep = 0;
	while(doit(0));
	printf("%d\n", dep + 1);
	for (i = 0; i <= dep; ++i) {
		if (!ans[i].size())
			break;
		for (j = 0; j < (int)ans[i].size(); ++j)
			printf("%s ", ans[i][j].c_str());
		puts("");
	}

	return 0;
}


/*ebpy*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <fstream>
#include <cctype>
#include <ctime>
#include <iomanip>
#include <sstream>

#define fi for (int i = 1; i <= n; ++i)
#define fj for (int j = 1; j <= m; ++j)
#define INF 0x3fffffff
#define pau system("pause")
#define printclock printf("Time used = %.2fs\n", (double) clock() / CLOCKS_PER_SEC);
#define eps 1e-5
#define cin std::cin
#define cout std::cout
#define endl std::endl
#define pair std::pair

const double pi = acos(-1.0);
const int max_n = 1e5 + 5;
typedef long long ll;
typedef pair<int, int> pii;

template<class Q> void getint(Q &res, char ch = getchar()) {
    while (!isdigit(ch) && ch != '-')    ch = getchar();
    bool flag = true;
    if ('-' == ch)  flag = false, ch = getchar();
    for (res = 0; isdigit(ch); ch = getchar())  res = res * 10 + ch - 48;
    if (!flag)  res = -res;
}
void putint(int x) {
    int i = 0, a[10];
    bool flag = true;
    if (x < 0) {
        flag = false;
        x = -x;
    } else if (x == 0) {
        a[i++] = 0;
    }
    while (x > 0) {
        a[i++] = x % 10;
        x /= 10;
    }
    if (flag == false)  putchar('-');
    for (int j = i - 1; j >= 0; --j)    putchar(a[j] + 48);
}
int mmax(int a, int b)  {return a > b ? a : b;}
int mmin(int a, int b)  {return a < b ? a : b;}
int gcd(int a, int b)   {while (b) b ^= a ^= b ^= a %= b; return a;}

/*int parent[max_n], rank[max_n];
void ini() {
    for (int i = 1; i < 10001; ++i) {
        parent[i] = i;
        rank[i] = 0;
    }
}
int find(int x) {
    return x == parent[x] ? x : parent[x] = find(parent[x]);
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (rank[x] < rank[y]) {
        parent[x] = y;
    } else {
        parent[y] = x;
        if (rank[x] == rank[y]) rank[x]++;
    }
}*/

//#define LOCAL
std::vector<std::string> vec[1000005];
std::string in, t;
std::stack<int> sta;
int main() {
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif // LOCAL
    cin >> in;
    int l = in.length(), dep = 1, i = 0;

    while (i < l) {
        //int j = i;
        t.clear();
        while (i < l && in[i] != ',') {
            t += in[i];
            i++;
        }
        vec[sta.size() + 1].push_back(t);
        /*char t2[50];
        memset(t2, 0, sizeof(t2));*/
        char *t2 = (char *)malloc(50);
        memset(t2, 0, sizeof(t2));
        ++i;
        int j2 = i;
        //cout << i << ' ' << in[i] << endl;
        while (i < l && in[i] != ',') {
            t2[i - j2] = in[i];
            i++;
        }
        ++i;
        t2[i - j2] = 0;
        int tnum = atoi(t2);
        //cout << tnum << endl;
        //pau;
        //cout << t2 << ' ' << tnum << endl;
        if (tnum == 0) {
            while (sta.size()) {
                int tt = sta.top();
                tt--;
                sta.pop();
                if (tt > 0) {
                    sta.push(tt);
                    break;
                }
            }
        } else {
            sta.push(tnum);
            dep = mmax(dep, sta.size() + 1);
        }
        free(t2);
    }
    cout << dep << endl;
    for (int i = 1; i <= dep; ++i) {
        for (int j = 0; j < vec[i].size() - 1; ++j)
            cout << vec[i][j] << ' ';
        cout << vec[i][vec[i].size() - 1] << endl;
    }
    return 0;
}


/*بسم الله الرحمن الرحيم*/
#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926536
#define Fight_Club cin.tie(0);cout.tie(0);std::ios::sync_with_stdio(false);
typedef long long ll;
typedef long double ld;
const ll MAX=1000001;
// Dir Array !
int dx[8]= {1,0,0,-1,1,-1,1,-1};
int dy[8]= {0,1,-1,0,1,-1,-1,1};
map<string,vector<string> >m;
vector<string>v[1234567];
char ch;
string s="",ss="";
int mx=0;
int to_int(string s) {
     stringstream ss(s); int ret; ss>>ret; return ret;
}
void DFS(int orr){
    mx=max(mx,orr);
    s="";
    while(cin>>ch){
    	ss+=ch;
        if(isalpha(ch))s+=ch;
        else break;
    }
    v[orr].push_back(s);
    string intt;
    while(cin>>ch){
    	ss+=ch;
        if(isdigit(ch))intt+=ch;
        else break;
    }
    int q=to_int(intt);
    for(int i=0;i<q;i++){
        DFS(orr+1);
    }
}
int main()
{
    Fight_Club
    while(true){
    	ss="";
    	DFS(1);
    	if(ss[ss.size()-1]!=',')break;
    }
    ss="",s="";
    cout<<mx<<endl;
    for(int i=1;i<=mx;i++){
        for(int j=0;j<v[i].size();j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }

    return 0;
}
/*Code.AbdO(1)*/
/*^_^*/


/*بسم الله الرحمن الرحيم*/
#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926536
#define Fight_Club cin.tie(0);cout.tie(0);std::ios::sync_with_stdio(false);
typedef long long ll;
typedef long double ld;
const ll MAX=1000001;
// Dir Array !
int dx[8]= {1,0,0,-1,1,-1,1,-1};
int dy[8]= {0,1,-1,0,1,-1,-1,1};
map<string,vector<string> >m;
vector<string>v[1234567];
char ch;
string s="",ss="";
int mx=0;
void DFS(string str,int orr){
    mx=max(mx,orr);
    s="";
    while(cin>>ch){
    	ss+=ch;
        if(ch==',')break;
        s+=ch;
    }
    if(!str.empty())m[str].push_back(s);
    v[orr].push_back(s);
    str=s;
    cin>>ch;
    ss+=ch;
    int q=ch-'0';
    cin>>ch;
    ss+=ch;
    for(int i=0;i<q;i++){
        DFS(str,orr+1);
    }
}
int main()
{
    Fight_Club
    string strr="";
    while(true){
    	ss="";
    	DFS("",1);
    	strr+=ss;
    	//cout<<ss<<endl;
    	if(ss[ss.size()-1]!=',')break;
    }
    ss="",s="";
    cout<<mx<<endl;
    for(int i=1;i<=mx;i++){
        for(int j=0;j<v[i].size();j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
/*Code.AbdO(1)*/
/*^_^*/


/*بسم الله الرحمن الرحيم*/
#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926536
#define Fight_Club cin.tie(0);cout.tie(0);std::ios::sync_with_stdio(false);
typedef long long ll;
typedef long double ld;
const ll MAX=1000001;
// Dir Array !
int dx[8]= {1,0,0,-1,1,-1,1,-1};
int dy[8]= {0,1,-1,0,1,-1,-1,1};
map<string,vector<string> >m;
vector<string>v[1234567];
char ch;
string s="",ss="";
int mx=0;
void DFS(int orr){
    mx=max(mx,orr);
    s="";
    while(cin>>ch){
    	ss+=ch;
        if(ch==',')break;
        s+=ch;
    }
    //if(!str.empty())m[str].push_back(s);
    v[orr].push_back(s);
    cin>>ch;
    ss+=ch;
    int q=ch-'0';
    cin>>ch;
    ss+=ch;
    for(int i=0;i<q;i++){
        DFS(orr+1);
    }
}
int main()
{
    Fight_Club
    while(true){
    	ss="";
    	DFS(1);
    	if(ss[ss.size()-1]!=',')break;
    }
    ss="",s="";
    cout<<mx<<endl;
    for(int i=1;i<=mx;i++){
        for(int j=0;j<v[i].size();j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
/*Code.AbdO(1)*/
/*^_^*/


/*بسم الله الرحمن الرحيم*/
#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926536
#define Fight_Club cin.tie(0);cout.tie(0);std::ios::sync_with_stdio(false);
typedef long long ll;
typedef long double ld;
const ll MAX=1000001;
// Dir Array !
int dx[8]= {1,0,0,-1,1,-1,1,-1};
int dy[8]= {0,1,-1,0,1,-1,-1,1};
map<string,vector<string> >m;
vector<string>v[1234567];
char ch;
string s="",ss="";
int mx=0;
void DFS(string str,int orr){
    mx=max(mx,orr);
    s="";
    while(cin>>ch){
    	ss+=ch;
        if(ch==',')break;
        s+=ch;
    }
    if(!str.empty())m[str].push_back(s);
    v[orr].push_back(s);
    str=s;
    cin>>ch;
    ss+=ch;
    int q=ch-'0';
    cin>>ch;
    ss+=ch;
    for(int i=0;i<q;i++){
        DFS(str,orr+1);
    }
}
int main()
{
    Fight_Club
    string strr="";
    while(true){
    	ss="";
    	DFS("",1);
    	strr+=ss;
    	//cout<<ss<<endl;
    	if(ss[ss.size()-1]!=',')break;
    }
    ss="",s="";
    cout<<mx<<endl;
    for(int i=1;i<=mx;i++){
        for(int j=0;j<v[i].size();j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }/**/
    return 0;
}
/*Code.AbdO(1)*/
/*^_^*/


#include <bits/stdc++.h>

using namespace std;

istringstream ss;
int depth = 0;
map<int, vector<string>> m;

void f(int level)
{
    string text, childrenStr;
    getline(ss, text, ',');
    getline(ss, childrenStr, ',');
    int children = stoi(childrenStr);

    m[level].push_back(text);
    depth = max(depth, level);
    for (int i = 0; i < children; i++) f(level+1);
}

int main()
{
    ios_base::sync_with_stdio(false);

    string s;
    cin >> s;
    ss = istringstream (s);

    while (!ss.eof()) f(1);

    for (const pair<int, vector<string>> &p : m) {
        for (const string &s : p.second) {
            cout << s << " ";
        }
        cout << endl;
    }
    printf("%d\n", depth);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

istringstream ss;
int depth = 0;
map<int, vector<string>> m;

void f(int level)
{
    string text, childrenStr;
    getline(ss, text, ',');
    getline(ss, childrenStr, ',');
    int children = stoi(childrenStr);

    m[level].push_back(text);
    depth = max(depth, level);
    for (int i = 0; i < children; i++) f(level+1);
}

int main()
{
    ios_base::sync_with_stdio(false);

    string s;
    cin >> s;
    ss = istringstream (s);

    while (!ss.eof()) f(1);

    printf("%d\n", depth);
    for (const pair<int, vector<string>> &p : m) {
        for (const string &s : p.second) {
            cout << s << " ";
        }
        cout << endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

istringstream ss;
int depth = 0;
map<int, vector<string>> m;

void f(int level)
{
    string text, childrenStr;
    getline(ss, text, ',');
    getline(ss, childrenStr, ',');
    int children = stoi(childrenStr);

    m[level].push_back(text);
    depth = max(depth, level);
    for (int i = 0; i < children; i++) f(level+1);
}

int main()
{
    ios_base::sync_with_stdio(false);

    string s;
    cin >> s;
    ss = istringstream (s);

    while (!ss.eof()) f(1);

    cout << depth << endl;
    for (const pair<int, vector<string>> &p : m) {
        for (const string &s : p.second) {
            cout << s << " ";
        }
        cout << endl;
    }

    return 0;
}


//看看会不会爆int!数组会不会少了一维！
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 4e6 + 10;
int ch[maxn];
char s[maxn];
bool vis[maxn];
vector<int> ve[maxn];
int ans, n;

void dfs(int pos, int cnt, int deep){
    ans = max(ans, deep);
    for (int i = pos; i < n; i += 2){
        if (cnt == 0) break;
        //printf("pos = %d cnt = %d deep = %d\n", pos, cnt, deep);
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[deep].push_back(ch[i]);
        cnt--;
        if (s[ch[i + 1]] == '0') continue;
        dfs(i + 2, s[ch[i + 1]] - '0', deep + 1);
    }
    //haha;
}

int main(){
    scanf("%s", s);
    n = 0;
    int pre, len = strlen(s);
    for (int i = 0; i < len; i++){
        if (i == 0 || s[i - 1] == ',') {
            //printf("i = %d pre = %d\n", i, pre);
            pre = i;
        }
        if (s[i] == ',') {
            ch[n++] = pre;
            //printf("pre = %d\n", pre);
        }
    }
    ch[n++] = pre;
/*
    printf("n = %d\n", n);
    for (int i = 0; i < n; i += 2){
        printf("%d %d\n", ch[i], ch[i + 1]);
    }

    for (int i = 0; i < len; i++){
        if (i % 10 == 0) cout << endl;
        printf("%c ", s[i]);
    }
    cout << endl;
*/
    for (int i = 0; i < n; i += 2){
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[1].push_back(ch[i]);
        ans = max(1, ans);
        if (s[ch[i + 1]] == '0') continue;
        dfs(i + 2, s[ch[i + 1]] - '0', 2);
    }
    printf("%d\n", ans);
    for (int i = 1; i <= ans; i++){
        for (int j = 0; j < ve[i].size(); j++){
            int pos = ve[i][j];
            for (int k = pos; s[k] != '\0' && s[k] != ','; k++){
                printf("%c", s[k]);
            }
            printf(" ");
        }
        cout << endl;
    }
    return 0;
}


//看看会不会爆int!数组会不会少了一维！
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 4e6 + 10;
int ch[maxn];
char s[maxn];
bool vis[maxn];
vector<int> ve[maxn];
int ans, n;

void dfs(int pos, int cnt, int deep){
    //printf("%d\n", cnt);
    ans = max(ans, deep);
    for (int i = pos; i < n; i += 2){
        if (cnt == 0) break;
        //printf("pos = %d cnt = %d deep = %d\n", pos, cnt, deep);
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[deep].push_back(ch[i]);
        cnt--;
        if (ch[i + 1] == 0) continue;
        dfs(i + 2, ch[i + 1], deep + 1);
    }
}
/*
a,10,b,0,b,0,b,0,b,0,b,0,b,0,b,0,b,0,b,0,b,0
*/
int main(){
    scanf("%s", s);
    n = 0;
    int pre = 0, len = strlen(s);
    for (int i = 0; i < len; i++){
        if (s[i] == ',') {
            ch[n++] = pre;
            if (i > 0) pre = i + 1;
        }
    }
    ch[n++] = pre;

    //printf("n = %d\n", n);
    for (int i = 1; i < n; i += 2){
        int lb = ch[i], rb = (i == n - 1 ? len - 1 : ch[i + 1] - 2);
        int tmp = 0;
        for (int j = lb; j <= rb; j++){
            tmp *= 10;
            tmp += s[j] - '0';
        }
        ch[i] = tmp;
        //printf("tmp = %d lb = %d rb = %d\n", tmp, lb, rb);
    }

    for (int i = 0; i < n; i += 2){
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[1].push_back(ch[i]);
        ans = max(1, ans);
        if (ch[i + 1] == 0) continue;
        dfs(i + 2, ch[i + 1], 2);
    }
    printf("%d\n", ans);
    for (int i = 1; i <= ans; i++){
        for (int j = 0; j < ve[i].size(); j++){
            int pos = ve[i][j];
            for (int k = pos; s[k] != '\0' && s[k] != ','; k++){
                printf("%c", s[k]);
            }
            printf(" ");
        }
        cout << endl;
    }
    return 0;
}


//看看会不会爆int!数组会不会少了一维！
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 4e6 + 10;
int ch[maxn];
char s[maxn];
bool vis[maxn];
vector<int> ve[maxn];
int ans, n;

void dfs(int pos, int cnt, int deep){
    printf("%d\n", cnt);
    ans = max(ans, deep);
    for (int i = pos; i < n; i += 2){
        if (cnt == 0) break;
        //printf("pos = %d cnt = %d deep = %d\n", pos, cnt, deep);
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[deep].push_back(ch[i]);
        cnt--;
        if (ch[i + 1] == 0) continue;
        dfs(i + 2, ch[i + 1], deep + 1);
    }
}
/*
a,10,b,0,b,0,b,0,b,0,b,0,b,0,b,0,b,0,b,0,b,0
*/
int main(){
    scanf("%s", s);
    n = 0;
    int pre = 0, len = strlen(s);
    for (int i = 0; i < len; i++){
        if (s[i] == ',') {
            ch[n++] = pre;
            if (i > 0) pre = i + 1;
        }
    }
    ch[n++] = pre;

    //printf("n = %d\n", n);
    for (int i = 1; i < n; i += 2){
        int lb = ch[i], rb = (i == n - 1 ? len - 1 : ch[i + 1] - 2);
        int tmp = 0;
        for (int j = lb; j <= rb; j++){
            tmp *= 10;
            tmp += s[j] - '0';
        }
        ch[i] = tmp;
        //printf("tmp = %d lb = %d rb = %d\n", tmp, lb, rb);
    }

    for (int i = 0; i < n; i += 2){
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[1].push_back(ch[i]);
        ans = max(1, ans);
        if (ch[i + 1] == 0) continue;
        dfs(i + 2, ch[i + 1], 2);
    }
    printf("%d\n", ans);
    for (int i = 1; i <= ans; i++){
        for (int j = 0; j < ve[i].size(); j++){
            int pos = ve[i][j];
            for (int k = pos; s[k] != '\0' && s[k] != ','; k++){
                printf("%c", s[k]);
            }
            printf(" ");
        }
        cout << endl;
    }
    return 0;
}


//看看会不会爆int!数组会不会少了一维！
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 4e6 + 10;
int ch[maxn];
char s[maxn];
bool vis[maxn];
vector<int> ve[maxn];
int ans, n;

void dfs(int pos, int cnt, int deep){
    ans = max(ans, deep);
    for (int i = pos; i < n; i += 2){
        if (cnt == 0) break;
        //printf("pos = %d cnt = %d deep = %d\n", pos, cnt, deep);
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[deep].push_back(ch[i]);
        cnt--;
        if (ch[i + 1] == 0) continue;
        dfs(i + 2, ch[i + 1], deep + 1);
    }
}

int main(){
    scanf("%s", s);
    n = 0;
    int pre = 0, len = strlen(s);
    for (int i = 0; i < len; i++){
        if (s[i] == ',') {
            ch[n++] = pre;
            if (i > 0) pre = i + 1;
        }
    }
    ch[n++] = pre;

    //printf("n = %d\n", n);
    for (int i = 1; i < n; i += 2){
        int lb = ch[i], rb = (i == n - 1 ? len - 1 : ch[i + 1] - 2);
        int tmp = 0;
        for (int j = rb; j >= lb; j--){
            tmp *= 10;
            tmp += s[j] - '0';
        }
        ch[i] = tmp;
        ///printf("tmp = %d lb = %d rb = %d\n", tmp, lb, rb);
    }

    for (int i = 0; i < n; i += 2){
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[1].push_back(ch[i]);
        ans = max(1, ans);
        if (ch[i + 1] == 0) continue;
        dfs(i + 2, ch[i + 1], 2);
    }
    printf("%d\n", ans);
    for (int i = 1; i <= ans; i++){
        for (int j = 0; j < ve[i].size(); j++){
            int pos = ve[i][j];
            for (int k = pos; s[k] != '\0' && s[k] != ','; k++){
                printf("%c", s[k]);
            }
            printf(" ");
        }
        cout << endl;
    }
    return 0;
}


//看看会不会爆int!数组会不会少了一维！
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 4e6 + 10;
int ch[maxn];
char s[maxn];
bool vis[maxn];
vector<int> ve[maxn];
int ans, n;

void dfs(int pos, int cnt, int deep){
    ans = max(ans, deep);
    for (int i = pos; i < n; i += 2){
        if (cnt == 0) break;
        //printf("pos = %d cnt = %d deep = %d\n", pos, cnt, deep);
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[deep].push_back(ch[i]);
        cnt--;
        if (ch[i + 1] == 0) continue;
        dfs(i + 2, ch[i + 1], deep + 1);
    }
}

int main(){
    scanf("%s", s);
    n = 0;
    int pre = 0, len = strlen(s);
    for (int i = 0; i < len; i++){
        if (s[i] == ',') {
            ch[n++] = pre;
            if (i > 0) pre = i + 1;
        }
    }
    ch[n++] = pre;

    printf("n = %d\n", n);
    for (int i = 1; i < n; i += 2){
        int lb = ch[i], rb = (i == n - 1 ? len - 1 : ch[i + 1] - 2);
        int tmp = 0;
        for (int j = rb; j >= lb; j--){
            tmp *= 10;
            tmp += s[j] - '0';
        }
        ch[i] = tmp;
        ///printf("tmp = %d lb = %d rb = %d\n", tmp, lb, rb);
    }

    for (int i = 0; i < n; i += 2){
        if (vis[i]) continue;
        vis[i] = vis[i + 1] = true;
        ve[1].push_back(ch[i]);
        ans = max(1, ans);
        if (ch[i + 1] == 0) continue;
        dfs(i + 2, ch[i + 1], 2);
    }
    printf("%d\n", ans);
    for (int i = 1; i <= ans; i++){
        for (int j = 0; j < ve[i].size(); j++){
            int pos = ve[i][j];
            for (int k = pos; s[k] != '\0' && s[k] != ','; k++){
                printf("%c", s[k]);
            }
            printf(" ");
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define repn(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define pern(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define PI 3.141592654
#define sz(x) int((x).size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;

using namespace std;

const int maxlv = 1000010;

vector<string> v[maxlv];
int maxl;
string s;
int cur;

void solve(int lvl){
	maxl = max(maxl, lvl);
	string tmp;
	while (isalpha(s[cur])) tmp = tmp + s[cur++];
	v[lvl].pb(tmp);
	cur++;
	string num;
	int nn;
	while (isdigit(s[cur])) num = num + s[cur++];
	istringstream(num) >> nn;
	cur++;
	repn(j,1,nn) solve(lvl + 1);
}

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.out","w",stdout);

	cin >> s;
	while (cur < sz(s))
		solve(1);
	cout << maxl << endl;
	repn(i,1,maxl){
		rep(j,0,sz(v[i])) cout << v[i][j] << " ";
		cout << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define repn(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define pern(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define PI 3.141592654
#define sz(x) int((x).size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;

using namespace std;

const int maxlv = 1000010;

vector<string> v[maxlv];
int maxl;
string s;
int cur;

void solve(int lvl){
	maxl = max(maxl, lvl);
	int start = cur;
	while (isalpha(s[start])) start++;
	v[lvl].pb(s.substr(cur, start - cur));
	cur = start + 1;
	string num;
	int nn;
	while (isdigit(s[cur])) num = num + s[cur++];
	istringstream(num) >> nn;
	cur++;
	repn(j,1,nn) solve(lvl + 1);
}

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.out","w",stdout);

	cin >> s;
	while (cur < sz(s))
		solve(1);
	cout << maxl << endl;
	repn(i,1,maxl){
		rep(j,0,sz(v[i])) cout << v[i][j] << " ";
		cout << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define repn(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define pern(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define PI 3.141592654
#define sz(x) int((x).size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;

using namespace std;

const int maxlv = 1000010;

vector<string> v[maxlv];
int maxl;
string s;
int cur;
string tmp;

void solve(int lvl){
	tmp = "";
	maxl = max(maxl, lvl);
	while (isalpha(s[cur])) tmp = tmp + s[cur++];
	v[lvl].pb(tmp);
	cur++;
	string num;
	int nn;
	while (isdigit(s[cur])) num = num + s[cur++];
	istringstream(num) >> nn;
	cur++;
	repn(j,1,nn) solve(lvl + 1);
}

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.out","w",stdout);

	cin >> s;
	while (cur < sz(s))
		solve(1);
	cout << maxl << endl;
	repn(i,1,maxl){
		rep(j,0,sz(v[i])) cout << v[i][j] << " ";
		cout << endl;
	}

	return 0;
}


/*input
a,1,a,1,a,1
*/
#include <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define F(i,a,b) for(int i = a; i <= b; i++)
#define RF(i,a,b) for(int i = a; i >= b; i--)
#define pii pair<int,int>
#define PI 3.14159265358979323846264338327950288
#define ll long long
#define ff first
#define ss second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define debug(x) cout << #x << " = " << x << endl
#define INF 1000000009
#define mod 1000000007
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define MX 2000005
vector < string > ans[MX];
vector < pair<string,ll> > v;
ll n;
ll pos;
ll maxlevel;
void f(ll lev,ll cnt)
{
	if(pos == n)
		return;
	maxlevel = max(maxlevel,lev);
	ans[lev].push_back(v[pos].ff);
	pos++;
	F(i,1,cnt)
	{
		f(lev+1,v[pos].ss);
	}
}
int main()
{
	std::ios::sync_with_stdio(false);
	string s;
	cin>>s;
	n = s.length();
	string temp = "";
	F(i,0,n-1)
	{
		if(s[i] == ',')
		{
			i++;
			int num = 0;
			while(i<n && s[i]>='0' && s[i]<='9')
			{
				int childcnt = (int)(s[i] - 48);
				num = num * 10 + childcnt;
				i++;
			}
			v.push_back({temp,num});
			//cout<<temp<<" "<<childcnt<<endl;
			temp = "";
		}
		else
			temp += s[i];
	}
	n = v.size();
	F(i,0,n-1)
	{
		if(pos < n)
		{
			f(0,v[pos].ss);
		}
	}
	//debug(maxlevel);
	cout<<maxlevel+1<<endl;
	F(i,0,maxlevel)
	{
		ll sz = ans[i].size();
		F(j,0,sz-1)
			cout<<ans[i][j]<<" ";
		cout<<endl;
	}
	return 0;
}


/*input
hello,2,ok,0,bye,1,test,1,one,1,two,2,a,0,b,0
*/
#include <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define F(i,a,b) for(int i = a; i <= b; i++)
#define RF(i,a,b) for(int i = a; i >= b; i--)
#define pii pair<int,int>
#define PI 3.14159265358979323846264338327950288
#define ll long long
#define ff first
#define ss second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define debug(x) cout << #x << " = " << x << endl
#define INF 1000000009
#define mod 1000000007
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define MX 200005
vector < string > ans[MX];
vector < pair<string,ll> > v;
ll n;
ll pos;
ll maxlevel;
void f(ll lev,ll cnt)
{
	if(pos == n)
		return;
	maxlevel = max(maxlevel,lev);
	ans[lev].push_back(v[pos].ff);
	pos++;
	F(i,1,cnt)
	{
		f(lev+1,v[pos].ss);
	}
}
int main()
{
	std::ios::sync_with_stdio(false);
	string s;
	cin>>s;
	n = s.length();
	string temp = "";
	F(i,0,n-1)
	{
		if(s[i] == ',')
		{
			i++;
			int num = 0;
			while(i<n && s[i]>='0' && s[i]<='9')
			{
				int childcnt = (int)(s[i] - 48);
				num = num * 10 + childcnt;
				i++;
			}
			v.push_back({temp,num});
			//cout<<temp<<" "<<childcnt<<endl;
			temp = "";
		}
		else
			temp += s[i];
	}
	n = v.size();
	F(i,0,n-1)
	{
		if(pos < n)
		{
			f(0,v[pos].ss);
		}
	}
	//debug(maxlevel);
	cout<<maxlevel+1<<endl;
	F(i,0,maxlevel)
	{
		ll sz = ans[i].size();
		F(j,0,sz-1)
			cout<<ans[i][j]<<" ";
		cout<<endl;
	}
	return 0;
}


/*input
A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0
*/
#include <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define F(i,a,b) for(int i = a; i <= b; i++)
#define RF(i,a,b) for(int i = a; i >= b; i--)
#define pii pair<int,int>
#define PI 3.14159265358979323846264338327950288
#define ll long long
#define ff first
#define ss second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define debug(x) cout << #x << " = " << x << endl
#define INF 1000000009
#define mod 1000000007
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define MX 200005
vector < string > ans[MX];
vector < pair<string,ll> > v;
ll n;
ll pos;
ll maxlevel;
void f(ll lev,ll cnt)
{
	if(pos == n)
		return;
	maxlevel = max(maxlevel,lev);
	ans[lev].push_back(v[pos].ff);
	pos++;
	F(i,1,cnt)
	{
		f(lev+1,v[pos].ss);
	}
}
int main()
{
	std::ios::sync_with_stdio(false);
	string s;
	cin>>s;
	n = s.length();
	string temp = "";
	F(i,0,n-1)
	{
		if(s[i] == ',')
		{
			i++;
			int childcnt = (int)(s[i] - 48);
			i++;
			v.push_back({temp,childcnt});
			//cout<<temp<<" "<<childcnt<<endl;
			temp = "";
		}
		else
			temp += s[i];
	}
	n = v.size();
	F(i,0,n-1)
	{
		if(pos < n)
		{
			f(0,v[pos].ss);
		}
	}
	//debug(maxlevel);
	cout<<maxlevel+1<<endl;
	F(i,0,maxlevel)
	{
		ll sz = ans[i].size();
		F(j,0,sz-1)
			cout<<ans[i][j]<<" ";
		cout<<endl;
	}
	return 0;
}


#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
typedef pair<LL, PLL> PLPLL;
typedef pair<LL, VLL> PVLL;
typedef vector<PLL> VPLL;
typedef vector<std::string> VS;
typedef std::vector<LL>::iterator VLLitr;

#define arjun int main()
#define FOR(i, x, y) for (LL i = (x); i < (y); ++i)
#define FORC(i, x, y, in) for (LL i = (x); i < (y); i = i + in)
#define RFOR(i, x, y) for (LL i = (x); i >= (y); i--)
#define FORO(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define pb(e) push_back(e)
#define mp make_pair
#define All(x) x.begin(), x.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define PI 3.14159265
#define RESET(a, b) memset(a, b, sizeof(a))

template <typename T> T maxm(T a, T b) { return (a > b) ? a : b; }
template <typename T> T minm(T a, T b) { return (a < b) ? a : b; }
template <typename T> T power(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T> T powerm(T e, T n, T m) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = (x * p) % m;
    p = (p * p) % m;
    n >>= 1;
  }
  return x;
}
template <typename T> T InverseEuler(T a, T m) {
  return (a == 1 ? 1 : power(a, m - 2, m));
}
template <typename T> T lcm(T a, T b) { return (a * (b / __gcd(a, b))); }

string StringToUpper(string strToConvert) {
  std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(),
                 ::toupper);
  return strToConvert;
}

string StringToLower(string strToConvert) {
  std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(),
                 ::tolower);
  return strToConvert;
}

LL MAX = (LL)(3 * 1e18);
LL MOD = (LL)(1e9 + 7);

#ifdef JUDGE
#include <fstream>
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#else
#include <iostream>
using std::cin;
using std::cout;
#endif

// max number using these digits!
LL p = 0;
LL maxIndent = 0;
VS v[1000000];
string s;

void funcIndent(LL indent){
  string s2 = "";
  maxIndent = maxm(indent,maxIndent);
  std::size_t found;
  FOR(i,p,s.length()){
    if(s[i] == ','){
      found = i;
      break;
    }
  }
  s2 = s.substr(p,found-p);
  p = found+1;
  v[indent].push_back(s2);
  s2 = "";
  bool ya = 0;
  FOR(i,p,s.length()){
    if(s[i] == ','){
      found = i;
      ya = 1;
      break;
    }
  }
  if(!ya) found = s.length()-1;
  s2 = s.substr(p,found-p);
  p = found+1;
  LL x = atoi(s2.c_str());
  while(x>0){
    funcIndent(indent+1);
    x--;
  }
}

arjun {
  fast;
  cin>>s;
  LL n = s.length();
  LL level = 0;
  while(p<s.length()-1)
    funcIndent(0);
  cout<<maxIndent+1<<endl;
  FOR(i,0,maxIndent+1){
    FORO(j,v[i]){
      cout<<(*j)<<" ";
    }
    cout<<endl;
  }
  return 0;
}


#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
typedef pair<LL, PLL> PLPLL;
typedef pair<LL, VLL> PVLL;
typedef vector<PLL> VPLL;
typedef vector<std::string> VS;
typedef std::vector<LL>::iterator VLLitr;

#define arjun int main()
#define FOR(i, x, y) for (LL i = (x); i < (y); ++i)
#define FORC(i, x, y, in) for (LL i = (x); i < (y); i = i + in)
#define RFOR(i, x, y) for (LL i = (x); i >= (y); i--)
#define FORO(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define pb(e) push_back(e)
#define mp make_pair
#define All(x) x.begin(), x.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define PI 3.14159265
#define RESET(a, b) memset(a, b, sizeof(a))

template <typename T> T maxm(T a, T b) { return (a > b) ? a : b; }
template <typename T> T minm(T a, T b) { return (a < b) ? a : b; }
template <typename T> T power(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T> T powerm(T e, T n, T m) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = (x * p) % m;
    p = (p * p) % m;
    n >>= 1;
  }
  return x;
}
template <typename T> T InverseEuler(T a, T m) {
  return (a == 1 ? 1 : power(a, m - 2, m));
}
template <typename T> T lcm(T a, T b) { return (a * (b / __gcd(a, b))); }

string StringToUpper(string strToConvert) {
  std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(),
                 ::toupper);
  return strToConvert;
}

string StringToLower(string strToConvert) {
  std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(),
                 ::tolower);
  return strToConvert;
}

LL MAX = (LL)(3 * 1e18);
LL MOD = (LL)(1e9 + 7);

#ifdef JUDGE
#include <fstream>
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#else
#include <iostream>
using std::cin;
using std::cout;
#endif

// max number using these digits!
LL p = 0;
LL maxIndent = 0;

void funcIndent(LL indent,string s,VS* v){
  string s2 = "";
  maxIndent = maxm(indent,maxIndent);
  std::size_t found;
  FOR(i,p,s.length()){
    if(s[i] == ','){
      found = i;
      break;
    }
  }
  s2 = s.substr(p,found-p);
  p = found+1;
  v[indent].push_back(s2);
  s2 = "";
  bool ya = 0;
  FOR(i,p,s.length()){
    if(s[i] == ','){
      found = i;
      ya = 1;
      break;
    }
  }
  if(!ya) found = s.length()-1;
  s2 = s.substr(p,found-p);
  p = found+1;
  LL x = atoi(s2.c_str());
  while(x>0){
    funcIndent(indent+1,s,v);
    x--;
  }
}

arjun {
  fast;
  string s;
  cin>>s;
  LL n = s.length();
  VS* v = new VS[n];
  LL level = 0;
  while(p<s.length()-1)
    funcIndent(0,s,v);
  cout<<maxIndent+1<<endl;
  FOR(i,0,maxIndent+1){
    FORO(j,v[i]){
      cout<<(*j)<<" ";
    }
    cout<<endl;
  }
  return 0;
}


#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
typedef pair<LL, PLL> PLPLL;
typedef pair<LL, VLL> PVLL;
typedef vector<PLL> VPLL;
typedef vector<std::string> VS;
typedef std::vector<LL>::iterator VLLitr;

#define arjun int main()
#define FOR(i, x, y) for (LL i = (x); i < (y); ++i)
#define FORC(i, x, y, in) for (LL i = (x); i < (y); i = i + in)
#define RFOR(i, x, y) for (LL i = (x); i >= (y); i--)
#define FORO(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define pb(e) push_back(e)
#define mp make_pair
#define All(x) x.begin(), x.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define PI 3.14159265
#define RESET(a, b) memset(a, b, sizeof(a))

template <typename T> T maxm(T a, T b) { return (a > b) ? a : b; }
template <typename T> T minm(T a, T b) { return (a < b) ? a : b; }
template <typename T> T power(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T> T powerm(T e, T n, T m) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = (x * p) % m;
    p = (p * p) % m;
    n >>= 1;
  }
  return x;
}
template <typename T> T InverseEuler(T a, T m) {
  return (a == 1 ? 1 : power(a, m - 2, m));
}
template <typename T> T lcm(T a, T b) { return (a * (b / __gcd(a, b))); }

string StringToUpper(string strToConvert) {
  std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(),
                 ::toupper);
  return strToConvert;
}

string StringToLower(string strToConvert) {
  std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(),
                 ::tolower);
  return strToConvert;
}

LL MAX = (LL)(3 * 1e18);
LL MOD = (LL)(1e9 + 7);

#ifdef JUDGE
#include <fstream>
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#else
#include <iostream>
using std::cin;
using std::cout;
#endif

// max number using these digits!
LL p = 0;
LL maxIndent = 0;

void funcIndent(LL indent,string s,VS* v){
  string s2 = "";
  maxIndent = maxm(indent,maxIndent);
  std::size_t found;
  FOR(i,p,s.length()){
    if(s[i] == ','){
      found = i;
      break;
    }
  }
  s2 = s.substr(p,found-p);
  p = found+1;
  v[indent].push_back(s2);
  s2 = "";
  bool ya = 0;
  FOR(i,p,s.length()){
    if(s[i] == ','){
      found = i;
      ya = 1;
      break;
    }
  }
  if(!ya) found = s.length()-1;
  s2 = s.substr(p,found-p);
  p = found+1;
  LL x = atoi(s2.c_str());
  while(x!=0){
    funcIndent(indent+1,s,v);
    x--;
  }
}

arjun {
  fast;
  string s;
  cin>>s;
  LL n = s.length();
  VS* v = new VS[n];
  LL level = 0;
  while(p<s.length())
    funcIndent(0,s,v);
  cout<<maxIndent+1<<endl;
  FOR(i,0,maxIndent+1){
    FORO(j,v[i]){
      cout<<(*j)<<" ";
    }
    cout<<endl;
  }
  return 0;
}


#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
typedef pair<LL, PLL> PLPLL;
typedef pair<LL, VLL> PVLL;
typedef vector<PLL> VPLL;
typedef vector<std::string> VS;
typedef std::vector<LL>::iterator VLLitr;

#define arjun int main()
#define FOR(i, x, y) for (LL i = (x); i < (y); ++i)
#define FORC(i, x, y, in) for (LL i = (x); i < (y); i = i + in)
#define RFOR(i, x, y) for (LL i = (x); i >= (y); i--)
#define FORO(it, c)                                                            \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define pb(e) push_back(e)
#define mp make_pair
#define All(x) x.begin(), x.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define PI 3.14159265
#define RESET(a, b) memset(a, b, sizeof(a))

template <typename T> T maxm(T a, T b) { return (a > b) ? a : b; }
template <typename T> T minm(T a, T b) { return (a < b) ? a : b; }
template <typename T> T power(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T> T powerm(T e, T n, T m) {
  T x = 1, p = e;
  while (n) {
    if (n & 1)
      x = (x * p) % m;
    p = (p * p) % m;
    n >>= 1;
  }
  return x;
}
template <typename T> T InverseEuler(T a, T m) {
  return (a == 1 ? 1 : power(a, m - 2, m));
}
template <typename T> T lcm(T a, T b) { return (a * (b / __gcd(a, b))); }

string StringToUpper(string strToConvert) {
  std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(),
                 ::toupper);
  return strToConvert;
}

string StringToLower(string strToConvert) {
  std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(),
                 ::tolower);
  return strToConvert;
}

LL MAX = (LL)(3 * 1e18);
LL MOD = (LL)(1e9 + 7);

#ifdef JUDGE
#include <fstream>
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#else
#include <iostream>
using std::cin;
using std::cout;
#endif

// max number using these digits!
LL p = 0;
LL maxIndent = 0;

void funcIndent(LL indent,string s,VS* v){
  string s2 = "";
  LL p2 = p;
  maxIndent = maxm(indent,maxIndent);
  FOR(i,p,s.length()){
    p2++;
    if(s[i] == ','){
      break;
    }
    else{
      s2+=s[i];
    }
  }
  v[indent].push_back(s2);
  s2 = "";
  p = p2;
  FOR(i,p,s.length()){
    p2++;
    if(s[i] == ','){
      break;
    }
    else{
      s2+=s[i];
    }
  }
  LL x = atoi(s2.c_str());
  p = p2;
  while(x!=0){
    funcIndent(indent+1,s,v);
    x--;
  }
}

arjun {
  fast;
  string s;
  cin>>s;
  LL n = s.length();
  VS* v = new VS[n];
  LL level = 0;
  while(p<s.length())
    funcIndent(0,s,v);
  cout<<maxIndent+1<<endl;
  FOR(i,0,maxIndent+1){
    FORO(j,v[i]){
      cout<<(*j)<<" ";
    }
    cout<<endl;
  }
  return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define de(x) cout << #x << "=" << x << endl
const int N=1000005;
char s[N];
int n,m,p,top;
struct Node {
	string str;
	int d;
}a[N];
int sta[N],dd[N];
//void dfs(int d) {
//	string tmp="";
//	while(s[p]!=',') tmp+=s[p++];
//	++p;
//	int son=s[p++]-'0';
//	++p;
//	a[++m].str=tmp;
//	a[m].d=d;
//	for(int i=1;i<=son;++i) dfs(d+1);
//}
vector<string> ans[N];
int main() {;
	scanf("%s",s+1);
	int n=strlen(s+1);
	m=top=0;s[0]=',';
	int d=0;

	for(int p=0;p<=n;++p) {
		if(s[p]==',') {
			string tmp="";
			while(s[++p]!=',') tmp+=s[p];
			int son=s[++p]-'0';
			//cout<<tmp<<" "<<son<<endl;

			int t;
			if(top) {
				--sta[top];
				t=dd[top]+1;//de(t);
				if(sta[top]==0) --top;
			} else {
				t=1;
			}
			if(son) {
				sta[++top]=son;
				dd[top]=t;
			}
			//printf("%d %d\n",t,top);

			a[++m].str=tmp;
			a[m].d=t;
		}
	}

	d=0;
	for(int i=1;i<=m;++i) {
		d=max(d,a[i].d);
		ans[a[i].d].push_back(a[i].str);
	}
	printf("%d\n",d);
	for(int i=1;i<=d;++i) {
		int sz=ans[i].size();
		for(int j=0;j<sz;++j) cout<<ans[i][j]<<' ';
		puts("");
	}
	//for(int i=1;i<=m;++i) cout<<a[i].str<<' '<<a[i].d<<endl;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define de(x) cout << #x << "=" << x << endl
const int N=1000005;
char s[N];
int n,m,p;
struct Node {
	string str;
	int d;
}a[N];
void dfs(int d) {
	string tmp="";
	while(s[p]!=',') tmp+=s[p++];
	++p;
	int son=s[p++]-'0';
	++p;
	a[++m].str=tmp;
	a[m].d=d;
	for(int i=1;i<=son;++i) dfs(d+1);
}
vector<string> ans[N];
int main() {;
	scanf("%s",s+1);
	int n=strlen(s+1);
	p=1;m=0;
	while(p<=n) dfs(1);
	int d=0;
	for(int i=1;i<=m;++i) {
		d=max(d,a[i].d);
		ans[a[i].d].push_back(a[i].str);
	}
	printf("%d\n",d);
	for(int i=1;i<=d;++i) {
		int sz=ans[i].size();
		for(int j=0;j<sz;++j) cout<<ans[i][j]<<' ';
		puts("");
	}
	//for(int i=1;i<=m;++i) cout<<a[i].str<<' '<<a[i].d<<endl;
    return 0;
}


#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define de(x) cout << #x << "=" << x << endl
const int N=1000005;
char s[N];
int n,m,p;
struct Node {
	string str;
	int d;
}a[N];
void dfs(int d) {
	string tmp="";
	while(s[p]!=',') tmp+=s[p++];
	++p;
	int son=s[p++]-'0';
	++p;
	a[++m].str=tmp;
	a[m].d=d;
	for(int i=1;i<=son;++i) dfs(d+1);
}
vector<string> ans[N];
int main() {;
	scanf("%s",s+1);
	int n=strlen(s+1);
	p=1;m=0;
	while(p<=n) dfs(1);
	int d=0;
	for(int i=1;i<=m;++i) {
		d=max(d,a[i].d);
		ans[a[i].d].push_back(a[i].str);
	}
	printf("%d\n",d);
	for(int i=1;i<=d;++i) {
		int sz=ans[i].size();
		for(int j=0;j<sz;++j) cout<<ans[i][j]<<' ';
		puts("");
	}
	//for(int i=1;i<=m;++i) cout<<a[i].str<<' '<<a[i].d<<endl;
    return 0;
}


#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define de(x) cout << #x << "=" << x << endl
const int N=1000005;
char s[N];
int n,m,p;
struct Node {
	string str;
	int d;
}a[N];
void dfs(int d) {
	string tmp="";
	while(s[p]!=',') tmp+=s[p++];
	++p;
	int son=0;
	while(s[p]!=',') {
		son=son*10+s[p]-'0';
		++p;
	}
	++p;
	a[++m].str=tmp;
	a[m].d=d;
	for(int i=1;i<=son;++i) dfs(d+1);
}
vector<string> ans[N];
int main() {;
	scanf("%s",s+1);
	int n=strlen(s+1);
	++n;s[n]=',';
	p=1;m=0;
	while(p<=n) dfs(1);
	int d=0;
	for(int i=1;i<=m;++i) {
		d=max(d,a[i].d);
		ans[a[i].d].push_back(a[i].str);
	}
	printf("%d\n",d);
	for(int i=1;i<=d;++i) {
		int sz=ans[i].size();
		for(int j=0;j<sz;++j) cout<<ans[i][j]<<' ';
		puts("");
	}
	//for(int i=1;i<=m;++i) cout<<a[i].str<<' '<<a[i].d<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<pair<string,int> >v;
vector<string>ans[100005];
int k,maxa = 0;

void dfs(int c,int l){
	if (maxa < l)
	maxa = l;
	ans[l].push_back(v[k++].first);
	while (c--){
		if (v[k].second == 0){
			if (maxa<l+1)
			maxa = l+1;
			ans[l+1].push_back(v[k++].first);
		}
		else
		dfs(v[k].second,l+1);
	}
}

int main(){
	string str;
	int x;
	cin>>str;
	for (int i=0;i<str.size();i++){
		string a,b;
		while (str[i] != ',' && i<str.size()){
			a.push_back(str[i]);
			i++;
		}
		i++;
		int x=0;
		while (str[i] != ',' && i<str.size()){
			x = x*10+int(str[i]-'0');
			i++;
		}
		v.push_back(make_pair(a,x));
		//cout<<a<<" "<<x<<"\n";
	}
	for (k=0;k<v.size();k++){
		dfs(v[k].second,0);
		k--;
	}
	if (ans[0].size() == 0)
	cout<<"0\n";
	else
	cout<<maxa+1<<"\n";
	for (int i=0;ans[i].size() != 0;i++){
		for (int j=0;j<ans[i].size();j++){
			cout<<ans[i][j]<<" ";
		}
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<pair<string,int> >v;
vector<string>ans[100005];
int k,maxa = 0;

void dfs(int c,int l){
	if (maxa < l)
	maxa = l;
	ans[l].push_back(v[k++].first);
	while (c--){
		if (v[k].second == 0){
			if (maxa<l+1)
			maxa = l+1;
			ans[l+1].push_back(v[k++].first);
		}
		else
		dfs(v[k].second,l+1);
	}
}

int main(){
	string str;
	int x;
	cin>>str;
	for (int i=0;i<str.size();i++){
		string a,b;
		while (str[i] != ',' && i<str.size()){
			a.push_back(str[i]);
			i++;
		}
		i++;
		while (str[i] != ',' && i<str.size()){
			b.push_back(str[i]);
			i++;
		}
		int x=0;
		stringstream convert(b);
		convert>>x;
		v.push_back(make_pair(a,x));
		//cout<<a<<" "<<x<<"\n";
	}
	for (k=0;k<v.size();k++){
		dfs(v[k].second,0);
		k--;
	}
	if (ans[0].size() == 0)
	cout<<"0\n";
	else
	cout<<maxa+1<<"\n";
	for (int i=0;ans[i].size() != 0;i++){
		for (int j=0;j<ans[i].size();j++){
			cout<<ans[i][j]<<" ";
		}
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<pair<string,int> >v;
vector<string>ans[100005];
int k,maxa = 0;

void dfs(int c,int l){
	if (maxa < l)
	maxa = l;
	ans[l].push_back(v[k++].first);
	while (c--){
		if (v[k].second == 0){
			if (maxa<l+1)
			maxa = l+1;
			ans[l+1].push_back(v[k++].first);
		}
		else
		dfs(v[k].second,l+1);
	}
}

int main(){
	string str;
	int x;
	cin>>str;
	stringstream ss(str);
	string token;
	while (getline(ss,token,',')){
		str = token;
		getline(ss,token,',');
		stringstream convert(token);
		convert>>x;
		v.push_back(make_pair(str,x));
	}
	for (k=0;k<v.size();k++){
		dfs(v[k].second,0);
		k--;
	}
	if (maxa == 0)
	cout<<maxa<<"\n";
	else
	cout<<maxa+1<<"\n";
	for (int i=0;ans[i].size() != 0;i++){
		for (int j=0;j<ans[i].size();j++){
			cout<<ans[i][j]<<" ";
		}
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<pair<string,int> >v;
vector<string>ans[100005];
int k,maxa = 0;

void dfs(int c,int l){
	if (maxa < l)
	maxa = l;
	ans[l].push_back(v[k++].first);
	while (c--){
		if (v[k].second == 0){
			if (maxa<l+1)
			maxa = l+1;
			ans[l+1].push_back(v[k++].first);
		}
		else
		dfs(v[k].second,l+1);
	}
}

int main(){
	string str;
	int x;
	cin>>str;
	stringstream ss(str);
	string token;
	while (getline(ss,token,',')){
		str = token;
		getline(ss,token,',');
		stringstream convert(token);
		convert>>x;
		v.push_back(make_pair(str,x));
	}
	for (k=0;k<v.size();k++){
		dfs(v[k].second,0);
		k--;
	}
	if (ans[0].size() == 0)
	cout<<"0\n";
	else
	cout<<maxa+1<<"\n";
	for (int i=0;ans[i].size() != 0;i++){
		for (int j=0;j<ans[i].size();j++){
			cout<<ans[i][j]<<" ";
		}
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<pair<string,int> >v;
vector<string>ans[100005];
int k,maxa = 0;

void dfs(int c,int l){
	if (maxa < l)
	maxa = l;
	ans[l].push_back(v[k++].first);
	while (c > 0){
		if (v[k].second == 0){
			if (maxa<l+1) maxa = l+1;
			ans[l+1].push_back(v[k++].first);
		}
		else
		dfs(v[k].second,l+1);
		c--;
	}
}

int main(){
	string str;
	int x;
	cin>>str;
	for (int i=0;i<str.size();i++){
		string a,b;
		while (str[i] != ',' && i<str.size()){
			a.push_back(str[i]);
			i++;
		}
		i++;
		int x=0;
		while (str[i] != ',' && i<str.size()){
			x = x*10+int(str[i]-'0');
			i++;
		}
		v.push_back(make_pair(a,x));
	}
	for (k=0;k<v.size();k++){
		if (v[k].second == 0){
			ans[0].push_back(v[k].first);
			continue;
		}
		dfs(v[k].second,0);
		k--;
	}
	if (ans[0].size() == 0)
	cout<<"0\n";
	else
	cout<<maxa+1<<"\n";
	for (int i=0;ans[i].size() != 0;i++){
		for (int j=0;j<ans[i].size();j++){
			cout<<ans[i][j]<<" ";
		}
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<pair<string,int> >v;
vector<string>ans[100005];
int k,maxa = 0;

void dfs(int c,int l){
	if (maxa < l)
	maxa = l;
	ans[l].push_back(v[k++].first);
	while (c > 0){
		if (v[k].second == 0){
			if (maxa<l+1)
			maxa = l+1;
			ans[l+1].push_back(v[k++].first);
		}
		else
		dfs(v[k].second,l+1);
		c--;
	}
}

int main(){
	string str;
	int x;
	cin>>str;
	for (int i=0;i<str.size();i++){
		string a,b;
		while (str[i] != ',' && i<str.size()){
			a.push_back(str[i]);
			i++;
		}
		i++;
		int x=0;
		while (str[i] != ',' && i<str.size()){
			x = x*10+int(str[i]-'0');
			i++;
		}
		v.push_back(make_pair(a,x));
		//cout<<a<<" "<<x<<"\n";
	}
	for (k=0;k<v.size();k++){
		dfs(v[k].second,0);
		k--;
	}
	if (ans[0].size() == 0)
	cout<<"0\n";
	else
	cout<<maxa+1<<"\n";
	for (int i=0;ans[i].size() != 0;i++){
		for (int j=0;j<ans[i].size();j++){
			cout<<ans[i][j]<<" ";
		}
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<pair<string,int> >v;
vector<string>ans[100005];
int k,maxa = 0;

void dfs(int c,int l){
	if (maxa < l)
	maxa = l;
	ans[l].push_back(v[k++].first);
	while (c-- && c > 0){
		if (v[k].second == 0){
			if (maxa<l+1)
			maxa = l+1;
			ans[l+1].push_back(v[k++].first);
		}
		else
		dfs(v[k].second,l+1);
	}
}

int main(){
	string str;
	int x;
	cin>>str;
	for (int i=0;i<str.size();i++){
		string a,b;
		while (str[i] != ',' && i<str.size()){
			a.push_back(str[i]);
			i++;
		}
		i++;
		int x=0;
		while (str[i] != ',' && i<str.size()){
			x = x*10+int(str[i]-'0');
			i++;
		}
		v.push_back(make_pair(a,x));
		//cout<<a<<" "<<x<<"\n";
	}
	for (k=0;k<v.size();k++){
		dfs(v[k].second,0);
		k--;
	}
	if (ans[0].size() == 0)
	cout<<"0\n";
	else
	cout<<maxa+1<<"\n";
	for (int i=0;ans[i].size() != 0;i++){
		for (int j=0;j<ans[i].size();j++){
			cout<<ans[i][j]<<" ";
		}
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

string str;
int mxdep, cnt;
vector<string> com[1000000];
stringstream ss;

void dfs(int ct, int dep) {
    if( !ct ) return;
    mxdep = max(mxdep, dep);
    for(int i = 0; i<ct; ++i) {
        ss >> str >> cnt;
        com[dep].push_back(str);
        dfs(cnt, dep+1);
    }
}

int main() {ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> str;
    for(char& c:str) if( c == ',' ) c = ' ';
    ss = stringstream(str);
    while( ss >> str ) {
        com[0].push_back(str);
        ss >> cnt;
        dfs(cnt, 1);
    }
    cout << mxdep+1 << endl;
    for(int i = 0; i<=mxdep; ++i) {
        for(size_t j = 0; j<com[i].size(); ++j) {
            if( j ) cout << ' ';
            cout << com[i][j];
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
vector<pair<string,int> >v;
vector<string>ans[100005];
int k;

void dfs(int c,int l){
	ans[l].push_back(v[k++].first);
	while (c--){
		if (v[k].second == 0)
		ans[l+1].push_back(v[k++].first);
		else
		dfs(v[k].second,l+1);
	}
}

int main(){
	string str;
	int x;
	cin>>str;
	stringstream ss(str);
	string token;
	while (getline(ss,token,',')){
		str = token;
		getline(ss,token,',');
		stringstream convert(token);
		convert>>x;
		v.push_back(make_pair(str,x));
	}
	for (k=0;k<v.size();k++){
		dfs(v[k].second,0);
		k--;
	}
	for (int i=0;ans[i].size() != 0;i++){
		for (int j=0;j<ans[i].size();j++){
			cout<<ans[i][j]<<" ";
		}
		cout<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

template<typename T>
void sci(T& t) {
    cin >> t;
}

template<typename T, typename... Ts>
void sci(T& t, Ts&... ts) {
    sci(t);
    sci(ts...);
}

#define scid(vars...) int vars; sci(vars)
#define scidl(vars...) lint vars; sci(vars)
#define scidd(vars...) double vars; sci(vars)
#define scids(vars...) string vars; sci(vars)

typedef long long lint;

vector<vector<string> > a;

void dfs(int d, int mx) {
    if (a.size() == d) {
        a.push_back(vector<string>());
    }
    int rd = 0;
    while (rd != mx) {
        rd++;
        string msg;
        char c;
        while (true) {
            if (!(cin >> c)) {
                return;;
            }
            if (c == ',') {
                break;
            }
            msg += c;
        }
        int num = 0;
        while (true) {
            if (!(cin >> c)) {
                break;
            }
            if (c == ',') {
                break;
            }
            num = num * 10 + c - '0';
        }
        a[d].push_back(msg);
        dfs(d + 1, num);
    }
}

int main() {
    freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(false); cin.tie(0);

    dfs(0, -1);
    while (!a.empty() && a.back().empty()) {
        a.pop_back();
    }
    cout << a.size() << "\n";
    for (auto& i : a) {
        for (auto& j : i) {
            cout << j << " ";
        }
        cout << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[2000000];
int cur;
string s;

void make(int x) {
    a[cur] = x;
//    cout << cur << " " << x << "\n";
    while ((cur < s.length()) && (s[cur] != ',')) {
        cur++;
    }
    cur++;
 //   cout << '!' << cur << "\n";
    int kol = 0;
    while ((cur < s.length()) && (s[cur] != ',')) {
        if (s[cur] != ',') {
          kol = kol * 10 + s[cur] - '0';
        }
        cur++;
    }
    //cout << '!' << cur << "\n";
    cur++;
    for (int i = 0; i < kol; i++) {
        make(x + 1);
    }
}

void print(int x) {
    int q = x;
    while ((q < s.length()) && (s[q + 1] != ',')) {
        cout << s[q];
        q++;
    }
    cout << s[q];
}

int main()
{
 //  freopen("input.txt", "r", stdin);
 //   freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
   cin.tie(0);
// hello
    cin >> s;
    cur = 0;

    while (cur < s.length()) {
      make(1);
    }

    //cout << s;
    int mx = 0;
    for(int i = 0; i < s.length(); i++) {
        mx = max(mx, a[i]);
    }

    cout << mx << "\n";

    for(int i = 1; i < mx + 1; i++) {
        int kol = 0;
        for (int j = 0; j < s.length(); j++) {
            if (a[j] != i) {
                continue;
            }
            if(kol > 0) {
                cout << " ";
            }
            print(j);
            kol++;
        }
        cout << "\n";
    }
}


//Grits..Guts..Glory
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define fr(i,a,b) for(int i=a;i<b;++i)
#define sin(x) scanf("%d",&x)
#define sinl(x) scanf("%lld",&x)
#define print(x) printf("%d\n",x)
#define printw(x) printf("%d ",x)
#define printl(x) printf("%lld\n",x)
#define trace(a) cout<<#a<<": "<<a<<endl
#define ioS ios_base::sync_with_stdio(0);

typedef long long int ll;
typedef long double ld;
typedef pair<int,int>pi;
typedef long long int ll;
typedef vector<int> vi;

const int MAXN=5e5+100;
int t,timei[MAXN],timeo[MAXN],a[MAXN];
vector<int>g[MAXN],cnt[MAXN][27];

//====================METHOD-2==========================

void dfs(int x, int p, int h)
{
	t++;
	timei[x]=t;
	cnt[h][a[x]].pb(t);
	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p)dfs(u,x,h+1);
	}
	timeo[x]=t;
}

int main()
{

	//freopen("test.txt","r",stdin);
	int n,q;
	sin(n);sin(q);

	int x, y;

	fr(i,2,n+1)
	{
		sin(x);
		g[i].pb(x);
		g[x].pb(i);
	}
	char c;
	fr(i,1,n+1){c=getchar();a[i]=c-'a';}

	int v, h;
	dfs(1,0,1);

	fr(i,0,q)
	{
		sin(v);sin(h);
		int ct=0;
		for(int j=0;j<26;j++)
		{
			int temp=lower_bound(cnt[h][j].begin(),cnt[h][j].end(),timei[v])-cnt[h][j].begin();
			int temp2=upper_bound(cnt[h][j].begin(),cnt[h][j].end(),timeo[v])-cnt[h][j].begin();
			temp2-=temp;
			if(temp2%2==1)ct++;
		}
		if(ct>1)printf("No\n");
		else printf("Yes\n");
	}

}


//Grits..Guts..Glory
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define MOD 1000000007
#define fr(i,a,b) for(int i=a;i<b;++i)
#define sin(x) scanf("%d",&x)
#define sinl(x) scanf("%lld",&x)
#define print(x) printf("%d\n",x)
#define printw(x) printf("%d ",x)
#define printl(x) printf("%lld\n",x)
#define trace(a) cout<<#a<<": "<<a<<endl
#define ioS ios_base::sync_with_stdio(0);cin.tie(0);

typedef long long int ll;
typedef long double ld;
typedef pair<int,int>pi;
typedef long long int ll;
typedef vector<int> vi;

const int MAXN=2e5+10;
vector<int>g[MAXN],Q[MAXN];
int sz[MAXN],c[MAXN],big[MAXN];;
int cnt[MAXN][27],v[MAXN],h[MAXN];
map<int, int>ans[MAXN];
int a[100];
int depth[MAXN],cntf[MAXN];

void dfs0(int x, int p,int d)
{
	sz[x]=1;
	depth[x]=d;
	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u==p)continue;
		dfs0(u,x,d+1);
		sz[x]+=sz[u];
	}
}

void add(int x, int p,int z)
{
	int h=depth[x];
	cntf[h]-=cnt[h][a[x]];
	cnt[h][a[x]]^=1;
	cntf[h]+=cnt[h][a[x]];
//	trace(x,h,cntf[h]);

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && big[u]!=1)add(u,x,z);
	}
}

void dfs(int x, int p, int keep)
{
	int mx=0,bigc=0;

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && sz[u]>mx)mx=sz[u],bigc=u;
	}

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && bigc!=u)dfs(u,x,0);
	}
	if(mx!=0)big[bigc]=1,dfs(bigc,x,1);

	add(x,p,1);

	fr(i,0,Q[x].size())
	{

		int ht=Q[x][i];
		//trace(x,ht,cntf[ht]);
		if(cntf[ht]<=1){ans[x][ht]=1;}
		else ans[x][ht]=0;
	}

	if(mx!=0)big[bigc]=0;
	if(keep==0)add(x,p,-1);
}

int main()
{

	//freopen("test.txt","r",stdin);
	int n,q;
	sin(n);sin(q);

	int x, y;

	fr(i,2,n+1)
	{
		sin(x);
		//g[i].pb(x);
		g[x].pb(i);
	}

	char c;
	fr(i,1,n+1){cin>>c;a[i]=c-'a';}

	fr(i,0,q)
	{
		sin(v[i]);sin(h[i]);
		Q[v[i]].pb(h[i]);
	}

	dfs0(1,0,1);

	//fr(i,1,n+1)trace(depth[i]);

	dfs(1,0,0);

	fr(i,0,q)
	{
		if(ans[v[i]][h[i]]==1)printf("%s\n","Yes");
		else printf("%s\n","No");
	}

}


//Grits..Guts..Glory
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define MOD 1000000007
#define fr(i,a,b) for(int i=a;i<b;++i)
#define sin(x) scanf("%d",&x)
#define sinl(x) scanf("%lld",&x)
#define print(x) printf("%d\n",x)
#define printw(x) printf("%d ",x)
#define printl(x) printf("%lld\n",x)
#define trace(a) cout<<#a<<": "<<a<<endl
#define ioS ios_base::sync_with_stdio(0);cin.tie(0);

typedef long long int ll;
typedef long double ld;
typedef pair<int,int>pi;
typedef long long int ll;
typedef vector<int> vi;

//==========================================
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
//============================================

const int MAXN=5e5+100;
vector<int>g[MAXN],Q[MAXN];
int sz[MAXN],c[MAXN],big[MAXN];;
int v[MAXN],h[MAXN];
map<int, int>ans[MAXN],cnt[MAXN];
int a[100];
int depth[MAXN],cntf[MAXN];

void dfs0(int x, int p,int d)
{
	sz[x]=1;
	depth[x]=d;
	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u==p)continue;
		dfs0(u,x,d+1);
		sz[x]+=sz[u];
	}
}

void add(int x, int p,int z)
{
	int h=depth[x];
	cntf[h]-=cnt[h][a[x]];
	cnt[h][a[x]]^=1;
	cntf[h]+=cnt[h][a[x]];
	trace(x,h,cntf[h]);

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && big[u]!=1)add(u,x,z);
	}
}

void dfs(int x, int p, int keep)
{
	int mx=0,bigc=0;

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && sz[u]>mx)mx=sz[u],bigc=u;
	}

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && bigc!=u)dfs(u,x,0);
	}
	if(mx!=0)big[bigc]=1,dfs(bigc,x,1);

	add(x,p,1);

	fr(i,0,Q[x].size())
	{

		int ht=Q[x][i];
		//trace(x,ht,cntf[ht]);
		if(cntf[ht]<=1){ans[x][ht]=1;}
		else ans[x][ht]=0;
	}

	if(mx!=0)big[bigc]=0;
	if(keep==0)add(x,p,-1);
}

int main()
{

	//freopen("test.txt","r",stdin);
	int n,q;
	sin(n);sin(q);

	int x, y;

	fr(i,2,n+1)
	{
		sin(x);
		//g[i].pb(x);
		g[x].pb(i);
	}

	char c;
	fr(i,1,n+1){cin>>c;a[i]=c-'a';}

	fr(i,0,q)
	{
		sin(v[i]);sin(h[i]);
		Q[v[i]].pb(h[i]);
	}

	dfs0(1,0,1);

	//fr(i,1,n+1)trace(depth[i]);

	dfs(1,0,0);

	fr(i,0,q)
	{
		if(ans[v[i]][h[i]]==1)printf("%s\n","Yes");
		else printf("%s\n","No");
	}

}


//Grits..Guts..Glory
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define MOD 1000000007
#define fr(i,a,b) for(int i=a;i<b;++i)
#define sin(x) scanf("%d",&x)
#define sinl(x) scanf("%lld",&x)
#define print(x) printf("%d\n",x)
#define printw(x) printf("%d ",x)
#define printl(x) printf("%lld\n",x)
#define trace(a) cout<<#a<<": "<<a<<endl
#define ioS ios_base::sync_with_stdio(0);cin.tie(0);

typedef long long int ll;
typedef long double ld;
typedef pair<int,int>pi;
typedef long long int ll;
typedef vector<int> vi;

const int MAXN=5e5+100;
vector<int>g[MAXN],Q[MAXN];
int sz[MAXN],c[MAXN],big[MAXN];;
int v[MAXN],h[MAXN];
map<int, int>ans[MAXN],cnt[MAXN];
int a[100];
int depth[MAXN],cntf[MAXN];

void dfs0(int x, int p,int d)
{
	sz[x]=1;
	depth[x]=d;
	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u==p)continue;
		dfs0(u,x,d+1);
		sz[x]+=sz[u];
	}
}

void add(int x, int p,int z)
{
	int h=depth[x];
	cntf[h]-=cnt[h][a[x]];
	cnt[h][a[x]]^=1;
	cntf[h]+=cnt[h][a[x]];
//	trace(x,h,cntf[h]);

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && big[u]!=1)add(u,x,z);
	}
}

void dfs(int x, int p, int keep)
{
	int mx=0,bigc=0;

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && sz[u]>mx)mx=sz[u],bigc=u;
	}

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && bigc!=u)dfs(u,x,0);
	}
	if(mx!=0)big[bigc]=1,dfs(bigc,x,1);

	add(x,p,1);

	fr(i,0,Q[x].size())
	{

		int ht=Q[x][i];
		//trace(x,ht,cntf[ht]);
		if(cntf[ht]<=1){ans[x][ht]=1;}
		else ans[x][ht]=0;
	}

	if(mx!=0)big[bigc]=0;
	if(keep==0)add(x,p,-1);
}

int main()
{

	//freopen("test.txt","r",stdin);
	int n,q;
	sin(n);sin(q);

	int x, y;

	fr(i,2,n+1)
	{
		sin(x);
		//g[i].pb(x);
		g[x].pb(i);
	}

	char c;
	fr(i,1,n+1){cin>>c;a[i]=c-'a';}

	fr(i,0,q)
	{
		sin(v[i]);sin(h[i]);
		Q[v[i]].pb(h[i]);
	}

	dfs0(1,0,1);

	//fr(i,1,n+1)trace(depth[i]);

	dfs(1,0,0);

	fr(i,0,q)
	{
		if(ans[v[i]][h[i]]==1)printf("%s\n","Yes");
		else printf("%s\n","No");
	}

}


//Grits..Guts..Glory
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define fr(i,a,b) for(int i=a;i<b;++i)
#define sin(x) scanf("%d",&x)
#define sinl(x) scanf("%lld",&x)
#define print(x) printf("%d\n",x)
#define printw(x) printf("%d ",x)
#define printl(x) printf("%lld\n",x)
#define trace(a) cout<<#a<<": "<<a<<endl
#define ioS ios_base::sync_with_stdio(0);

typedef long long int ll;
typedef long double ld;
typedef pair<int,int>pi;
typedef long long int ll;
typedef vector<int> vi;

const int MAXN=5e5+100;
int t,timei[MAXN],timeo[MAXN],a[MAXN];
vector<int>g[MAXN],cnt[MAXN][27];

//====================METHOD-2==========================

void dfs(int x, int p, int h)
{
	t++;
	timei[x]=t;
	cnt[h][a[x]].pb(t);
	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p)dfs(u,x,h+1);
	}
	timeo[x]=t;
}

int main()
{

	//freopen("test.txt","r",stdin);
	int n,q;
	sin(n);sin(q);

	int x, y;

	fr(i,2,n+1)
	{
		sin(x);
		g[i].pb(x);
		g[x].pb(i);
	}
	char c;
	fr(i,1,n+1){cin>>c;a[i]=c-'a';}

	int v, h;
	dfs(1,0,1);

	fr(i,0,q)
	{
		sin(v);sin(h);
		int ct=0;
		for(int j=0;j<26;j++)
		{
			int temp=lower_bound(cnt[h][j].begin(),cnt[h][j].end(),timei[v])-cnt[h][j].begin();
			int temp2=upper_bound(cnt[h][j].begin(),cnt[h][j].end(),timeo[v])-cnt[h][j].begin();
			temp2-=temp;
			if(temp2%2==1)ct++;
		}
		if(ct>1)printf("No\n");
		else printf("Yes\n");
	}

}


//Grits..Guts..Glory
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define fr(i,a,b) for(int i=a;i<b;++i)
#define sin(x) scanf("%d",&x)
#define sinl(x) scanf("%lld",&x)
#define print(x) printf("%d\n",x)
#define printw(x) printf("%d ",x)
#define printl(x) printf("%lld\n",x)
#define trace(a) cout<<#a<<": "<<a<<endl
#define ioS ios_base::sync_with_stdio(0);

typedef long long int ll;
typedef long double ld;
typedef pair<int,int>pi;
typedef long long int ll;
typedef vector<int> vi;

const int MAXN=5e5+100;
int t,timei[MAXN],timeo[MAXN],a[MAXN];
vector<int>g[MAXN],cnt[MAXN][27];

//====================METHOD-2==========================

void dfs(int x, int p, int h)
{
	t++;
	timei[x]=t;
	cnt[h][a[x]].pb(t);
	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p)dfs(u,x,h+1);
	}
	timeo[x]=t;
}

int main()
{

	//freopen("test.txt","r",stdin);
	int n,q;
	sin(n);sin(q);

	int x, y;

	fr(i,2,n+1)
	{
		sin(x);
		g[i].pb(x);
		g[x].pb(i);
	}
	char c;
	fr(i,1,n+1){scanf("%c",&c);a[i]=c-'a';}

	int v, h;
	dfs(1,0,1);

	fr(i,0,q)
	{
		sin(v);sin(h);
		int ct=0;
		for(int j=0;j<26;j++)
		{
			int temp=lower_bound(cnt[h][j].begin(),cnt[h][j].end(),timei[v])-cnt[h][j].begin();
			int temp2=upper_bound(cnt[h][j].begin(),cnt[h][j].end(),timeo[v])-cnt[h][j].begin();
			temp2-=temp;
			if(temp2%2==1)ct++;
		}
		if(ct>1)printf("No\n");
		else printf("Yes\n");
	}

}


//Grits..Guts..Glory
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define re return
#define ss second
#define pb push_back
#define mpk make_pair
#define MOD 1000000007
#define fr(i,a,b) for(int i=a;i<b;++i)
#define sin(x) scanf("%d",&x)
#define sinl(x) scanf("%lld",&x)
#define print(x) printf("%d\n",x)
#define printw(x) printf("%d ",x)
#define printl(x) printf("%lld\n",x)
#define trace(a) cout<<#a<<": "<<a<<endl
#define ioS ios_base::sync_with_stdio(0);cin.tie(0);

typedef long long int ll;
typedef long double ld;
typedef pair<int,int>pi;
typedef long long int ll;
typedef vector<int> vi;

const int MAXN=5e5+10;
vector<int>g[MAXN],Q[MAXN];
int sz[MAXN],c[MAXN],big[MAXN];;
int cnt[MAXN][27],v[MAXN],h[MAXN];
map<int, int>ans[MAXN];
int a[100];
int depth[MAXN],cntf[MAXN];

void dfs0(int x, int p,int d)
{
	sz[x]=1;
	depth[x]=d;
	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u==p)continue;
		dfs0(u,x,d+1);
		sz[x]+=sz[u];
	}
}

void add(int x, int p,int z)
{
	int h=depth[x];
	cntf[h]-=cnt[h][a[x]];
	cnt[h][a[x]]^=1;
	cntf[h]+=cnt[h][a[x]];
	trace(x,h,cntf[h]);

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && big[u]!=1)add(u,x,z);
	}
}

void dfs(int x, int p, int keep)
{
	int mx=0,bigc=0;

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && sz[u]>mx)mx=sz[u],bigc=u;
	}

	fr(i,0,g[x].size())
	{
		int u=g[x][i];
		if(u!=p && bigc!=u)dfs(u,x,0);
	}
	if(mx!=0)big[bigc]=1,dfs(bigc,x,1);

	add(x,p,1);

	fr(i,0,Q[x].size())
	{

		int ht=Q[x][i];

		if(cntf[ht]<=1){ans[x][ht]=1;}
		else ans[x][ht]=0;
	}

	if(mx!=0)big[bigc]=0;
	if(keep==0)add(x,p,-1);
}

int main()
{

	//freopen("test.txt","r",stdin);
	int n,q;
	sin(n);sin(q);

	int x, y;

	fr(i,2,n+1)
	{
		sin(x);
		//g[i].pb(x);
		g[x].pb(i);
	}

	char c;
	fr(i,1,n+1){cin>>c;a[i]=c-'a';}

	fr(i,0,q)
	{
		sin(v[i]);sin(h[i]);
		Q[v[i]].pb(h[i]);
	}

	dfs0(1,0,1);

	dfs(1,0,0);

	fr(i,0,q)
	{
		if(ans[v[i]][h[i]]==1)printf("%s\n","Yes");
		else printf("%s\n","No");
	}

}


//In the name of god
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define speedup ios :: sync_with_stdio(false)

const int MN=100*100*100;

int n,m,par[MN],start[MN],Time,dep[MN],finish[MN],depth;
vector<int> g[MN];
vector<pii> row[MN];
int mem[MN][30];

void dfs(int v){
	row[dep[v]].pb(pii(start[v],v));
	for (int i=0;i<g[v].size();i++){
		int u=g[v][i];
		start[u]=++Time;
		dep[u]=dep[v]+1;
		depth=max(dep[u],depth);
		dfs(u);
	}
	finish[v]=++Time;
}

void prep(){
	for (int i=0;i<=depth;i++)
		sort(row[i].begin(),row[i].end());
	for (int i=0;i<MN;i++){
		for (int j=1;j<row[i].size();j++){
			for (int k=0;k<30;k++)
				mem[row[i][j].ss][k]+=mem[row[i][j-1].ss][k];
		}
	}
}

bool handle(int v,int h){
	if (h<=dep[v] || h>depth)
		return 1;
	vector<pii> :: iterator it1,it2;
	it1=lower_bound(row[h].begin(),row[h].end(),pii(start[v],0));
	if (it1==row[h].end())
		return 1;
	it2=lower_bound(row[h].begin(),row[h].end(),pii(finish[v],0));
	if (row[h].end()==row[h].begin())
		return 1;
	if (it2==row[h].begin())
		return 1;
	it2--;
	int u=(*it1).ss,w=(*it2).ss;
	bool flag=0;
	for (int i=0;i<30;i++){
		int tmp=0;
		if (it1!=row[h].begin())
			tmp=mem[(*(it1-1)).ss][i];
		if ( (mem[w][i]-tmp)%2){
			if (flag)
				return 0;
			flag=1;
		}
	}
	return 1;
}

int main(){
	speedup;
	scanf("%d %d",&n,&m);
	for (int i=1;i<n;i++){
		int p;
		scanf("%d",&p);
		p--;
		g[p].pb(i);
		par[i]=p;
	}
	char str[MN];
	scanf("%s",str);
	for (int i=0;i<n;i++){
		char c=str[i];
		mem[i][c-'a']++;
	}

	dfs(0);
	prep();
	for (int i=0;i<m;i++){
		int v,h;
		scanf("%d %d",&v,&h);
		if (handle(--v,--h))
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
    return 0;
}


//In the name of god
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define speedup ios :: sync_with_stdio(false)

const int MN=100*100*100;

int n,m,par[MN],start[MN],Time,dep[MN],finish[MN];
vector<int> g[MN];
vector<pii> row[MN];
int mem[MN][30];

void dfs(int v){
	for (int i=0;i<g[v].size();i++){
		int u=g[v][i];
		if (u==par[v])
			continue;
		start[u]=++Time;
		dfs(u);
	}
	finish[v]=++Time;
}

void bfs(){
	vector<int> q;
	q.pb(0);
	row[0].pb(pii(0,0));
	for (int i=0;i<q.size();i++){
		for (int j=0;j<g[q[i]].size();j++){
			int u=g[q[i]][j];
			if (u==par[q[i]])
				continue;
			q.pb(u);
			dep[u]=dep[q[i]]+1;
			row[dep[u]].pb(pii(start[u],u));
		}
	}
}

void prep(){
	for (int i=0;i<MN;i++)
		sort(row[i].begin(),row[i].end());
	for (int i=0;i<MN;i++){
		for (int j=1;j<row[i].size();j++){
			for (int k=0;k<30;k++)
				mem[row[i][j].ss][k]+=mem[row[i][j-1].ss][k];
		}
	}
}

bool handle(int v,int h){
	if (h<=dep[v])
		return 1;
	vector<pii> :: iterator it1,it2;
	it1=lower_bound(row[h].begin(),row[h].end(),pii(start[v],0));
	if (it1==row[h].end())
		return 1;
	it2=lower_bound(row[h].begin(),row[h].end(),pii(finish[v],0));
	it2--;
	int u=(*it1).ss,w=(*it2).ss;
	bool flag=0;
	for (int i=0;i<30;i++){
		int tmp=0;
		if (it1!=row[h].begin())
			tmp=mem[(*(it1-1)).ss][i];
		if ( (mem[w][i]-tmp)%2){
			if (flag)
				return 0;
			flag=1;
		}
	}
	return 1;
}

int main(){
	speedup;
	cin>>n>>m;
	for (int i=1;i<n;i++){
		int p;
		cin>>p;
		p--;
		g[i].pb(p);
		g[p].pb(i);
		par[i]=p;
	}

	for (int i=0;i<n;i++){
		char c;
		cin>>c;
		mem[i][c-'a']++;
	}

	dfs(0);
	bfs();
	prep();
	for (int i=0;i<m;i++){
		int v,h;
		cin>>v>>h;
		if (handle(--v,--h))
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
    return 0;
}


//In the name of god
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define speedup ios :: sync_with_stdio(false)

const int MN=100*100*100;

int n,m,par[MN],start[MN],Time,dep[MN],finish[MN];
vector<int> g[MN];
vector<pii> row[MN];
int mem[MN][30];

void dfs(int v){
	for (int i=0;i<g[v].size();i++){
		int u=g[v][i];
		if (u==par[v])
			continue;
		start[u]=++Time;
		dfs(u);
	}
	finish[v]=++Time;
}

void bfs(){
	vector<int> q;
	q.pb(0);
	row[0].pb(pii(0,0));
	for (int i=0;i<q.size();i++){
		for (int j=0;j<g[q[i]].size();j++){
			int u=g[q[i]][j];
			if (u==par[q[i]])
				continue;
			q.pb(u);
			dep[u]=dep[q[i]]+1;
			row[dep[u]].pb(pii(start[u],u));
		}
	}
}

void prep(){
	for (int i=0;i<MN;i++)
		sort(row[i].begin(),row[i].end());
	for (int i=0;i<MN;i++){
		for (int j=1;j<row[i].size();j++){
			for (int k=0;k<30;k++)
				mem[row[i][j].ss][k]+=mem[row[i][j-1].ss][k];
		}
	}
}

bool handle(int v,int h){
	if (h<=dep[v])
		return 1;
	vector<pii> :: iterator it1,it2;
	it1=lower_bound(row[h].begin(),row[h].end(),pii(start[v],0));
	if (it1==row[h].end())
		return 1;
	it2=lower_bound(row[h].begin(),row[h].end(),pii(finish[v],0));
	it2--;
	int u=(*it1).ss,w=(*it2).ss;
	bool flag=0;
	for (int i=0;i<30;i++){
		int tmp=0;
		if (it1!=row[h].begin())
			tmp=mem[(*(it1-1)).ss][i];
		if ( (mem[w][i]-tmp)%2){
			if (flag)
				return 0;
			flag=1;
		}
	}
	return 1;
}

int main(){
	cin>>n>>m;
	for (int i=1;i<n;i++){
		int p;
		cin>>p;
		p--;
		g[i].pb(p);
		g[p].pb(i);
		par[i]=p;
	}

	for (int i=0;i<n;i++){
		char c;
		cin>>c;
		mem[i][c-'a']++;
	}

	dfs(0);
	bfs();
	prep();
	for (int i=0;i<m;i++){
		int v,h;
		cin>>v>>h;
		if (handle(--v,--h))
			cout<<"YES\n";
		else
			cout<<"NO\n";
	}
    return 0;
}


//In the name of god
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define speedup ios :: sync_with_stdio(false)

const int MN=100*100*100;

int n,m,par[MN],start[MN],Time,dep[MN],finish[MN];
vector<int> g[MN];
vector<pii> row[MN];
int mem[MN][30];

void dfs(int v){
	for (int i=0;i<g[v].size();i++){
		int u=g[v][i];
		if (u==par[v])
			continue;
		start[u]=++Time;
		dfs(u);
	}
	finish[v]=++Time;
}

void bfs(){
	vector<int> q;
	q.pb(0);
	row[0].pb(pii(0,0));
	for (int i=0;i<q.size();i++){
		for (int j=0;j<g[q[i]].size();j++){
			int u=g[q[i]][j];
			if (u==par[q[i]])
				continue;
			q.pb(u);
			dep[u]=dep[q[i]]+1;
			row[dep[u]].pb(pii(start[u],u));
		}
	}
}

void prep(){
	for (int i=0;i<MN;i++)
		sort(row[i].begin(),row[i].end());
	for (int i=0;i<MN;i++){
		for (int j=1;j<row[i].size();j++){
			for (int k=0;k<30;k++)
				mem[row[i][j].ss][k]+=mem[row[i][j-1].ss][k];
		}
	}
}

bool handle(int v,int h){
	if (h<=dep[v])
		return 1;
	vector<pii> :: iterator it1,it2;
	it1=lower_bound(row[h].begin(),row[h].end(),pii(start[v],0));
	if (it1==row[h].end())
		return 1;
	it2=lower_bound(row[h].begin(),row[h].end(),pii(finish[v],0));
	it2--;
	int u=(*it1).ss,w=(*it2).ss;
	bool flag=0;
	for (int i=0;i<30;i++){
		int tmp=0;
		if (it1!=row[h].begin())
			tmp=mem[(*(it1-1)).ss][i];
		if ( (mem[w][i]-tmp)%2){
			if (flag)
				return 0;
			flag=1;
		}
	}
	return 1;
}

int main(){
	speedup;
	scanf("%d %d",&n,&m);
	for (int i=1;i<n;i++){
		int p;
		scanf("%d",&p);
		p--;
		g[i].pb(p);
		g[p].pb(i);
		par[i]=p;
	}
	char str[MN];
	scanf("%s",str);
	for (int i=0;i<n;i++){
		char c=str[i];
		mem[i][c-'a']++;
	}

	dfs(0);
	bfs();
	prep();
	for (int i=0;i<m;i++){
		int v,h;
		scanf("%d %d",&v,&h);
		if (handle(--v,--h))
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
    return 0;
}


//In the name of god
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define speedup ios :: sync_with_stdio(false)

const int MN=100*100*100;

int n,m,par[MN],start[MN],Time,dep[MN],finish[MN];
vector<int> g[MN];
vector<pii> row[MN];
int mem[MN][30];

void dfs(int v){
	for (int i=0;i<g[v].size();i++){
		int u=g[v][i];
		if (u==par[v])
			continue;
		start[u]=++Time;
		dfs(u);
	}
	finish[v]=++Time;
}

void bfs(){
	vector<int> q;
	q.pb(0);
	row[0].pb(pii(0,0));
	for (int i=0;i<q.size();i++){
		for (int j=0;j<g[q[i]].size();j++){
			int u=g[q[i]][j];
			if (u==par[q[i]])
				continue;
			q.pb(u);
			dep[u]=dep[q[i]]+1;
			row[dep[u]].pb(pii(start[u],u));
		}
	}
}

void prep(){
	for (int i=0;i<MN;i++)
		sort(row[i].begin(),row[i].end());
	for (int i=0;i<MN;i++){
		for (int j=1;j<row[i].size();j++){
			for (int k=0;k<30;k++)
				mem[row[i][j].ss][k]+=mem[row[i][j-1].ss][k];
		}
	}
}

bool handle(int v,int h){
	if (h<=dep[v])
		return 1;
	vector<pii> :: iterator it1,it2;
	it1=lower_bound(row[h].begin(),row[h].end(),pii(start[v],0));
	if (it1==row[h].end())
		return 1;
	it2=lower_bound(row[h].begin(),row[h].end(),pii(finish[v],0));
	it2--;
	int u=(*it1).ss,w=(*it2).ss;
	bool flag=0;
	for (int i=0;i<30;i++){
		int tmp=0;
		if (it1!=row[h].begin())
			tmp=mem[(*(it1-1)).ss][i];
		if ( (mem[w][i]-tmp)%2){
			if (flag)
				return 0;
			flag=1;
		}
	}
	return 1;
}

int main(){
	cin>>n>>m;
	for (int i=1;i<n;i++){
		int p;
		cin>>p;
		p--;
		g[i].pb(p);
		g[p].pb(i);
		par[i]=p;
	}

	for (int i=0;i<n;i++){
		char c;
		cin>>c;
		mem[i][c-'a']++;
	}

	dfs(0);
	bfs();
	prep();
	for (int i=0;i<m;i++){
		int v,h;
		cin>>v>>h;
		if (handle(--v,--h))
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
    return 0;
}


//In the name of god
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define speedup ios :: sync_with_stdio(false)

const int MN=100*100*100;

int n,m,par[MN],start[MN],Time,dep[MN],finish[MN],depth;
vector<int> g[MN];
vector<pii> row[MN];
int mem[MN][30];

void dfs(int v){
	for (int i=0;i<g[v].size();i++){
		int u=g[v][i];
		if (u==par[v])
			continue;
		start[u]=++Time;
		dfs(u);
	}
	finish[v]=++Time;
}

void bfs(){
	vector<int> q;
	q.pb(0);
	row[0].pb(pii(0,0));
	for (int i=0;i<q.size();i++){
		for (int j=0;j<g[q[i]].size();j++){
			int u=g[q[i]][j];
			if (u==par[q[i]])
				continue;
			q.pb(u);
			dep[u]=dep[q[i]]+1;
			row[dep[u]].pb(pii(start[u],u));
			depth=dep[u];
		}
	}
}

void prep(){
	for (int i=0;i<MN;i++)
		sort(row[i].begin(),row[i].end());
	for (int i=0;i<MN;i++){
		for (int j=1;j<row[i].size();j++){
			for (int k=0;k<30;k++)
				mem[row[i][j].ss][k]+=mem[row[i][j-1].ss][k];
		}
	}
}

bool handle(int v,int h){
	if (h<=dep[v] || h>depth)
		return 1;
	vector<pii> :: iterator it1,it2;
	it1=lower_bound(row[h].begin(),row[h].end(),pii(start[v],0));
	if (it1==row[h].end())
		return 1;
	it2=lower_bound(row[h].begin(),row[h].end(),pii(finish[v],0));
	it2--;
	int u=(*it1).ss,w=(*it2).ss;
	bool flag=0;
	for (int i=0;i<30;i++){
		int tmp=0;
		if (it1!=row[h].begin())
			tmp=mem[(*(it1-1)).ss][i];
		if ( (mem[w][i]-tmp)%2){
			if (flag)
				return 0;
			flag=1;
		}
	}
	return 1;
}

int main(){
	speedup;
	scanf("%d %d",&n,&m);
	for (int i=1;i<n;i++){
		int p;
		scanf("%d",&p);
		p--;
		g[i].pb(p);
		g[p].pb(i);
		par[i]=p;
	}
	char str[MN];
	scanf("%s",str);
	for (int i=0;i<n;i++){
		char c=str[i];
		mem[i][c-'a']++;
	}

	dfs(0);
	bfs();
	prep();
	for (int i=0;i<m;i++){
		int v,h;
		scanf("%d %d",&v,&h);
		if (handle(--v,--h))
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
    return 0;
}


//In the name of god
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define speedup ios :: sync_with_stdio(false)

const int MN=100*100*100;

int n,m,par[MN],start[MN],Time,dep[MN],finish[MN],depth;
vector<int> g[MN];
vector<pii> row[MN];
int mem[MN][30];

void dfs(int v){
	for (int i=0;i<g[v].size();i++){
		int u=g[v][i];
		if (u==par[v])
			continue;
		start[u]=++Time;
		dfs(u);
	}
	finish[v]=++Time;
}

void bfs(){
	vector<int> q;
	q.pb(0);
	row[0].pb(pii(0,0));
	for (int i=0;i<q.size();i++){
		for (int j=0;j<g[q[i]].size();j++){
			int u=g[q[i]][j];
			if (u==par[q[i]])
				continue;
			q.pb(u);
			dep[u]=dep[q[i]]+1;
			row[dep[u]].pb(pii(start[u],u));
			depth=dep[u];
		}
	}
}

void prep(){
	for (int i=0;i<MN;i++)
		sort(row[i].begin(),row[i].end());
	for (int i=0;i<MN;i++){
		for (int j=1;j<row[i].size();j++){
			for (int k=0;k<30;k++)
				mem[row[i][j].ss][k]+=mem[row[i][j-1].ss][k];
		}
	}
}

bool handle(int v,int h){
	if (h<=dep[v] || h>depth)
		return 1;
	vector<pii> :: iterator it1,it2;
	it1=lower_bound(row[h].begin(),row[h].end(),pii(start[v],0));
	if (it1==row[h].end())
		return 1;
	it2=lower_bound(row[h].begin(),row[h].end(),pii(finish[v],0));
	it2--;
	if (row[h].end()==row[h].begin())
		return 1;
	int u=(*it1).ss,w=(*it2).ss;
	bool flag=0;
	for (int i=0;i<30;i++){
		int tmp=0;
		if (it1!=row[h].begin())
			tmp=mem[(*(it1-1)).ss][i];
		if ( (mem[w][i]-tmp)%2){
			if (flag)
				return 0;
			flag=1;
		}
	}
	return 1;
}

int main(){
	speedup;
	scanf("%d %d",&n,&m);
	for (int i=1;i<n;i++){
		int p;
		scanf("%d",&p);
		p--;
		g[i].pb(p);
		g[p].pb(i);
		par[i]=p;
	}
	char str[MN];
	scanf("%s",str);
	for (int i=0;i<n;i++){
		char c=str[i];
		mem[i][c-'a']++;
	}

	dfs(0);
	bfs();
	prep();
	for (int i=0;i<m;i++){
		int v,h;
		scanf("%d %d",&v,&h);
		if (handle(--v,--h))
			cout<<"Yes\n";
		else
			cout<<"No\n";
	}
    return 0;
}


///in his name ..:::12.6.94:::..
#include <iostream>
#include <algorithm>
#include <vector>
#define pb push_back
#define F first
#define S second

using namespace std;
typedef pair<int, int> pii;
const int maxn=5*1000*100+10;
int n,q,dp[maxn][30],str[maxn],fin[maxn],cnt,dis[maxn],INF = 1000*1000*1000;
vector <pii> Hi[2][maxn];
vector <int> ve[maxn];
char ch[maxn];

void make_dp(int u){
    dp[u][ch[u]-'a'] = 1;
    if(!Hi[0][dis[u]].size()) return;
    int id = (Hi[0][dis[u]].back()).S;
    for (int i = 0; i < 26; i++)
        dp[u][i] += dp[id][i];
}

void DFS (int u){
    str[u] = cnt++;
    make_dp(u);
    for (int i = 0; i < ve[u].size(); i++){
        dis[ve[u][i]] = dis[u] + 1;
        DFS (ve[u][i]);
    }
    fin[u] = cnt++;
    Hi[0][dis[u]].pb(pii(str[u],u));
    Hi[1][dis[u]].pb(pii(fin[u],u));
}

bool findi(int u, int h){
    int id = lower_bound (Hi[0][h].begin(), Hi[0][h].end(), pii(str[u],0)) - Hi[0][h].begin();
    int jd = upper_bound (Hi[1][h].begin(), Hi[1][h].end(), pii(fin[u],INF)) - Hi[1][h].begin() - 1;
    int fard = 0;
    for(int i = 0; i < 26 ; i++)
        if( ( dp[Hi[1][h][jd].S][i] - (id <= 0 ? 0 : dp[Hi[0][h][id-1].S][i]) ) % 2) fard++;
    if(fard > 1) return false;
    return true;
}

int main()
{
    cin>> n >> q;
    for(int i = 1; i <= n - 1; i++){
        int par;
        cin >> par; par--;
        ve[par].pb(i);
    }
    for (int i = 0; i < n; i++) cin >> ch[i];
    str[0] = 1; dis[0] = 1;
    DFS (0);
    for(int i = 0; i<q; i++){
        int u,h;
        cin >> u >> h; u--;
        if (dis[u] > h ) {cout << "Yes\n"; continue;}
        if(findi(u, h)) cout << "Yes\n";
        else cout << "No\n" ;
    }
    return 0;
}

/*
6 5
1 1 1 3 3
zacccd
1 1
3 3
4 1
6 1
1 2

*/