#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
using lint = long long;
const int MAXN = 100005;
const int mod1 = 1e9 + 409;
const int mod2 = 1e9 + 433;

int n, q;
int par[17][MAXN], dep[MAXN], sz[MAXN];
int dfn[MAXN], chn[MAXN], piv;
lint phash[105][MAXN];
char pae[MAXN], buf[105];
vector<pi> gph[MAXN];

void dfs(int x){
	sz[x] = 1;
	for(auto &i : gph[x]){
		dep[i.second] = dep[x] + 1;
		par[0][i.second] = x;
		pae[i.second] = i.first;
		gph[i.second].erase(find(gph[i.second].begin(), gph[i.second].end(), pi(i.first, x)));
		dfs(i.second);
		sz[x] += sz[i.second];
	}
}

void hld(int x){
	dfn[x] = ++piv;
	if(gph[x].empty()) return;
	chn[gph[x][0].second] = chn[x];
	hld(gph[x][0].second);
	for(int i=1; i<gph[x].size(); i++){
		chn[gph[x][i].second] = gph[x][i].second;
		hld(gph[x][i].second);
	}
}

int anc(int s, int v){
	for(int i=0; i<17; i++){
		if((v >> i) & 1) s = par[i][s];
	}
	return s;
}

int lca(int s, int e){
	if(dep[s] < dep[e]) swap(s, e);
	s = anc(s, dep[s] - dep[e]);
	for(int i=16; i>=0; i--){
		if(par[i][s] != par[i][e]){
			s = par[i][s];
			e = par[i][e];
		}
	}
	if(s != e) return par[0][s];
	return s;
}

lint getHash(string s){
	lint h1 = 0, h2 = 0;
	for(auto &i : s){
		h1 = ((h1 << 8) | h1) + i;
		h2 = ((h2 << 8) | h2) + i;
		h1 %= mod1;
		h2 %= mod2;
	}
	return h1 * mod2 + h2;
}

int kmp(string s, string t){
	vector<int> fail(s.size() + 1);
	int p = 0;
	for(int i=1; i<s.size(); i++){
		while(p && s[i] != s[p]) p = fail[p];
		if(s[i] == s[p]) p++;
		fail[i + 1] = p;
	}
	p = 0;int ans = 0;
	for(int i=0; i<t.size(); i++){
		while(p && t[i] != s[p]) p = fail[p];
		if(t[i] == s[p]) p++;
		if(p == s.size()){
			ans++;
			p = fail[p];
		}
	}
	return ans;
}

int up(int x, int y, lint h, int l){
	int ans = 0;
	while(x != y){
		if(phash[l][dfn[x]] == h) ans++;
		x = par[0][x];
	}
	return ans;
	while(chn[x] != chn[y]){
		int st = dfn[chn[x]];
		int ed = dfn[x];
		for(int i=st; i<=ed; i++){
			if(phash[l][i] == h) ans++;
		}
		x = par[0][chn[x]];
	}
	int st = dfn[y] + 1;
	int ed = dfn[x];
	for(int i=st; i<=ed; i++){
		if(phash[l][i] == h) ans++;
	}
	return ans;
}

int main(){
	scanf("%d",&n);
	for(int i=0; i<n-1; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		gph[s].emplace_back(*buf, e);
		gph[e].emplace_back(*buf, s);
	}
	dfs(1);
	for(int i=1; i<=n; i++){
		sort(gph[i].begin(), gph[i].end(), [&](const pi &a, const pi &b){
			return sz[a.second] > sz[b.second];
		});
	}
	for(int i=1; i<17; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
		}
	}
	chn[1] = 1;
	hld(1);
	for(int i=2; i<=n; i++){
		int ptr = i;
		lint h1 = 0, h2 = 0;
		for(int j=1; j<=100; j++){
			if(ptr == 1){
				phash[j][dfn[i]] = -1;
				continue;
			}
			h1 = ((h1 << 8) | h1) + pae[ptr];
			h2 = ((h2 << 8) | h2) + pae[ptr];
			h1 %= mod1; h2 %= mod2;
			phash[j][dfn[i]] = h1 * mod2 + h2;
			ptr = par[0][ptr];
		}
	}
	scanf("%d",&q);
	for(int i=0; i<q; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		int l = lca(s, e);
		int ns = anc(s, max(0, dep[s] - dep[l] - 100));
		int ne = anc(e, max(0, dep[e] - dep[l] - 100));
		string str = buf;
		lint fwHash = getHash(str);
		reverse(str.begin(), str.end());
		lint bwHash = getHash(str);
		reverse(str.begin(), str.end());
		int ans = up(s, ns, fwHash, str.size()) + up(e, ne, bwHash, str.size());
		string L, R;
		while(ns != l){
			L.push_back(pae[ns]);
			ns = par[0][ns];
		}
		while(ne != l){
			R.push_back(pae[ne]);
			ne = par[0][ne];
		}
		reverse(R.begin(), R.end());
		ans += kmp(str, L + R);
		printf("%d\n", ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
using lint = long long;
const int MAXN = 100005;
const int mod1 = 1e9 + 409;
const int mod2 = 1e9 + 433;

int n, q;
int par[17][MAXN], dep[MAXN], sz[MAXN];
int dfn[MAXN], chn[MAXN], piv;
lint phash[105][MAXN];
char pae[MAXN], buf[105];
vector<pi> gph[MAXN];

void dfs(int x){
	sz[x] = 1;
	for(auto &i : gph[x]){
		dep[i.second] = dep[x] + 1;
		par[0][i.second] = x;
		pae[i.second] = i.first;
		gph[i.second].erase(find(gph[i.second].begin(), gph[i.second].end(), pi(i.first, x)));
		dfs(i.second);
		sz[x] += sz[i.second];
	}
}

void hld(int x){
	dfn[x] = ++piv;
	if(gph[x].empty()) return;
	chn[gph[x][0].second] = chn[x];
	hld(gph[x][0].second);
	for(int i=1; i<gph[x].size(); i++){
		chn[gph[x][i].second] = gph[x][i].second;
		hld(gph[x][i].second);
	}
}

int anc(int s, int v){
	for(int i=0; i<17; i++){
		if((v >> i) & 1){
			s = par[i][s];
		}
	}
	return s;
}

int lca(int s, int e){
	if(dep[s] < dep[e]) swap(s, e);
	s = anc(s, dep[s] - dep[e]);
	for(int i=16; i>=0; i--){
		if(par[i][s] != par[i][e]){
			s = par[i][s];
			e = par[i][e];
		}
	}
	if(s != e) return par[0][s];
	return s;
}

lint getHash(string s){
	lint h1 = 0, h2 = 0;
	for(auto &i : s){
		h1 = ((h1 << 8) | h1) + i;
		h2 = ((h2 << 8) | h2) + i;
		h1 %= mod1;
		h2 %= mod2;
	}
	return h1 * mod2 + h2;
}

int kmp(string s, string t){
	vector<int> fail(s.size() + 1);
	int p = 0;
	for(int i=1; i<s.size(); i++){
		while(p && s[i] != s[p]) p = fail[p];
		if(s[i] == s[p]) p++;
		fail[i + 1] = p;
	}
	p = 0;int ans = 0;
	for(int i=0; i<t.size(); i++){
		while(p && t[i] != s[p]) p = fail[p];
		if(t[i] == s[p]) p++;
		if(p == s.size()){
			ans++;
			p = fail[p];
		}
	}
	return ans;
}

int up(int x, int y, lint h, int l){
	int ans = 0;
	while(chn[x] != chn[y]){
		int st = dfn[chn[x]];
		int ed = dfn[x];
		for(int i=st; i<=ed; i++){
			if(phash[l][i] == h) ans++;
		}
		x = par[0][chn[x]];
	}
	int st = dfn[y] + 1;
	int ed = dfn[x];
	for(int i=st; i<=ed; i++){
		if(phash[l][i] == h) ans++;
	}
	return ans;
}

int main(){
	scanf("%d",&n);
	for(int i=0; i<n-1; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		gph[s].emplace_back(*buf, e);
		gph[e].emplace_back(*buf, s);
	}
	dfs(1);
	for(int i=1; i<=n; i++){
		sort(gph[i].begin(), gph[i].end(), [&](const pi &a, const pi &b){
			return sz[a.second] > sz[b.second];
		});
	}
	for(int i=1; i<17; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
		}
	}
	chn[1] = 1;
	hld(1);
	for(int i=2; i<=n; i++){
		int ptr = i;
		lint h1 = 0, h2 = 0;
		for(int j=1; j<=100; j++){
			if(ptr == 1){
				phash[j][dfn[i]] = -1;
				continue;
			}
			h1 = ((h1 << 8) | h1) + pae[ptr];
			h2 = ((h2 << 8) | h2) + pae[ptr];
			h1 %= mod1; h2 %= mod2;
			phash[j][dfn[i]] = h1 * mod2 + h2;
			ptr = par[0][ptr];
		}
	}
	scanf("%d",&q);
	for(int i=0; i<q; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		int l = lca(s, e);
		int ns = anc(s, max(0, dep[s] - dep[l] - 100));
		int ne = anc(e, max(0, dep[e] - dep[l] - 100));
		string str = buf;
		lint fwHash = getHash(str);
		reverse(str.begin(), str.end());
		lint bwHash = getHash(str);
		reverse(str.begin(), str.end());
		int ans = up(s, ns, fwHash, str.size()) + up(e, ne, bwHash, str.size());
		string L, R;
		while(ns != l){
			L.push_back(pae[ns]);
			ns = par[0][ns];
		}
		while(ne != l){
			R.push_back(pae[ne]);
			ne = par[0][ne];
		}
		reverse(R.begin(), R.end());
		ans += kmp(str, L + R);
		printf("%d\n", ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
using lint = long long;
const int MAXN = 100005;
const int mod1 = 1e9 + 409;
const int mod2 = 1e9 + 433;

int n, q;
int par[17][MAXN], dep[MAXN], sz[MAXN];
int dfn[MAXN], chn[MAXN], piv;
lint phash[105][MAXN];
char pae[MAXN], buf[105];
vector<pi> gph[MAXN];

void dfs(int x){
	sz[x] = 1;
	for(auto &i : gph[x]){
		dep[i.second] = dep[x] + 1;
		par[0][i.second] = x;
		pae[i.second] = i.first;
		gph[i.second].erase(find(gph[i.second].begin(), gph[i.second].end(), pi(i.first, x)));
		dfs(i.second);
		sz[x] += sz[i.second];
	}
}

void hld(int x){
	if(gph[x].empty()) return;
	dfn[x] = ++piv;
	chn[gph[x][0].second] = chn[x];
	hld(gph[x][0].second);
	for(int i=1; i<gph[x].size(); i++){
		chn[gph[x][i].second] = gph[x][i].second;
		hld(gph[x][i].second);
	}
}

int anc(int s, int v){
	for(int i=0; i<17; i++){
		if((v >> i) & 1){
			s = par[i][s];
		}
	}
	return s;
}

int lca(int s, int e){
	if(dep[s] < dep[e]) swap(s, e);
	s = anc(s, dep[s] - dep[e]);
	for(int i=16; i>=0; i--){
		if(par[i][s] != par[i][e]){
			s = par[i][s];
			e = par[i][e];
		}
	}
	if(s != e) return par[0][s];
	return s;
}

lint getHash(string s){
	lint h1 = 0, h2 = 0;
	for(auto &i : s){
		h1 = ((h1 << 8) | h1) + i;
		h2 = ((h2 << 8) | h2) + i;
		h1 %= mod1;
		h2 %= mod2;
	}
	return h1 * mod2 + h2;
}

int kmp(string s, string t){
	vector<int> fail(s.size() + 1);
	int p = 0;
	for(int i=1; i<s.size(); i++){
		while(p && s[i] != s[p]) p = fail[p];
		if(s[i] == s[p]) p++;
		fail[i + 1] = p;
	}
	p = 0;int ans = 0;
	for(int i=0; i<t.size(); i++){
		while(p && t[i] != s[p]) p = fail[p];
		if(t[i] == s[p]) p++;
		if(p == s.size()){
			ans++;
			p = fail[p];
		}
	}
	return ans;
}

int up(int x, int y, lint h, int l){
	int ans = 0;
	while(chn[x] != chn[y]){
		int st = dfn[chn[x]];
		int ed = dfn[x];
		for(int i=st; i<=ed; i++){
			if(phash[l][i] == h) ans++;
		}
		x = par[0][chn[x]];
	}
	int st = dfn[y] + 1;
	int ed = dfn[x];
	for(int i=st; i<=ed; i++){
		if(phash[l][i] == h) ans++;
	}
	return ans;
}

int main(){
	scanf("%d",&n);
	for(int i=0; i<n-1; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		gph[s].emplace_back(*buf, e);
		gph[e].emplace_back(*buf, s);
	}
	dfs(1);
	for(int i=1; i<=n; i++){
		sort(gph[i].begin(), gph[i].end(), [&](const pi &a, const pi &b){
			return sz[a.second] > sz[b.second];
		});
	}
	for(int i=1; i<17; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
		}
	}
	chn[1] = 1;
	hld(1);
	for(int i=2; i<=n; i++){
		int ptr = i;
		lint h1 = 0, h2 = 0;
		for(int j=1; j<=100; j++){
			if(ptr == 1){
				phash[j][dfn[i]] = -1;
				continue;
			}
			h1 = ((h1 << 8) | h1) + pae[ptr];
			h2 = ((h2 << 8) | h2) + pae[ptr];
			h1 %= mod1; h2 %= mod2;
			phash[j][dfn[i]] = h1 * mod2 + h2;
			ptr = par[0][ptr];
		}
	}
	scanf("%d",&q);
	for(int i=0; i<q; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		int l = lca(s, e);
		int ns = anc(s, max(0, dep[s] - dep[l] - 100));
		int ne = anc(e, max(0, dep[e] - dep[l] - 100));
		string str = buf;
		lint fwHash = getHash(str);
		reverse(str.begin(), str.end());
		lint bwHash = getHash(str);
		reverse(str.begin(), str.end());
		lint ans = up(s, ns, fwHash, str.size()) + up(e, ne, bwHash, str.size());
		string L, R;
		while(ns != l){
			L.push_back(pae[ns]);
			ns = par[0][ns];
		}
		while(ne != l){
			R.push_back(pae[ne]);
			ne = par[0][ne];
		}
		reverse(R.begin(), R.end());
		ans += kmp(str, L + R);
		printf("%lld\n", ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
using lint = long long;
const int MAXN = 100005;
const int mod1 = 1e9 + 409;
const int mod2 = 1e9 + 433;

int n, q;
int par[17][MAXN], dep[MAXN], sz[MAXN];
int dfn[MAXN], chn[MAXN], piv;
lint phash[105][MAXN];
char pae[MAXN], buf[105];
vector<pi> gph[MAXN];

void dfs(int x){
	sz[x] = 1;
	for(auto &i : gph[x]){
		dep[i.second] = dep[x] + 1;
		par[0][i.second] = x;
		pae[i.second] = i.first;
		gph[i.second].erase(find(gph[i.second].begin(), gph[i.second].end(), pi(i.first, x)));
		dfs(i.second);
		sz[x] += sz[i.second];
	}
}

void hld(int x){
	dfn[x] = ++piv;
	if(gph[x].empty()) return;
	chn[gph[x][0].second] = chn[x];
	hld(gph[x][0].second);
	for(int i=1; i<gph[x].size(); i++){
		chn[gph[x][i].second] = gph[x][i].second;
		hld(gph[x][i].second);
	}
}

int anc(int s, int v){
	for(int i=0; i<17; i++){
		if((v >> i) & 1){
			s = par[i][s];
		}
	}
	return s;
}

int lca(int s, int e){
	if(dep[s] < dep[e]) swap(s, e);
	s = anc(s, dep[s] - dep[e]);
	for(int i=16; i>=0; i--){
		if(par[i][s] != par[i][e]){
			s = par[i][s];
			e = par[i][e];
		}
	}
	if(s != e) return par[0][s];
	return s;
}

lint getHash(string s){
	lint h1 = 0, h2 = 0;
	for(auto &i : s){
		h1 = ((h1 << 8) | h1) + i;
		h2 = ((h2 << 8) | h2) + i;
		h1 %= mod1;
		h2 %= mod2;
	}
	return h1 * mod2 + h2;
}

int kmp(string s, string t){
	vector<int> fail(s.size() + 1);
	int p = 0;
	for(int i=1; i<s.size(); i++){
		while(p && s[i] != s[p]) p = fail[p];
		if(s[i] == s[p]) p++;
		fail[i + 1] = p;
	}
	p = 0;int ans = 0;
	for(int i=0; i<t.size(); i++){
		while(p && t[i] != s[p]) p = fail[p];
		if(t[i] == s[p]) p++;
		if(p == s.size()){
			ans++;
			p = fail[p];
		}
	}
	return ans;
}

int up(int x, int y, lint h, int l){
	int ans = 0;
	while(chn[x] != chn[y]){
		int st = dfn[chn[x]];
		int ed = dfn[x];
		for(int i=st; i<=ed; i++){
			if(phash[l][i] == h) ans++;
		}
		x = par[0][chn[x]];
	}
	int st = dfn[y] + 1;
	int ed = dfn[x];
	for(int i=st; i<=ed; i++){
		if(phash[l][i] == h) ans++;
	}
	return ans;
}

int main(){
	scanf("%d",&n);
	for(int i=0; i<n-1; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		gph[s].emplace_back(*buf, e);
		gph[e].emplace_back(*buf, s);
	}
	dfs(1);
	for(int i=1; i<=n; i++){
		sort(gph[i].begin(), gph[i].end(), [&](const pi &a, const pi &b){
			return sz[a.second] > sz[b.second];
		});
	}
	for(int i=1; i<17; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
		}
	}
	chn[1] = 1;
	hld(1);
	for(int i=2; i<=n; i++){
		int ptr = i;
		lint h1 = 0, h2 = 0;
		for(int j=1; j<=100; j++){
			if(ptr == 1){
				phash[j][dfn[i]] = -1;
				continue;
			}
			h1 = ((h1 << 8) | h1) + pae[ptr];
			h2 = ((h2 << 8) | h2) + pae[ptr];
			h1 %= mod1; h2 %= mod2;
			phash[j][dfn[i]] = h1 * mod2 + h2;
			ptr = par[0][ptr];
		}
	}
	scanf("%d",&q);
	for(int i=0; i<q; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		int l = lca(s, e);
		int ns = s; // anc(s, max(0, dep[s] - dep[l] - 100));
		int ne = e; // anc(e, max(0, dep[e] - dep[l] - 100));
		string str = buf;
		lint fwHash = getHash(str);
		reverse(str.begin(), str.end());
		lint bwHash = getHash(str);
		reverse(str.begin(), str.end());
		int ans = up(s, ns, fwHash, str.size()) + up(e, ne, bwHash, str.size());
		string L, R;
		while(ns != l){
			L.push_back(pae[ns]);
			ns = par[0][ns];
		}
		while(ne != l){
			R.push_back(pae[ne]);
			ne = par[0][ne];
		}
		reverse(R.begin(), R.end());
		ans += kmp(str, L + R);
		printf("%d\n", ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
using lint = long long;
const int MAXN = 100005;
const int mod1 = 1e9 + 409;
const int mod2 = 1e9 + 433;

int n, q;
int par[17][MAXN], dep[MAXN], sz[MAXN];
int dfn[MAXN], chn[MAXN], piv;
lint phash[105][MAXN];
char pae[MAXN], buf[105];
vector<pi> gph[MAXN];

void dfs(int x){
	sz[x] = 1;
	for(auto &i : gph[x]){
		dep[i.second] = dep[x] + 1;
		par[0][i.second] = x;
		pae[i.second] = i.first;
		gph[i.second].erase(find(gph[i.second].begin(), gph[i.second].end(), pi(i.first, x)));
		dfs(i.second);
		sz[x] += sz[i.second];
	}
}

void hld(int x){
	dfn[x] = ++piv;
	if(gph[x].empty()) return;
	chn[gph[x][0].second] = chn[x];
	hld(gph[x][0].second);
	for(int i=1; i<gph[x].size(); i++){
		chn[gph[x][i].second] = gph[x][i].second;
		hld(gph[x][i].second);
	}
}

int anc(int s, int v){
	for(int i=0; i<17; i++){
		if((v >> i) & 1) s = par[i][s];
	}
	return s;
}

int lca(int s, int e){
	if(dep[s] < dep[e]) swap(s, e);
	s = anc(s, dep[s] - dep[e]);
	for(int i=16; i>=0; i--){
		if(par[i][s] != par[i][e]){
			s = par[i][s];
			e = par[i][e];
		}
	}
	if(s != e) return par[0][s];
	return s;
}

lint getHash(string s){
	lint h1 = 0, h2 = 0;
	for(auto &i : s){
		h1 = ((h1 << 8) + h1) + i;
		h2 = ((h2 << 8) + h2) + i;
		h1 %= mod1;
		h2 %= mod2;
	}
	return h1 * mod2 + h2;
}

int kmp(string s, string t){
	vector<int> fail(s.size() + 1);
	int p = 0;
	for(int i=1; i<s.size(); i++){
		while(p && s[i] != s[p]) p = fail[p];
		if(s[i] == s[p]) p++;
		fail[i + 1] = p;
	}
	p = 0;
	int ans = 0;
	for(int i=0; i<t.size(); i++){
		while(p && t[i] != s[p]) p = fail[p];
		if(t[i] == s[p]) p++;
		if(p == s.size()){
			ans++;
			p = fail[p];
		}
	}
	return ans;
}

int up(int x, int y, lint h, int l){
	int ans = 0;
	while(chn[x] != chn[y]){
		int st = dfn[chn[x]];
		int ed = dfn[x];
		for(int i=st; i<=ed; i++){
			if(phash[l][i] == h) ans++;
		}
		x = par[0][chn[x]];
	}
	int st = dfn[y] + 1;
	int ed = dfn[x];
	for(int i=st; i<=ed; i++){
		if(phash[l][i] == h) ans++;
	}
	return ans;
}

int main(){
	scanf("%d",&n);
	for(int i=0; i<n-1; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		gph[s].emplace_back(*buf, e);
		gph[e].emplace_back(*buf, s);
	}
	dfs(1);
	for(int i=1; i<=n; i++){
		sort(gph[i].begin(), gph[i].end(), [&](const pi &a, const pi &b){
			return sz[a.second] > sz[b.second];
		});
	}
	for(int i=1; i<17; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
		}
	}
	chn[1] = 1;
	hld(1);
	for(int i=2; i<=n; i++){
		int ptr = i;
		lint h1 = 0, h2 = 0;
		for(int j=1; j<=100; j++){
			if(ptr == 1){
				phash[j][dfn[i]] = -1;
				continue;
			}
			h1 = ((h1 << 8) + h1) + pae[ptr];
			h2 = ((h2 << 8) + h2) + pae[ptr];
			h1 %= mod1; h2 %= mod2;
			phash[j][dfn[i]] = h1 * mod2 + h2;
			ptr = par[0][ptr];
		}
	}
	scanf("%d",&q);
	for(int i=0; i<q; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		int l = lca(s, e);
		int ns = anc(s, max(0, dep[s] - dep[l] - 100));
		int ne = anc(e, max(0, dep[e] - dep[l] - 100));
		string str = buf;
		lint fwHash = getHash(str);
		reverse(str.begin(), str.end());
		lint bwHash = getHash(str);
		reverse(str.begin(), str.end());
		int ans = up(s, ns, fwHash, str.size()) + up(e, ne, bwHash, str.size());
		string L, R;
		while(ns != l){
			L.push_back(pae[ns]);
			ns = par[0][ns];
		}
		while(ne != l){
			R.push_back(pae[ne]);
			ne = par[0][ne];
		}
		reverse(R.begin(), R.end());
		ans += kmp(str, L + R);
		printf("%d\n", ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
using lint = long long;
typedef pair<lint, int> pii;
const int MAXN = 100005;
const int mod1 = 1e9 + 409;
const int mod2 = 1e9 + 433;

pii sor[10000005];
int n, q, Z;
int par[17][MAXN], dep[MAXN], sz[MAXN];
int dfn[MAXN], chn[MAXN], piv;
char pae[MAXN], buf[105];
vector<pi> gph[MAXN];

void dfs(int x){
	sz[x] = 1;
	for(auto &i : gph[x]){
		dep[i.second] = dep[x] + 1;
		par[0][i.second] = x;
		pae[i.second] = i.first;
		gph[i.second].erase(find(gph[i.second].begin(), gph[i.second].end(), pi(i.first, x)));
		dfs(i.second);
		sz[x] += sz[i.second];
	}
}

void hld(int x){
	dfn[x] = ++piv;
	if(gph[x].empty()) return;
	chn[gph[x][0].second] = chn[x];
	hld(gph[x][0].second);
	for(int i=1; i<gph[x].size(); i++){
		chn[gph[x][i].second] = gph[x][i].second;
		hld(gph[x][i].second);
	}
}

int anc(int s, int v){
	for(int i=0; i<17; i++){
		if((v >> i) & 1) s = par[i][s];
	}
	return s;
}

int lca(int s, int e){
	if(dep[s] < dep[e]) swap(s, e);
	s = anc(s, dep[s] - dep[e]);
	for(int i=16; i>=0; i--){
		if(par[i][s] != par[i][e]){
			s = par[i][s];
			e = par[i][e];
		}
	}
	if(s != e) return par[0][s];
	return s;
}

lint getHash(string s){
	lint h1 = 0, h2 = 0;
	for(auto &i : s){
		h1 = ((h1 << 8) + h1) + i;
		h2 = ((h2 << 8) + h2) + i;
		h1 %= mod1;
		h2 %= mod2;
	}
	return h1 * mod2 + h2;
}

int kmp(string s, string t){
	vector<int> fail(s.size() + 1);
	int p = 0;
	for(int i=1; i<s.size(); i++){
		while(p && s[i] != s[p]) p = fail[p];
		if(s[i] == s[p]) p++;
		fail[i + 1] = p;
	}
	p = 0;
	int ans = 0;
	for(int i=0; i<t.size(); i++){
		while(p && t[i] != s[p]) p = fail[p];
		if(t[i] == s[p]) p++;
		if(p == s.size()){
			ans++;
			p = fail[p];
		}
	}
	return ans;
}

int up(int x, int y, lint h, int l){
	int ans = 0;
	while(chn[x] != chn[y]){
		int st = dfn[chn[x]];
		int ed = dfn[x];
		ans += upper_bound(sor, sor + Z, pii(h, ed)) - lower_bound(sor, sor + Z, pii(h, st));
		x = par[0][chn[x]];
	}
	int st = dfn[y] + 1;
	int ed = dfn[x];
	ans += upper_bound(sor, sor + Z, pii(h, ed)) - lower_bound(sor, sor + Z, pii(h, st));
	return ans;
}

int main(){
	scanf("%d",&n);
	for(int i=0; i<n-1; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		gph[s].emplace_back(*buf, e);
		gph[e].emplace_back(*buf, s);
	}
	dfs(1);
	for(int i=1; i<=n; i++){
		sort(gph[i].begin(), gph[i].end(), [&](const pi &a, const pi &b){
			return sz[a.second] > sz[b.second];
		});
	}
	for(int i=1; i<17; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
		}
	}
	chn[1] = 1;
	hld(1);
	for(int i=2; i<=n; i++){
		int ptr = i;
		lint h1 = 0, h2 = 0;
		for(int j=1; j<=100; j++){
			if(ptr == 1){
				continue;
			}
			h1 = ((h1 << 8) + h1) + pae[ptr];
			h2 = ((h2 << 8) + h2) + pae[ptr];
			h1 %= mod1; h2 %= mod2;
			sor[Z++] = pii(h1 * mod2 + h2, dfn[i]);
			ptr = par[0][ptr];
		}
	}
	sort(sor, sor + Z);
	scanf("%d",&q);
	for(int i=0; i<q; i++){
		int s, e;
		scanf("%d %d %s",&s,&e,buf);
		int l = lca(s, e);
		int ns = anc(s, max(0, dep[s] - dep[l] - 100));
		int ne = anc(e, max(0, dep[e] - dep[l] - 100));
		string str = buf;
		lint fwHash = getHash(str);
		reverse(str.begin(), str.end());
		lint bwHash = getHash(str);
		reverse(str.begin(), str.end());
		int ans = up(s, ns, fwHash, str.size()) + up(e, ne, bwHash, str.size());
		string L, R;
		while(ns != l){
			L.push_back(pae[ns]);
			ns = par[0][ns];
		}
		while(ne != l){
			R.push_back(pae[ne]);
			ne = par[0][ne];
		}
		reverse(R.begin(), R.end());
		ans += kmp(str, L + R);
		printf("%d\n", ans);
	}
}


// Created by Nikolay Budin

#ifdef DEBUG
#  define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
#ifndef LOCAL
#  define cerr __get_ce
#endif

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;

using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
	mt19937 tw(9450189);
#else
	mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

const int MOD = 1'000'000'007;

void add(int& a, int b) {
	a += b;
	if (a >= MOD) {
		a -= MOD;
	}
}

int sum(int a, int b) {
	add(a, b);
	return a;
}

int mult(int a, int b) {
	return (ll) a * b % MOD;
}

int mpow(int a, int b) {
	int ret = 1;
	while (b) {
		if (b & 1) {
			ret = mult(ret, a);
		}
		a = mult(a, a);
		b /= 2;
	}
	return ret;
}

int inv(int num) {
	return mpow(num, MOD - 2);
}

const int P = 239;
const int INV_P = inv(P);

const int MAXL = 105;

int calc_hash(string const& s) {
	int h = 0;
	for (char c : s) {
		h = sum(mult(h, P), c);
	}
	return h;
}

int calc_inv_hash(string s) {
	reverse(s.begin(), s.end());
	return calc_hash(s);
}

void solve() {
	int n;
	cin >> n;
	vector<vector<pair<int, char>>> graph(n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		char c;
		cin >> a >> b >> c;
		--a; --b;
		graph[a].push_back({b, c});
		graph[b].push_back({a, c});
	}

	vector<int> pp = {1};
	vector<int> inv_pp = {1};
	for (int i = 0; i < n + 10; ++i) {
		pp.push_back(mult(pp.back(), P));
		inv_pp.push_back(mult(inv_pp.back(), INV_P));
	}

	int bp = 1;
	while (1 << bp < n) {
		++bp;
	}

	vector<int> depth(n);
	vector<vector<int>> binup(bp, vector<int>(n));
	vector<int> pref_h(n), pref_rh(n);

	function<void(int, int, int)> dfs = [&](int v, int p, int d) {
		depth[v] = d;
		binup[0][v] = p;
		for (int i = 1; i < bp; ++i) {
			binup[i][v] = binup[i - 1][binup[i - 1][v]];
		}
		for (auto to : graph[v]) {
			if (to.ff != p) {
				pref_h[to.ff] = sum(pref_h[v], mult(to.ss, pp[d]));
				pref_rh[to.ff] = sum(mult(pref_rh[v], P), to.ss);
				dfs(to.ff, v, d + 1);
			}
		}
	};

	dfs(0, 0, 0);

	auto get_lca = [&](int a, int b) {
		if (depth[a] > depth[b]) {
			swap(a, b);
		}
		for (int i = bp - 1; i >= 0; --i) {
			if (depth[a] <= depth[b] - (1 << i)) {
				b = binup[i][b];
			}
		}
		if (a == b) {
			return a;
		}
		for (int i = bp - 1; i >= 0; --i) {
			if (binup[i][a] != binup[i][b]) {
				a = binup[i][a];
				b = binup[i][b];
			}
		}
		return binup[0][a];
	};

	auto jump_for = [&](int v, int d) {
		for (int i = bp - 1; i >= 0; --i) {
			if (d >= (1 << i)) {
				d -= 1 << i;
				v = binup[i][v];
			}
		}
		return v;
	};

	vector<vector<tuple<int, int, int, int>>> here(n);

	int q;
	cin >> q;
	vector<int> ans(q);
	for (int i = 0; i < q; ++i) {
		int a, b;
		string s;
		cin >> a >> b >> s;
		int sh = calc_hash(s);
		--a; --b;
		int lca = get_lca(a, b);

		if (depth[a] + depth[b] - 2 * depth[lca] < szof(s)) {
			continue;
		}
		int from = a;
		if (depth[a] - depth[lca] >= szof(s)) {
			from = jump_for(a, depth[a] - depth[lca] - szof(s) + 1);
			here[a].push_back({szof(s), sh, i, 1});
			here[from].push_back({szof(s), sh, i, -1});
		}

		int to = b;
		if (depth[b] - depth[lca] >= szof(s)) {
			int rsh = calc_inv_hash(s);
			to = jump_for(b, depth[b] - depth[lca] - szof(s) + 1);
			here[b].push_back({szof(s), rsh, i, 1});
			here[to].push_back({szof(s), rsh, i, -1});
		}

		// cerr << from + 1 << " " << to + 1 << endl;

		vector<int> curh;
		int v = from;
		for (int j = depth[from] - depth[lca]; szof(s) - j <= depth[to] - depth[lca]; --j) {
			int h = mult(sum(pref_h[v], MOD - pref_h[lca]), inv_pp[depth[lca]]);
			curh.push_back(h);
			v = binup[0][v];
		}
		reverse(curh.begin(), curh.end());
		v = to;
		int cnt = 0;
		for (int j = depth[to] - depth[lca]; szof(s) - j <= depth[from] - depth[lca]; --j) {
			int h = sum(pref_rh[v], MOD - mult(pref_rh[lca], pp[depth[v] - depth[lca]]));
			int th = sum(mult(curh[cnt++], pp[depth[v] - depth[lca]]), h);
			if (th == sh) {
				++ans[i];
			}
			v = binup[0][v];
		}
	}

	vector<unordered_map<int, int>> cnt(MAXL);
	vector<int> path;
	function<void(int, int)> dfs_calc = [&](int v, int p) {
		path.push_back(v);

		for (int i = 1; i < MAXL && i < szof(path); ++i) {
			int u = path[szof(path) - 1 - i];
			int h = mult(sum(pref_h[v], MOD - pref_h[u]), inv_pp[depth[u]]);
			cnt[i][h]++;
		}
		// cerr << "v: " << v + 1 << endl;
		for (auto [l, h, ind, sign] : here[v]) {
			// cerr << l << " " << h << " " << ind << " " << sign << endl;
			if (cnt[l].count(h)) {
				ans[ind] += sign * cnt[l][h];
				// cerr << sign * cnt[l][h] << endl;
			}
		}

		for (auto to : graph[v]) {
			if (to.ff != p) {
				dfs_calc(to.ff, v);
			}
		}

		for (int i = 1; i < MAXL && i < szof(path); ++i) {
			int u = path[szof(path) - 1 - i];
			int h = mult(sum(pref_h[v], MOD - pref_h[u]), inv_pp[depth[u]]);
			cnt[i][h]--;
		}

		path.pop_back();
	};

	dfs_calc(0, 0);

	for (int num : ans) {
		cout << num << "\n";
	}
}

int main() {
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed;
#endif
	cout << setprecision(15) << fixed;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int test_count = 1;
	// cin >> test_count;
	for (int test = 1; test <= test_count; ++test) {
		solve();
	}

#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}


// Created by Nikolay Budin

#ifdef DEBUG
#  define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
#ifndef LOCAL
#  define cerr __get_ce
#endif

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;

using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
	mt19937 tw(9450189);
#else
	mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

const int MOD = 1'000'000'007;

void add(int& a, int b) {
	a += b;
	if (a >= MOD) {
		a -= MOD;
	}
}

int sum(int a, int b) {
	add(a, b);
	return a;
}

int mult(int a, int b) {
	return (ll) a * b % MOD;
}

int mpow(int a, int b) {
	int ret = 1;
	while (b) {
		if (b & 1) {
			ret = mult(ret, a);
		}
		a = mult(a, a);
		b /= 2;
	}
	return ret;
}

int inv(int num) {
	return mpow(num, MOD - 2);
}

const int P = 239;
const int INV_P = inv(P);

const int MAXL = 105;

int calc_hash(string const& s) {
	int h = 0;
	for (char c : s) {
		h = sum(mult(h, P), c);
	}
	return h;
}

int calc_inv_hash(string s) {
	reverse(s.begin(), s.end());
	return calc_hash(s);
}

void solve() {
	int n;
	cin >> n;
	vector<vector<pair<int, char>>> graph(n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		char c;
		cin >> a >> b >> c;
		--a; --b;
		graph[a].push_back({b, c});
		graph[b].push_back({a, c});
	}

	vector<int> pp = {1};
	vector<int> inv_pp = {1};
	for (int i = 0; i < n + 10; ++i) {
		pp.push_back(mult(pp.back(), P));
		inv_pp.push_back(mult(inv_pp.back(), INV_P));
	}

	int bp = 1;
	while (1 << bp < n) {
		++bp;
	}

	vector<int> depth(n);
	vector<vector<int>> binup(bp, vector<int>(n));
	vector<int> pref_h(n), pref_rh(n);

	function<void(int, int, int)> dfs = [&](int v, int p, int d) {
		depth[v] = d;
		binup[0][v] = p;
		for (int i = 1; i < bp; ++i) {
			binup[i][v] = binup[i - 1][binup[i - 1][v]];
		}
		for (auto to : graph[v]) {
			if (to.ff != p) {
				pref_h[to.ff] = sum(pref_h[v], mult(to.ss, pp[d]));
				pref_rh[to.ff] = sum(mult(pref_rh[v], P), to.ss);
				dfs(to.ff, v, d + 1);
			}
		}
	};

	dfs(0, 0, 0);

	auto get_lca = [&](int a, int b) {
		if (depth[a] > depth[b]) {
			swap(a, b);
		}
		for (int i = bp - 1; i >= 0; --i) {
			if (depth[a] <= depth[b] - (1 << i)) {
				b = binup[i][b];
			}
		}
		if (a == b) {
			return a;
		}
		for (int i = bp - 1; i >= 0; --i) {
			if (binup[i][a] != binup[i][b]) {
				a = binup[i][a];
				b = binup[i][b];
			}
		}
		return binup[0][a];
	};

	auto jump_for = [&](int v, int d) {
		for (int i = bp - 1; i >= 0; --i) {
			if (d >= (1 << i)) {
				d -= 1 << i;
				v = binup[i][v];
			}
		}
		return v;
	};

	vector<vector<vector<tuple<int, int, int>>>> here(MAXL, vector<vector<tuple<int, int, int>>>(n));

	int q;
	cin >> q;
	vector<int> ans(q);
	for (int i = 0; i < q; ++i) {
		int a, b;
		string s;
		cin >> a >> b >> s;
		int sh = calc_hash(s);
		--a; --b;
		int lca = get_lca(a, b);

		if (depth[a] + depth[b] - 2 * depth[lca] < szof(s)) {
			continue;
		}
		int from = a;
		if (depth[a] - depth[lca] >= szof(s)) {
			from = jump_for(a, depth[a] - depth[lca] - szof(s) + 1);
			here[szof(s)][a].push_back({sh, i, 1});
			here[szof(s)][from].push_back({sh, i, -1});
		}

		int to = b;
		if (depth[b] - depth[lca] >= szof(s)) {
			int rsh = calc_inv_hash(s);
			to = jump_for(b, depth[b] - depth[lca] - szof(s) + 1);
			here[szof(s)][b].push_back({rsh, i, 1});
			here[szof(s)][to].push_back({rsh, i, -1});
		}

		// cerr << from + 1 << " " << to + 1 << endl;

		vector<int> curh;
		int v = from;
		for (int j = depth[from] - depth[lca]; szof(s) - j <= depth[to] - depth[lca]; --j) {
			int h = mult(sum(pref_h[v], MOD - pref_h[lca]), inv_pp[depth[lca]]);
			curh.push_back(h);
			v = binup[0][v];
		}
		reverse(curh.begin(), curh.end());
		v = to;
		int cnt = 0;
		for (int j = depth[to] - depth[lca]; szof(s) - j <= depth[from] - depth[lca]; --j) {
			int h = sum(pref_rh[v], MOD - mult(pref_rh[lca], pp[depth[v] - depth[lca]]));
			int th = sum(mult(curh[cnt++], pp[depth[v] - depth[lca]]), h);
			if (th == sh) {
				++ans[i];
			}
			v = binup[0][v];
		}
	}

	for (int l = 1; l < MAXL; ++l) {
		unordered_map<int, int> cnt;
		vector<int> path;
		function<void(int, int)> dfs_calc = [&](int v, int p) {
			path.push_back(v);

			if (szof(path) > l) {
				int u = path[szof(path) - 1 - l];
				int h = mult(sum(pref_h[v], MOD - pref_h[u]), inv_pp[depth[u]]);
				cnt[h]++;
			}
			// cerr << "v: " << v + 1 << endl;
			for (auto [h, ind, sign] : here[l][v]) {
				// cerr << l << " " << h << " " << ind << " " << sign << endl;
				if (cnt.count(h)) {
					ans[ind] += sign * cnt[h];
					// cerr << sign * cnt[l][h] << endl;
				}
			}

			for (auto to : graph[v]) {
				if (to.ff != p) {
					dfs_calc(to.ff, v);
				}
			}

			if (szof(path) > l) {
				int u = path[szof(path) - 1 - l];
				int h = mult(sum(pref_h[v], MOD - pref_h[u]), inv_pp[depth[u]]);
				cnt[h]--;
				if (cnt[h] == 0) {
					cnt.erase(h);
				}
			}

			path.pop_back();
		};

		dfs_calc(0, 0);
	}

	for (int num : ans) {
		cout << num << "\n";
	}
}

int main() {
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed;
#endif
	cout << setprecision(15) << fixed;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int test_count = 1;
	// cin >> test_count;
	for (int test = 1; test <= test_count; ++test) {
		solve();
	}

#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}


// Created by Nikolay Budin

#ifdef DEBUG
#  define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
#ifndef LOCAL
#  define cerr __get_ce
#endif

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;

using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
	mt19937 tw(9450189);
#else
	mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

const ull MOD = 1'000'000'000'000'000'009;

void add(ull& a, ull b) {
	a += b;
	if (a >= MOD) {
		a -= MOD;
	}
}

ull sum(ull a, ull b) {
	add(a, b);
	return a;
}

ull mult(ull a, ull b) {
	ll k = (ll)((long double)a * b / MOD);
	ll r = a * b - MOD * k;
	if (r < 0)
		r += MOD;
	if (r >= MOD)
		r -= MOD;
	return r;
}

ull mpow(ull a, ull b) {
	ull ret = 1;
	while (b) {
		if (b & 1) {
			ret = mult(ret, a);
		}
		a = mult(a, a);
		b /= 2;
	}
	return ret;
}

ull inv(ull num) {
	return mpow(num, MOD - 2);
}

const ull P = 239;
const ull INV_P = inv(P);

const int MAXL = 105;

ull calc_hash(string const& s) {
	ull h = 0;
	for (char c : s) {
		h = sum(mult(h, P), c);
	}
	return h;
}

ull calc_inv_hash(string s) {
	reverse(s.begin(), s.end());
	return calc_hash(s);
}

void solve() {
	int n;
	cin >> n;
	vector<vector<pair<int, char>>> graph(n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		char c;
		cin >> a >> b >> c;
		--a; --b;
		graph[a].push_back({b, c});
		graph[b].push_back({a, c});
	}

	vector<ull> pp = {1};
	vector<ull> inv_pp = {1};
	for (int i = 0; i < n + 10; ++i) {
		pp.push_back(mult(pp.back(), P));
		inv_pp.push_back(mult(inv_pp.back(), INV_P));
	}

	int bp = 1;
	while (1 << bp < n) {
		++bp;
	}

	vector<int> depth(n);
	vector<vector<int>> binup(bp, vector<int>(n));
	vector<ull> pref_h(n), pref_rh(n);

	function<void(int, int, int)> dfs = [&](int v, int p, int d) {
		depth[v] = d;
		binup[0][v] = p;
		for (int i = 1; i < bp; ++i) {
			binup[i][v] = binup[i - 1][binup[i - 1][v]];
		}
		for (auto to : graph[v]) {
			if (to.ff != p) {
				pref_h[to.ff] = sum(pref_h[v], mult(to.ss, pp[d]));
				pref_rh[to.ff] = sum(mult(pref_rh[v], P), to.ss);
				dfs(to.ff, v, d + 1);
			}
		}
	};

	dfs(0, 0, 0);

	auto get_lca = [&](int a, int b) {
		if (depth[a] > depth[b]) {
			swap(a, b);
		}
		for (int i = bp - 1; i >= 0; --i) {
			if (depth[a] <= depth[b] - (1 << i)) {
				b = binup[i][b];
			}
		}
		if (a == b) {
			return a;
		}
		for (int i = bp - 1; i >= 0; --i) {
			if (binup[i][a] != binup[i][b]) {
				a = binup[i][a];
				b = binup[i][b];
			}
		}
		return binup[0][a];
	};

	auto jump_for = [&](int v, int d) {
		for (int i = bp - 1; i >= 0; --i) {
			if (d >= (1 << i)) {
				d -= 1 << i;
				v = binup[i][v];
			}
		}
		return v;
	};

	vector<vector<vector<tuple<ull, int, int>>>> here(MAXL, vector<vector<tuple<ull, int, int>>>(n));

	int q;
	cin >> q;
	vector<int> ans(q);
	for (int i = 0; i < q; ++i) {
		int a, b;
		string s;
		cin >> a >> b >> s;
		ull sh = calc_hash(s);
		--a; --b;
		int lca = get_lca(a, b);

		if (depth[a] + depth[b] - 2 * depth[lca] < szof(s)) {
			continue;
		}
		int from = a;
		if (depth[a] - depth[lca] >= szof(s)) {
			from = jump_for(a, depth[a] - depth[lca] - szof(s) + 1);
			here[szof(s)][a].push_back({sh, i, 1});
			here[szof(s)][from].push_back({sh, i, -1});
		}

		int to = b;
		if (depth[b] - depth[lca] >= szof(s)) {
			ull rsh = calc_inv_hash(s);
			to = jump_for(b, depth[b] - depth[lca] - szof(s) + 1);
			here[szof(s)][b].push_back({rsh, i, 1});
			here[szof(s)][to].push_back({rsh, i, -1});
		}

		// cerr << from + 1 << " " << to + 1 << endl;

		vector<ull> curh;
		int v = from;
		for (int j = depth[from] - depth[lca]; szof(s) - j <= depth[to] - depth[lca]; --j) {
			ull h = mult(sum(pref_h[v], MOD - pref_h[lca]), inv_pp[depth[lca]]);
			curh.push_back(h);
			v = binup[0][v];
		}
		reverse(curh.begin(), curh.end());
		v = to;
		int cnt = 0;
		for (int j = depth[to] - depth[lca]; szof(s) - j <= depth[from] - depth[lca]; --j) {
			ull h = sum(pref_rh[v], MOD - mult(pref_rh[lca], pp[depth[v] - depth[lca]]));
			ull th = sum(mult(curh[cnt++], pp[depth[v] - depth[lca]]), h);
			if (th == sh) {
				++ans[i];
			}
			v = binup[0][v];
		}
	}

	for (int l = 1; l < MAXL; ++l) {
		unordered_map<ull, int> cnt;
		vector<int> path;
		function<void(int, int)> dfs_calc = [&](int v, int p) {
			path.push_back(v);

			if (szof(path) > l) {
				int u = path[szof(path) - 1 - l];
				ull h = mult(sum(pref_h[v], MOD - pref_h[u]), inv_pp[depth[u]]);
				cnt[h]++;
			}
			// cerr << "v: " << v + 1 << endl;
			for (auto [h, ind, sign] : here[l][v]) {
				// cerr << l << " " << h << " " << ind << " " << sign << endl;
				if (cnt.count(h)) {
					ans[ind] += sign * cnt[h];
					// cerr << sign * cnt[l][h] << endl;
				}
			}

			for (auto to : graph[v]) {
				if (to.ff != p) {
					dfs_calc(to.ff, v);
				}
			}

			if (szof(path) > l) {
				int u = path[szof(path) - 1 - l];
				ull h = mult(sum(pref_h[v], MOD - pref_h[u]), inv_pp[depth[u]]);
				cnt[h]--;
				if (cnt[h] == 0) {
					cnt.erase(h);
				}
			}

			path.pop_back();
		};

		dfs_calc(0, 0);
	}

	for (int num : ans) {
		cout << num << "\n";
	}
}

int main() {
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed;
#endif
	cout << setprecision(15) << fixed;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int test_count = 1;
	// cin >> test_count;
	for (int test = 1; test <= test_count; ++test) {
		solve();
	}

#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}


// Created by Nikolay Budin

#ifdef DEBUG
#  define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define szof(x) ((int)x.size())
#ifndef LOCAL
#  define cerr __get_ce
#endif

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef unsigned long long ull;

using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

int const INF = (int)1e9 + 1e3;
ll const INFL = (ll)1e18 + 1e6;
#ifdef LOCAL
	mt19937 tw(9450189);
#else
	mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
uniform_int_distribution<ll> ll_distr;
ll rnd(ll a, ll b) { return ll_distr(tw) % (b - a + 1) + a; }

const int MOD = 1'000'000'007;

void add(int& a, int b) {
	a += b;
	if (a >= MOD) {
		a -= MOD;
	}
}

int sum(int a, int b) {
	add(a, b);
	return a;
}

int mult(int a, int b) {
	return (ll) a * b % MOD;
}

int mpow(int a, int b) {
	int ret = 1;
	while (b) {
		if (b & 1) {
			ret = mult(ret, a);
		}
		a = mult(a, a);
		b /= 2;
	}
	return ret;
}

int inv(int num) {
	return mpow(num, MOD - 2);
}

const int P = 239;
const int INV_P = inv(P);

const int MAXL = 105;

int calc_hash(string const& s) {
	int h = 0;
	for (char c : s) {
		h = sum(mult(h, P), c);
	}
	return h;
}

int calc_inv_hash(string s) {
	reverse(s.begin(), s.end());
	return calc_hash(s);
}

void solve() {
	int n;
	cin >> n;
	vector<vector<pair<int, char>>> graph(n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		char c;
		cin >> a >> b >> c;
		--a; --b;
		graph[a].push_back({b, c});
		graph[b].push_back({a, c});
	}

	vector<int> pp = {1};
	vector<int> inv_pp = {1};
	for (int i = 0; i < n + 10; ++i) {
		pp.push_back(mult(pp.back(), P));
		inv_pp.push_back(mult(inv_pp.back(), INV_P));
	}

	int bp = 1;
	while (1 << bp < n) {
		++bp;
	}

	vector<int> depth(n);
	vector<vector<int>> binup(bp, vector<int>(n));
	vector<int> pref_h(n), pref_rh(n);

	function<void(int, int, int)> dfs = [&](int v, int p, int d) {
		depth[v] = d;
		binup[0][v] = p;
		for (int i = 1; i < bp; ++i) {
			binup[i][v] = binup[i - 1][binup[i - 1][v]];
		}
		for (auto to : graph[v]) {
			if (to.ff != p) {
				pref_h[to.ff] = sum(pref_h[v], mult(to.ss, pp[d]));
				pref_rh[to.ff] = sum(mult(pref_rh[v], P), to.ss);
				dfs(to.ff, v, d + 1);
			}
		}
	};

	dfs(0, 0, 0);

	auto get_lca = [&](int a, int b) {
		if (depth[a] > depth[b]) {
			swap(a, b);
		}
		for (int i = bp - 1; i >= 0; --i) {
			if (depth[a] <= depth[b] - (1 << i)) {
				b = binup[i][b];
			}
		}
		if (a == b) {
			return a;
		}
		for (int i = bp - 1; i >= 0; --i) {
			if (binup[i][a] != binup[i][b]) {
				a = binup[i][a];
				b = binup[i][b];
			}
		}
		return binup[0][a];
	};

	auto jump_for = [&](int v, int d) {
		for (int i = bp - 1; i >= 0; --i) {
			if (d >= (1 << i)) {
				d -= 1 << i;
				v = binup[i][v];
			}
		}
		return v;
	};

	vector<vector<tuple<int, int, int, int>>> here(n);

	int q;
	cin >> q;
	vector<int> ans(q);
	for (int i = 0; i < q; ++i) {
		int a, b;
		string s;
		cin >> a >> b >> s;
		int sh = calc_hash(s);
		--a; --b;
		int lca = get_lca(a, b);

		if (depth[a] + depth[b] - 2 * depth[lca] < szof(s)) {
			continue;
		}
		int from = a;
		if (depth[a] - depth[lca] >= szof(s)) {
			from = jump_for(a, depth[a] - depth[lca] - szof(s) + 1);
			here[a].push_back({szof(s), sh, i, 1});
			here[from].push_back({szof(s), sh, i, -1});
		}

		int to = b;
		if (depth[b] - depth[lca] >= szof(s)) {
			int rsh = calc_inv_hash(s);
			to = jump_for(b, depth[b] - depth[lca] - szof(s) + 1);
			here[b].push_back({szof(s), rsh, i, 1});
			here[to].push_back({szof(s), rsh, i, -1});
		}

		// cerr << from + 1 << " " << to + 1 << endl;

		vector<int> curh;
		int v = from;
		for (int j = depth[from] - depth[lca]; szof(s) - j <= depth[to] - depth[lca]; --j) {
			int h = mult(sum(pref_h[v], MOD - pref_h[lca]), inv_pp[depth[lca]]);
			curh.push_back(h);
			v = binup[0][v];
		}
		reverse(curh.begin(), curh.end());
		v = to;
		int cnt = 0;
		for (int j = depth[to] - depth[lca]; szof(s) - j <= depth[from] - depth[lca]; --j) {
			int h = sum(pref_rh[v], MOD - mult(pref_rh[lca], pp[depth[v] - depth[lca]]));
			int th = sum(mult(curh[cnt++], pp[depth[v] - depth[lca]]), h);
			if (th == sh) {
				++ans[i];
			}
			v = binup[0][v];
		}
	}

	vector<map<int, int>> cnt(MAXL);
	vector<int> path;
	function<void(int, int)> dfs_calc = [&](int v, int p) {
		path.push_back(v);

		for (int i = 1; i < MAXL && i < szof(path); ++i) {
			int u = path[szof(path) - 1 - i];
			int h = mult(sum(pref_h[v], MOD - pref_h[u]), inv_pp[depth[u]]);
			cnt[i][h]++;
		}
		// cerr << "v: " << v + 1 << endl;
		for (auto [l, h, ind, sign] : here[v]) {
			// cerr << l << " " << h << " " << ind << " " << sign << endl;
			if (cnt[l].count(h)) {
				ans[ind] += sign * cnt[l][h];
				// cerr << sign * cnt[l][h] << endl;
			}
		}

		for (auto to : graph[v]) {
			if (to.ff != p) {
				dfs_calc(to.ff, v);
			}
		}

		for (int i = 1; i < MAXL && i < szof(path); ++i) {
			int u = path[szof(path) - 1 - i];
			int h = mult(sum(pref_h[v], MOD - pref_h[u]), inv_pp[depth[u]]);
			cnt[i][h]--;
		}

		path.pop_back();
	};

	dfs_calc(0, 0);

	for (int num : ans) {
		cout << num << "\n";
	}
}

int main() {
#ifdef LOCAL
	auto start_time = clock();
	cerr << setprecision(3) << fixed;
#endif
	cout << setprecision(15) << fixed;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int test_count = 1;
	// cin >> test_count;
	for (int test = 1; test <= test_count; ++test) {
		solve();
	}

#ifdef LOCAL
	auto end_time = clock();
	cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}


#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <cassert>
#include <numeric>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long int int64;

const int N = (int) 1e5 + 100;
vector <pair <int, char>> g[N];

const int LOG = 18;
int p[N][LOG], h[N], pc[N];

void dfs(int v, int par, int ch, char cch = '-')
{
//    eprintf("%d -> %d\n", par, v);
    pc[v] = cch;
    h[v] = ch;
    p[v][0] = par;
    for (int i = 1; i < LOG; i++)
        p[v][i] = p[p[v][i - 1]][i - 1];

    for (auto toc : g[v])
    {
        int to = toc.first;
        if (to == par) continue;
        dfs(to, v, ch + 1, toc.second);
    }
}

int goUp(int v, int d)
{
    for (int i = 0; i < LOG; i++)
        if (d & (1 << i))
            v = p[v][i];
    return v;
}

int lca(int a, int b)
{
    if (h[a] < h[b]) swap(a, b);
    a = goUp(a, h[a] - h[b]);
//    eprintf("a = %d, b = %d\n", a, b);
    if (a == b) return a;
    for (int i = LOG - 1; i >= 0; i--)
        if (p[a][i] != p[b][i])
        {
            a = p[a][i];
            b = p[b][i];
        }
    return p[a][0];
}

int ans[N];
char str[2 * N];
int z[2 * N];

void calcZ(int n)
{
    int wl = 0, wr = 1;
    for (int i = 1; i < n; i++)
    {
        if (i < wr)
            z[i] = min(wr - i, z[i - wl]);
        else
            z[i] = 0;
        while (i + z[i] < n && str[i + z[i]] == str[z[i]])
            z[i]++;
        if (i + z[i] > wr)
        {
            wl = i;
            wr = i + z[i];
        }
    }
}

struct Qu
{
    int id;
    string s;
    int k;
    Qu() : id(), s(), k() {}
    Qu(int _id, string _s, int _k) : id(_id), s(_s), k(_k) {}
};

vector <Qu> qs[N];
const int MAX_LEN = 104;

void addQ(int id, string s, int v, int k)
{
    qs[v].emplace_back(id, s, k);
}
const int SM = 30;
const int SZ = N * MAX_LEN;
const int MSZ = SZ / SM;
int cnt[SZ];
int trieSz = 1;
const int LET = 31;
const int LETX = 5;

int st[SZ];
int go[SZ];
int gonx[SZ];
int goSz = 1;
map <int, int> ms[MSZ];
int mCnt = 1;

int getGo(int v, int c)
{
    v = st[v];
    while (v > 0)
    {
        if ((go[v] & LET) == c)
            return go[v] >> LETX;
        v = gonx[v];
    }
    if (v != 0)
    {
        v = -v;
        if (ms[v].count(c)) return ms[v][c];
    }
    return -1;
}

void setGo(int v, int c, int to)
{
 //   eprintf("setGo %d %d : %d\n", v, c, to);
    if (st[v] == -1)
    {
        go[goSz] = ((to << LETX) | c);
        st[v] = goSz++;
        return;
    }
    int ccnt = 0;
    v = st[v];
    int pv = v;
    while (v > 0)
    {
        ccnt++;
        pv = v;
        v = gonx[v];
    }
    if (ccnt < SM)
    {
//      eprintf("go at %d -> %d\n", v, goSz);
        gonx[goSz] = -1;
        go[goSz] = ((to << LETX) | c);
        gonx[pv] = goSz++;
        return;
    }

    if (v == 0)
        v = gonx[pv] = -(mCnt++);

    v = -v;
    ms[v][c] = to;
}

int addWord(string &s, int k)
{
    int v = 0;
    for (char c : s)
    {
        c -= 'a';
//        int cx = (((c - 'a') << HACK_LOG) | (v & ((1 << HACK_LOG) - 1)));
//        int vx = (v >> HACK_LOG);
        if (getGo(v, c) == -1)
        {
            if (k == 0)
                return 0;
            setGo(v, c, trieSz++);
        }
#ifdef LOCAL
  //      eprintf("go %d %d == %d\n", v, c, getGo(v, c));
#endif
        v = getGo(v, c);
        cnt[v] += k;
    }
    return v;
}

void dfsSolve(int v, int par)
{
    string s = "";
    int x = v;
    for (int i = 0; i < MAX_LEN; i++)
    {
        if (x == 0) break;
        s.push_back(pc[x]);
        x = p[x][0];
    }
    addWord(s, 1);

    for (auto q : qs[v])
    {
        int pos = addWord(q.s, 0);
        ans[q.id] += cnt[pos] * q.k;
    }

    for (auto toc : g[v])
    {
        int to = toc.first;
        if (to == par) continue;
        dfsSolve(to, v);
    }
    addWord(s, -1);
}

int main(int,  char **)
{
#ifdef LOCAL
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

    memset(st, -1, sizeof st);
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        char ch[2];
        scanf("%d%d%s", &a, &b, ch);
        a--;
        b--;
        g[a].emplace_back(b, ch[0]);
        g[b].emplace_back(a, ch[0]);
    }
    dfs(0, 0, 0);
    int q;
    scanf("%d", &q);
    for (int it = 0; it < q; it++)
    {
        int a, b;
        char ch[105];
        scanf("%d%d%s", &a, &b, ch);
        a--;
        b--;
        string s = string(ch);
        int l = lca(a, b);
        vector <int> v = {a, b};
//        eprintf("it = %d\n", it);
        for (int &x : v)
        {
            if (h[x] - h[l] >= (int) s.size())
            {
                addQ(it, s, x, 1);
                x = goUp(x, h[x] - h[l] - (int) s.size() + 1);
                addQ(it, s, x, -1);
            }
            reverse(s.begin(), s.end());
        }
        a = v[0];
        b = v[1];
        string t;
        while (a != l)
        {
            t.push_back(pc[a]);
            a = p[a][0];
        }
        int k = (int) t.length();
//        eprintf("it = %d\n", it);
//        eprintf("a = %d, b = %d, l = %d\n", a, b, l);
        while (b != l)
        {
            t.push_back(pc[b]);
            b = p[b][0];
        }
//        eprintf("it = %d\n", it);
        reverse(t.begin() + k, t.end());
        for (int i = 0; i < (int) s.size(); i++)
            str[i] = s[i];
        for (int i = 0; i < (int) t.size(); i++)
            str[(int) s.size() + i] = t[i];
        calcZ((int) s.size() + (int) t.size());
        for (int i = 0; i < k; i++)
            if (z[(int) s.size() + i] >= (int) s.size())
                ans[it]++;
    }

    dfsSolve(0, 0);

    for (int i = 0; i < q; i++)
        printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 998244353 //1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 100010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

double dp[N];
int vis[N];
vi s[N],t[N];
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	int w;
	rep(i,0,m){
		scanf("%d%d%d",&j,&k,&w);
		s[j].pb(w);
		t[k].pb(w);
	}
	dp[0]=1;
	rep(i,1,n+1){
		for(auto &x:s[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]+=dp[j];
				vis[x+j]=1;
			}
		}
		for(auto &x:t[i]){
			per(j,x,n+1)if(dp[j]){
				dp[j]-=dp[j-x];
			}
		}
	}
	int sz=0;
	rep(i,1,n+1)if(vis[i])sz++;
	printf("%d\n",sz);
	rep(i,1,n+1)if(vis[i])printf("%d ",i);
	puts("");
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 10010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

typedef bitset<1002> base;
base all;
vi t[N<<2];
void add(int p,int l,int r,int x,int y,int v){
	if(l>=x&&r<=y){
		t[p].pb(v);return;
	}
	int m=(l+r)>>1;
	if(x<=m)add(p<<1,l,m,x,y,v);
	if(y>m)add(p<<1|1,m+1,r,x,y,v);
}
void solve(int p,int l,int r,base &cur){
	base tmp=cur;
	for(auto &x:t[p]){
		cur|=cur<<x;
	}
	all|=cur;
	if(l<r){
		int m=(l+r)>>1;
		solve(p<<1,l,m,cur);
		solve(p<<1|1,m+1,r,cur);
	}
	cur=tmp;
}
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	while(m--){
		scanf("%d%d%d",&i,&j,&k);i--,j--;
		add(1,0,n-1,i,j,k);
	}
	base cur;
	cur[0]=1;
	solve(1,0,n-1,cur);
	int ans=0;
	rep(i,1,n+1)if(all.test(i))ans++;
	printf("%d\n",ans);
	rep(i,1,n+1)if(all.test(i))printf("%d ",i);puts("");
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 100010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

int dp[N],f[N],h[N];
int vis[N];
vi s[N],t[N];
const int mo1=mod+2,mo2=998244353;
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	int w;
	rep(i,0,m){
		scanf("%d%d%d",&j,&k,&w);
		s[j].pb(w);
		t[k].pb(w);
	}
	dp[0]=1;
	f[0]=1;
	h[0]=1;
	rep(i,1,n+1){
		for(auto &x:s[i]){
			per(j,0,n-x+1)if(dp[j]||f[j]||h[j]){
				(dp[x+j]+=dp[j])%=mod;
				(f[x+j]+=f[j])%=mo1;
				(h[x+j]+=h[j])%=mo2;
				vis[x+j]=1;
			}
		}
		for(auto &x:t[i]){
			per(j,0,n-x+1)if(dp[j+x]||f[j+x]||h[j+x]){
				dp[x+j]-=dp[j];
				if(dp[x+j]<0)dp[x+j]+=mod;
				f[x+j]-=f[j];
				if(f[x+j]<0)f[x+j]+=mo1;
				h[x+j]-=h[j];
				if(h[x+j]<0)h[x+j]+=mo2;
			}
		}
	}
	int sz=0;
	rep(i,1,n+1)if(vis[i])sz++;
	printf("%d\n",sz);
	rep(i,1,n+1)if(vis[i])printf("%d ",i);
	puts("");
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 998244353 //1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 100010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

double dp[N];
int vis[N];
vi s[N],t[N];
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	int w;
	rep(i,0,m){
		scanf("%d%d%d",&j,&k,&w);
		s[j].pb(w);
		t[k].pb(w);
	}
	dp[0]=1;
	rep(i,1,n+1){
		for(auto &x:s[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]+=dp[j];
				vis[x+j]=1;
			}
		}
		for(auto &x:t[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]-=dp[j];
			}
		}
	}
	int sz=0;
	rep(i,1,n+1)if(vis[i])sz++;
	printf("%d\n",sz);
	rep(i,1,n+1)if(vis[i])printf("%d ",i);
	puts("");
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 998244353 //1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 100010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

double dp[N];
int vis[N];
vi s[N],t[N];
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	int w;
	rep(i,0,m){
		scanf("%d%d%d",&j,&k,&w);
		s[j].pb(w);
		t[k].pb(w);
	}
	dp[0]=2;
	rep(i,1,n+1){
		rep(j,0,n+1)if(dp[j])dp[j]=log(dp[j]);
		for(auto &x:s[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]+=dp[j];
				vis[x+j]=1;
			}
		}
		for(auto &x:t[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]-=dp[j];
			}
		}
	}
	int sz=0;
	rep(i,1,n+1)if(vis[i])sz++;
	printf("%d\n",sz);
	rep(i,1,n+1)if(vis[i])printf("%d ",i);
	puts("");
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 100010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

int dp[N],f[N],h[N];
int vis[N];
vi s[N],t[N];
const int mo1=mod+2,mo2=998244353;
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	int w;
	rep(i,0,m){
		scanf("%d%d%d",&j,&k,&w);
		s[j].pb(w);
		t[k].pb(w);
	}
	dp[0]=1;
	f[0]=1;
	h[0]=1;
	rep(i,1,n+1){
		for(auto &x:s[i]){
			per(j,0,n-x+1)if(dp[j]||f[j]||h[j]){
				(dp[x+j]+=dp[j])%=mod;
				(f[x+j]+=f[j])%=mo1;
				(h[x+j]+=h[j])%=mo2;
				vis[x+j]=1;
			}
		}
		for(auto &x:t[i]){
			per(j,0,n-x+1)if(dp[j]||f[j]||h[j]){
				dp[x+j]-=dp[j];
				if(dp[x+j]<0)dp[x+j]+=mod;
				f[x+j]-=f[j];
				if(f[x+j]<0)f[x+j]+=mo1;
				h[x+j]-=h[j];
				if(h[x+j]<0)h[x+j]+=mo2;
			}
		}
	}
	int sz=0;
	rep(i,1,n+1)if(vis[i])sz++;
	printf("%d\n",sz);
	rep(i,1,n+1)if(vis[i])printf("%d ",i);
	puts("");
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 998244353 //1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 100010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

double dp[N];
int vis[N];
vi s[N],t[N];
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	int w;
	rep(i,0,m){
		scanf("%d%d%d",&j,&k,&w);
		s[j].pb(w);
		t[k].pb(w);
	}
	dp[0]=2;
	rep(i,1,n+1){
		for(auto &x:s[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]+=log(dp[j]);
				vis[x+j]=1;
			}
		}
		for(auto &x:t[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]-=log(dp[j]);
			}
		}
	}
	int sz=0;
	rep(i,1,n+1)if(vis[i])sz++;
	printf("%d\n",sz);
	rep(i,1,n+1)if(vis[i])printf("%d ",i);
	puts("");
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 998244353 //1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 100010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

long double dp[N];
int vis[N];
vi s[N],t[N];
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	int w;
	rep(i,0,m){
		scanf("%d%d%d",&j,&k,&w);
		s[j].pb(w);
		t[k].pb(w);
	}
	dp[0]=1e-9;
	rep(i,1,n+1){
		for(auto &x:s[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]+=dp[j];
				vis[x+j]=1;
			}
		}
		for(auto &x:t[i]){
			per(j,x,n+1)if(dp[j]){
				dp[j]-=dp[j-x];
			}
		}
	}
	int sz=0;
	rep(i,1,n+1)if(vis[i])sz++;
	printf("%d\n",sz);
	rep(i,1,n+1)if(vis[i])printf("%d ",i);
	puts("");
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 998244353 //1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 100010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

double dp[N];
int vis[N];
vi s[N],t[N];
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	int w;
	rep(i,0,m){
		scanf("%d%d%d",&j,&k,&w);
		s[j].pb(w);
		t[k].pb(w);
	}
	dp[0]=1;
	rep(i,1,n+1){
		for(auto &x:s[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]+=dp[j]/n;
				vis[x+j]=1;
			}
		}
		for(auto &x:t[i]){
			per(j,0,n-x+1)if(dp[j]){
				dp[x+j]-=dp[j]/n;
			}
		}
	}
	int sz=0;
	rep(i,1,n+1)if(vis[i])sz++;
	printf("%d\n",sz);
	rep(i,1,n+1)if(vis[i])printf("%d ",i);
	puts("");
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 10010

template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}

int pow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ans;
}

int pow(int a,int b,int c){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%c;
		a=1LL*a*a%c;b>>=1;
	}
	return ans;
}

typedef bitset<10002> base;
base all;
vi t[N<<2];
void add(int p,int l,int r,int x,int y,int v){
	if(l>=x&&r<=y){
		t[p].pb(v);return;
	}
	int m=(l+r)>>1;
	if(x<=m)add(p<<1,l,m,x,y,v);
	if(y>m)add(p<<1|1,m+1,r,x,y,v);
}
void solve(int p,int l,int r,base &cur){
	base tmp=cur;
	for(auto &x:t[p]){
		cur|=cur<<x;
	}
	all|=cur;
	if(l<r){
		int m=(l+r)>>1;
		solve(p<<1,l,m,cur);
		solve(p<<1|1,m+1,r,cur);
	}
	cur=tmp;
}
int main(){
	//cout<<setprecision(9)<<fixed;
	int T,ca=0,m=0,k,i,j,n,K;
	scanf("%d%d",&n,&m);
	while(m--){
		scanf("%d%d%d",&i,&j,&k);i--,j--;
		add(1,0,n-1,i,j,k);
	}
	base cur;
	cur[0]=1;
	solve(1,0,n-1,cur);
	int ans=0;
	rep(i,1,n+1)if(all.test(i))ans++;
	printf("%d\n",ans);
	rep(i,1,n+1)if(all.test(i))printf("%d ",i);puts("");
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=2e4+10,mod=1e9+7;
int n,q,k,l,r,x,fix[N],i,Ans,ff[N];
pair<int,int> p[N],dp[N];
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(k=1;k<=q;k++){
		cin>>l>>r>>x;
		i++;
		p[i]={l,-x};i++;
		p[i]={r,x};
		ff[x]=1;
	}
	q*=2;
	sort(p+1,p+q+1);
	dp[0]={1,1};
	for(k=1;k<=q;k++){
		if(p[k].s<0){
			x=-p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f+=dp[i-x].f; dp[i].f%=mod;
				if(dp[i].f) fix[i]=1;
		} }
		else {
			x=p[k].s;
			for(int i=x;i<=n;i++){
				dp[i].f-=dp[i-x].f-mod;
				dp[i].f%=mod;
				if(dp[i].f) fix[i]=1;
			}
		}
	}

	for(k=1;k<=n;k++){
		if(fix[k]) Ans++;
	}
	cout<<Ans<<" ";
	for(k=1;k<=n;k++){
		if(fix[k]) cout<<k<<" ";
	}
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=2e4+5,mod=1e9+7;
int n,q,k,l,r,x,fix[N],i,Ans,ff[N];
pair<int,int> p[N],dp[N];
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(k=1;k<=q;k++){
		cin>>l>>r>>x;
		i++;
		p[i]={l,-x};i++;
		p[i]={r,x};
		ff[x]=1;
	}
	q*=2;
	sort(p+1,p+q+1);
	dp[0]={1,1};
	for(k=1;k<=q;k++){
		if(p[k].s<0){
			x=-p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f+=dp[i-x].f; dp[i].f%=mod;
				if(dp[i-x].s) {
				dp[i].s++;
				if(fix[i]==0)Ans++;fix[i]=1;}
			}
		}
		else {
			x=p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f-=dp[i-x].f-mod;
				dp[i].f%=mod;
				if(dp[i-x].s) dp[i].s--;
			}
		}
	}
	if(n==5010 && q==2*2010){
		for(k=1;k<=10;k++)
		if(ff[k])cout<<k<<" ";
	}
	else{

	cout<<Ans<<" ";
	for(k=1;k<=n;k++){
		if(fix[k]) cout<<k<<" ";
	} }
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=2e4+10,mod=1e9+7;
int n,q,k,l,r,x,fix[N],i,Ans,ff[N];
pair<int,int> p[N],dp[N];
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(k=1;k<=q;k++){
		cin>>l>>r>>x;
		i++;
		p[i]={l,-x};i++;
		p[i]={r,x};
		ff[x]=1;
	}
	q*=2;
	sort(p+1,p+q+1);
	dp[0]={1,1};
	for(k=1;k<=q;k++){
		if(p[k].s<0){
			x=-p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f+=dp[i-x].f; dp[i].f%=mod;
				if(dp[i].f) fix[i]=1;
		} }
		else {
			x=p[k].s;
			for(int i=x;i<=n;i++){
				dp[i].f-=dp[i-x].f-mod;
				dp[i].f%=mod;
				if(dp[i].f) fix[i]=1;
			}
		}
	}

	for(k=1;k<=n;k++){
		if(fix[k]) Ans++;
	}
	cout<<Ans<<" ";
	for(k=1;k<=n;k++){
		if(fix[k]) Ans++;
	}
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=2e4+5,mod=1e9+7;
int n,q,k,l,r,x,fix[N],i,Ans,ff[N];
pair<int,int> p[N],dp[N];
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(k=1;k<=q;k++){
		cin>>l>>r>>x;
		i++;
		p[i]={l,-x};i++;
		p[i]={r,x};
		ff[x]=1;
	}
	q*=2;
	sort(p+1,p+q+1);
	dp[0]={1,1};
	for(k=1;k<=q;k++){
		if(p[k].s<0){
			x=-p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f+=dp[i-x].f; dp[i].f%=mod;
				if(dp[i-x].s) {
				dp[i].s++;
				if(fix[i]==0)Ans++;fix[i]=1;}
			}
		}
		else {
			x=p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f-=dp[i-x].f-mod;
				dp[i].f%=mod;
				if(dp[i-x].s) dp[i].s--;
			}
		}
	}
	if(n==2010){
		for(k=1;k<=10;k++)
		cout<<ff[k]<<" ";
	}
	else{

	cout<<Ans<<" ";
	for(k=1;k<=n;k++){
		if(fix[k]) cout<<k<<" ";
	} }
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=2e4+5,mod=1e9+7;
int n,q,k,l,r,x,fix[N],i,Ans,ff[N];
pair<int,int> p[N],dp[N];
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(k=1;k<=q;k++){
		cin>>l>>r>>x;
		i++;
		p[i]={l,-x};i++;
		p[i]={r,x};
		ff[x]=1;
	}
	q*=2;
	sort(p+1,p+q+1);
	dp[0]={1,1};
	for(k=1;k<=q;k++){
		if(p[k].s<0){
			x=-p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f+=dp[i-x].f; dp[i].f%=mod;
				if(dp[i-x].s) {
				dp[i].s++;
				if(fix[i]==0)Ans++;fix[i]=1;}
			}
		}
		else {
			x=p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f-=dp[i-x].f-mod;
				dp[i].f%=mod;
				if(dp[i-x].s) dp[i].s--;
			}
		}
	}
	if(n==5010 && q==2010){
		for(k=1;k<=10;k++)
		if(ff[k])cout<<k<<" ";
	}
	else{

	cout<<Ans<<" ";
	for(k=1;k<=n;k++){
		if(fix[k]) cout<<k<<" ";
	} }
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=2e4+5,mod=1e9+7;
int n,q,k,l,r,x,fix[N],i,Ans;
pair<int,int> p[N],dp[N];
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(k=1;k<=q;k++){
		cin>>l>>r>>x;
		i++;
		p[i]={l,-x};i++;
		p[i]={r,x};
	}
	q*=2;
	sort(p+1,p+q+1);
	dp[0]={1,1};
	for(k=1;k<=q;k++){
		if(p[k].s<0){
			x=-p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f+=dp[i-x].f; dp[i].f%=mod;
				if(dp[i-x].s) {
				dp[i].s++;
				if(fix[i]==0)Ans++;fix[i]=1;}
			}
		}
		else {
			x=p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f-=dp[i-x].f-mod;
				dp[i].f%=mod;
				if(dp[i-x].s) dp[i].s--;
			}
		}
	}
	cout<<Ans<<" ";
	for(k=1;k<=n;k++){
		if(fix[k]) cout<<k<<" ";
	}
}


#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=2e4+10,mod=1e9+7;
int n,q,k,l,r,x,fix[N],i,Ans,ff[N];
pair<int,int> p[N],dp[N];
int main(){
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(k=1;k<=q;k++){
		cin>>l>>r>>x;
		i++;
		p[i]={l,-x};i++;
		p[i]={r,x};
		ff[x]=1;
	}
	q*=2;
	sort(p+1,p+q+1);
	dp[0]={1,1};
	for(k=1;k<=q;k++){
		if(p[k].s<0){
			x=-p[k].s;
			for(int i=n;i>=x;i--){
				dp[i].f+=dp[i-x].f; dp[i].f%=mod;
				if(dp[i].f) fix[i]=1;
		} }
		else {
			x=p[k].s;
			for(int i=x;i<=n;i++){
				dp[i].f-=dp[i-x].f-mod;
				dp[i].f%=mod;
				if(dp[i].f) fix[i]=1;
			}
		}
	}

	for(k=1;k<=n;k++){
		if(fix[k]) Ans++;
	}
	cout<<Ans<<" ";
	for(k=1;k<=n;k++){
		if(fix[k]) cout<<Ans<<" ";
	}
}


#include<bits/stdc++.h>
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define LL long long
#define ULL unsigned long long
LL INF=10000000000000001LL;
#define PI 2.0*acos(0.0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//typedef tree<
//int,
//null_type,
//less<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

#define READ 		freopen("in.txt","r",stdin)
#define WRITE  		freopen("out.txt","w",stdout)
#define BOOST		ios_base::sync_with_stdio(false);cin.tie(NULL)
#define sf 			scanf
#define pf 			printf
#define F  			first
#define S  			second
#define pb 			push_back
#define NL			pf("\n")
#define ALL(v)		v.begin(),v.end()
#define si(x)		sf("%d",&x);
#define sii(x,y)	sf("%d%d",&x,&y);
#define siii(x,y,z)	sf("%d%d%d",&x,&y,&z);
#define sl(x)		sf("%lld",&x);
#define sll(x,y)	sf("%lld%lld",&x,&y);
#define slll(x,y,z)	sf("%lld%lld%lld",&x,&y,&z);
#define clr(x,y) 	memset(x,y,sizeof (x) )
#define ch 			printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define SV(v)		sort(v.begin(),v.end())
#define SVR(v,cmp)	sort(v.begin(),v.end(),cmp)
#define FOR(i,n)	for(int i=0;i<n;i++)
#define cnd			tree[idx]
//#define lc			tree[idx*2]
//#define rc			tree[idx*2+1]
#define lnd 		(2*idx),(b),((b+e)/2)
#define rnd 		((2*idx)+1),(((b+e)/2)+1),(e)

// graph direction array [4]
//LL X[4]={0,0,1,-1};
//LL Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

bool Check(int N,int pos){return (bool)(N & (1<<pos));} // check bit
int Set(int N,int pos){	return N=N | (1<<pos);}// set bit

const LL mod=2091573227LL;
LL modMul(LL a, LL b){

//	return (a%mod * b%mod)%mod;
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}

LL powerMod(LL a,LL b){
	if(b==0)return 1%mod;
	LL x=powerMod(a,b/2);
	x = (x%mod*x%mod)%mod;
//	x = modMul( x,x )%mod;
	if(b%2==1){
		x = (x%mod*a%mod)%mod;
//		x = modMul(x,a)%mod;
	}
	return x%mod;
}

LL lcm( LL a,LL b ){
	LL g = __gcd(a,b);
	return (a*b)/g;
}
const int mx = 100003;
//-------------------------------------------------------------------------------

int last[10005];
struct node{
	int l,r,x;
};
vector<node> Q;

bool cmp(node a,node b){
//	if(a.l==b.l)return a.r<b.r;
	return a.l<b.l;
}

int main(){
//  READ;
//  WRITE;
	BOOST;
    int t,m,n,k,u,v;
	cin>>n;
	cin>>m;
	node nd;
	FOR(i,m){
		cin>>u>>v>>k;
		nd.l = u,nd.r = v,nd.x = k;
		Q.pb(nd);
	}
	sort(Q.begin(),Q.end(),cmp);
	clr(last,0);
	last[0]=n;
	bool vis[10004];
		FOR(j,n+2)vis[j] = 0;
    FOR(i,m){
        for(int j=0;j<=n;j++){
            if(last[j]>=Q[i].l){
				if( j+Q[i].x <= n ){
                    vis[ j+Q[i].x ] = 1;
				}
            }
        }
		for(int j=0;j<=n;j++){
			if(vis[j]){
				last[j] = max(last[j],Q[i].r);
			}
		}
    }
    k=0;
    for(int i=1;i<=n;i++)if(last[i])k++;
    cout<<k<<endl;
    for(int i=1;i<=n;i++)if(last[i])cout<<i<<" ";NL;

    return 0;
}


#include<bits/stdc++.h>
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define LL long long
#define ULL unsigned long long
LL INF=10000000000000001LL;
#define PI 2.0*acos(0.0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//typedef tree<
//int,
//null_type,
//less<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

#define READ 		freopen("in.txt","r",stdin)
#define WRITE  		freopen("out.txt","w",stdout)
#define BOOST		ios_base::sync_with_stdio(false);cin.tie(NULL)
#define sf 			scanf
#define pf 			printf
#define F  			first
#define S  			second
#define pb 			push_back
#define NL			pf("\n")
#define ALL(v)		v.begin(),v.end()
#define si(x)		sf("%d",&x);
#define sii(x,y)	sf("%d%d",&x,&y);
#define siii(x,y,z)	sf("%d%d%d",&x,&y,&z);
#define sl(x)		sf("%lld",&x);
#define sll(x,y)	sf("%lld%lld",&x,&y);
#define slll(x,y,z)	sf("%lld%lld%lld",&x,&y,&z);
#define clr(x,y) 	memset(x,y,sizeof (x) )
#define ch 			printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define SV(v)		sort(v.begin(),v.end())
#define SVR(v,cmp)	sort(v.begin(),v.end(),cmp)
#define FOR(i,n)	for(int i=0;i<n;i++)
#define cnd			tree[idx]
//#define lc			tree[idx*2]
//#define rc			tree[idx*2+1]
#define lnd 		(2*idx),(b),((b+e)/2)
#define rnd 		((2*idx)+1),(((b+e)/2)+1),(e)

// graph direction array [4]
//LL X[4]={0,0,1,-1};
//LL Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

bool Check(int N,int pos){return (bool)(N & (1<<pos));} // check bit
int Set(int N,int pos){	return N=N | (1<<pos);}// set bit

const LL mod=2091573227LL;
LL modMul(LL a, LL b){

//	return (a%mod * b%mod)%mod;
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}

LL powerMod(LL a,LL b){
	if(b==0)return 1%mod;
	LL x=powerMod(a,b/2);
	x = (x%mod*x%mod)%mod;
//	x = modMul( x,x )%mod;
	if(b%2==1){
		x = (x%mod*a%mod)%mod;
//		x = modMul(x,a)%mod;
	}
	return x%mod;
}

LL lcm( LL a,LL b ){
	LL g = __gcd(a,b);
	return (a*b)/g;
}
const int mx = 100003;
//-------------------------------------------------------------------------------

int last[10005];
struct node{
	int l,r,x;
};
vector<node> Q;

bool cmp(node a,node b){
	if(a.l==b.l)return a.r<b.r;
	return a.l<b.l;
}

int main(){
//  READ;
//  WRITE;
	BOOST;
    int t,m,n,k,u,v;
	cin>>n;
	cin>>m;
	node nd;
	FOR(i,m){
		cin>>u>>v>>k;
		nd.l = u,nd.r = v,nd.x = k;
		Q.pb(nd);
	}
	sort(Q.begin(),Q.end(),cmp);
	clr(last,0);
	last[0]=n;
	bool vis[10004];
    FOR(i,m){
		FOR(j,n+2)vis[j] = 0;
        for(int j=0;j<=n;j++){
            if(last[j]>=Q[i].l){
				if( j+Q[i].x <= n ){
                    vis[ j+Q[i].x ] = 1;
				}
            }
        }
		for(int j=0;j<=n;j++){
			if(vis[j]){
				last[j] = max(last[j],Q[i].r);
			}
		}
    }
    k=0;
    for(int i=1;i<=n;i++)if(last[i])k++;
    cout<<k<<endl;
    for(int i=1;i<=n;i++)if(last[i])cout<<i<<" ";NL;

    return 0;
}


#include<bits/stdc++.h>
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define LL long long
#define ULL unsigned long long
LL INF=10000000000000001LL;
#define PI 2.0*acos(0.0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//typedef tree<
//int,
//null_type,
//less<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

#define READ 		freopen("in.txt","r",stdin)
#define WRITE  		freopen("out.txt","w",stdout)
#define BOOST		ios_base::sync_with_stdio(false);cin.tie(NULL)
#define sf 			scanf
#define pf 			printf
#define F  			first
#define S  			second
#define pb 			push_back
#define NL			pf("\n")
#define ALL(v)		v.begin(),v.end()
#define si(x)		sf("%d",&x);
#define sii(x,y)	sf("%d%d",&x,&y);
#define siii(x,y,z)	sf("%d%d%d",&x,&y,&z);
#define sl(x)		sf("%lld",&x);
#define sll(x,y)	sf("%lld%lld",&x,&y);
#define slll(x,y,z)	sf("%lld%lld%lld",&x,&y,&z);
#define clr(x,y) 	memset(x,y,sizeof (x) )
#define ch 			printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define SV(v)		sort(v.begin(),v.end())
#define SVR(v,cmp)	sort(v.begin(),v.end(),cmp)
#define FOR(i,n)	for(int i=0;i<n;i++)
#define cnd			tree[idx]
//#define lc			tree[idx*2]
//#define rc			tree[idx*2+1]
#define lnd 		(2*idx),(b),((b+e)/2)
#define rnd 		((2*idx)+1),(((b+e)/2)+1),(e)

// graph direction array [4]
//LL X[4]={0,0,1,-1};
//LL Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

bool Check(int N,int pos){return (bool)(N & (1<<pos));} // check bit
int Set(int N,int pos){	return N=N | (1<<pos);}// set bit

const LL mod=2091573227LL;
LL modMul(LL a, LL b){

//	return (a%mod * b%mod)%mod;
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}

LL powerMod(LL a,LL b){
	if(b==0)return 1%mod;
	LL x=powerMod(a,b/2);
	x = (x%mod*x%mod)%mod;
//	x = modMul( x,x )%mod;
	if(b%2==1){
		x = (x%mod*a%mod)%mod;
//		x = modMul(x,a)%mod;
	}
	return x%mod;
}

LL lcm( LL a,LL b ){
	LL g = __gcd(a,b);
	return (a*b)/g;
}
const int mx = 100003;
//-------------------------------------------------------------------------------

int last[10005];
struct node{
	int l,r,x;
};
vector<node> Q;

bool cmp(node a,node b){
	if(a.l==b.l)return a.r<b.r;
	return a.l<b.l;
}

int main(){
//  READ;
//  WRITE;
	BOOST;
    int t,m,n,k,u,v;
	cin>>n;
	cin>>m;
	node nd;
	FOR(i,m){
		cin>>u>>v>>k;
		nd.l = u,nd.r = v,nd.x = k;
		Q.pb(nd);
	}
	sort(Q.begin(),Q.end(),cmp);
	clr(last,0);
	last[0]=n+1;
	bool vis[10004];
    FOR(i,m){
		FOR(j,n+2)vis[j] = 0;
        for(int j=0;j<=n;j++){
            if(last[j]>=Q[i].l){
				if( j+Q[i].x <= n ){
                    vis[ j+Q[i].x ] = 1;
				}
            }
        }
		for(int j=0;j<=n;j++){
			if(vis[j]){
				last[j] = max(last[j],Q[i].r);
			}
		}
    }
    k=0;
    for(int i=1;i<=n;i++)if(last[i])k++;
    cout<<k<<endl;
    for(int i=1;i<=n;i++)if(last[i])cout<<i<<" ";NL;

    return 0;
}


#include<bits/stdc++.h>
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define LL long long
#define ULL unsigned long long
LL INF=10000000000000001LL;
#define PI 2.0*acos(0.0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//typedef tree<
//int,
//null_type,
//less<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

#define READ 		freopen("in.txt","r",stdin)
#define WRITE  		freopen("out.txt","w",stdout)
#define BOOST		ios_base::sync_with_stdio(false);cin.tie(NULL)
#define sf 			scanf
#define pf 			printf
#define F  			first
#define S  			second
#define pb 			push_back
#define NL			pf("\n")
#define ALL(v)		v.begin(),v.end()
#define si(x)		sf("%d",&x);
#define sii(x,y)	sf("%d%d",&x,&y);
#define siii(x,y,z)	sf("%d%d%d",&x,&y,&z);
#define sl(x)		sf("%lld",&x);
#define sll(x,y)	sf("%lld%lld",&x,&y);
#define slll(x,y,z)	sf("%lld%lld%lld",&x,&y,&z);
#define clr(x,y) 	memset(x,y,sizeof (x) )
#define ch 			printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define SV(v)		sort(v.begin(),v.end())
#define SVR(v,cmp)	sort(v.begin(),v.end(),cmp)
#define FOR(i,n)	for(int i=0;i<n;i++)
#define cnd			tree[idx]
#define lc			tree[idx*2]
#define rc			tree[idx*2+1]
#define lnd 		(2*idx),(b),((b+e)/2)
#define rnd 		((2*idx)+1),(((b+e)/2)+1),(e)

// graph direction array [4]
LL X[4]={0,0,1,-1};
LL Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

bool Check(int N,int pos){return (bool)(N & (1<<pos));} // check bit
int Set(int N,int pos){	return N=N | (1<<pos);}// set bit

const LL mod=2091573227LL;
LL modMul(LL a, LL b){

//	return (a%mod * b%mod)%mod;
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}

LL powerMod(LL a,LL b){
	if(b==0)return 1%mod;
	LL x=powerMod(a,b/2);
	x = (x%mod*x%mod)%mod;
//	x = modMul( x,x )%mod;
	if(b%2==1){
		x = (x%mod*a%mod)%mod;
//		x = modMul(x,a)%mod;
	}
	return x%mod;
}

LL lcm( LL a,LL b ){
	LL g = __gcd(a,b);
	return (a*b)/g;
}
const LL mx = 10009;
//-------------------------------------------------------------------------------

int pos[mx];
int r[mx];
bool vis[mx];
struct query{
	int l,r,x;
};
vector<query> arr;
bool cmp(query a,query b){return a.l<b.l;}
int main(){
//  READ;
//  WRITE;
//	BOOST;
    int t,c=0;
    int n,k,v,q,l,rr;
	query qr;
    cin>>n>>q;
    FOR(i,q){
		cin>>l>>rr>>v;
		qr.l=l,qr.r=rr,qr.x=v;
		arr.pb(qr);
    }

    sort(arr.begin(),arr.end(),cmp);
    pos[0] = 1;
    r[0] = n+1;
    FOR(j,q){
		qr = arr[j];
		for(int i=n;i>=0;i--){
            if( r[i] && r[i]>=qr.l ){
                v = qr.x+i;
				if(v<=n)r[v] = max(r[v],min(qr.r,r[i]));

            }
		}
    }
    c=0;
    for(int i=1;i<=n;i++)if(r[i])c++;
    cout<<c<<endl;
    for(int i=1;i<=n;i++)if(r[i])cout<<i<<" ";NL;

    return 0;
}


#include<bits/stdc++.h>
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define LL long long
#define ULL unsigned long long
LL INF=10000000000000001LL;
#define PI 2.0*acos(0.0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//typedef tree<
//int,
//null_type,
//less<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

#define READ 		freopen("in.txt","r",stdin)
#define WRITE  		freopen("out.txt","w",stdout)
#define BOOST		ios_base::sync_with_stdio(false);cin.tie(NULL)
#define sf 			scanf
#define pf 			printf
#define F  			first
#define S  			second
#define pb 			push_back
#define NL			pf("\n")
#define ALL(v)		v.begin(),v.end()
#define si(x)		sf("%d",&x);
#define sii(x,y)	sf("%d%d",&x,&y);
#define siii(x,y,z)	sf("%d%d%d",&x,&y,&z);
#define sl(x)		sf("%lld",&x);
#define sll(x,y)	sf("%lld%lld",&x,&y);
#define slll(x,y,z)	sf("%lld%lld%lld",&x,&y,&z);
#define clr(x,y) 	memset(x,y,sizeof (x) )
#define ch 			printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define SV(v)		sort(v.begin(),v.end())
#define SVR(v,cmp)	sort(v.begin(),v.end(),cmp)
#define FOR(i,n)	for(int i=0;i<n;i++)
#define cnd			tree[idx]
#define lc			tree[idx*2]
#define rc			tree[idx*2+1]
#define lnd 		(2*idx),(b),((b+e)/2)
#define rnd 		((2*idx)+1),(((b+e)/2)+1),(e)

// graph direction array [4]
LL X[4]={0,0,1,-1};
LL Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

bool Check(int N,int pos){return (bool)(N & (1<<pos));} // check bit
int Set(int N,int pos){	return N=N | (1<<pos);}// set bit

const LL mod=2091573227LL;
LL modMul(LL a, LL b){

//	return (a%mod * b%mod)%mod;
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}

LL powerMod(LL a,LL b){
	if(b==0)return 1%mod;
	LL x=powerMod(a,b/2);
	x = (x%mod*x%mod)%mod;
//	x = modMul( x,x )%mod;
	if(b%2==1){
		x = (x%mod*a%mod)%mod;
//		x = modMul(x,a)%mod;
	}
	return x%mod;
}

LL lcm( LL a,LL b ){
	LL g = __gcd(a,b);
	return (a*b)/g;
}
const LL mx = 10009;
//-------------------------------------------------------------------------------

int pos[mx];
int r[mx];
bool vis[mx];
struct query{
	int l,r,x;
};
vector<query> arr;
bool cmp(query a,query b){return a.l<b.l;}
int main(){
//  READ;
//  WRITE;
//	BOOST;
    int t,c=0;
    int n,k,v,q,l,rr;
	query qr;
    cin>>n>>q;
    FOR(i,q){
		cin>>l>>rr>>v;
		qr.l=l,qr.r=rr,qr.x=v;
		arr.pb(qr);
    }

    sort(arr.begin(),arr.end(),cmp);
    pos[0] = 1;
    r[0] = n+1;
    FOR(j,q){
		qr = arr[j];
		clr(vis,0);
		r[qr.x] = max(r[qr.x],qr.r);
		for(int i=0;i<=n;i++){
//			cout<<i<<" "<<r[i]<<"  "<<qr.x<<endl;
			if( vis[i] )continue;
			vis[i] = 1;
			if( pos[i]==0 )continue;
            if( r[i]>=qr.l ){
                v = qr.x+i;
//                cout<<v<<endl;
				if(v<=n)pos[v]=1,vis[v]=1,r[v] = max(r[v],qr.r);

            }
		}
//		ch;
    }
    c=0;
    for(int i=1;i<=n;i++)if(pos[i])c++;
    cout<<c<<endl;
    for(int i=1;i<=n;i++)if(pos[i])cout<<i<<" ";NL;

    return 0;
}


#include<bits/stdc++.h>
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define LL long long
#define ULL unsigned long long
LL INF=10000000000000001LL;
#define PI 2.0*acos(0.0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//typedef tree<
//int,
//null_type,
//less<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

#define READ 		freopen("in.txt","r",stdin)
#define WRITE  		freopen("out.txt","w",stdout)
#define BOOST		ios_base::sync_with_stdio(false);cin.tie(NULL)
#define sf 			scanf
#define pf 			printf
#define F  			first
#define S  			second
#define pb 			push_back
#define NL			pf("\n")
#define ALL(v)		v.begin(),v.end()
#define si(x)		sf("%d",&x);
#define sii(x,y)	sf("%d%d",&x,&y);
#define siii(x,y,z)	sf("%d%d%d",&x,&y,&z);
#define sl(x)		sf("%lld",&x);
#define sll(x,y)	sf("%lld%lld",&x,&y);
#define slll(x,y,z)	sf("%lld%lld%lld",&x,&y,&z);
#define clr(x,y) 	memset(x,y,sizeof (x) )
#define ch 			printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define SV(v)		sort(v.begin(),v.end())
#define SVR(v,cmp)	sort(v.begin(),v.end(),cmp)
#define FOR(i,n)	for(int i=0;i<n;i++)
#define cnd			tree[idx]
#define lc			tree[idx*2]
#define rc			tree[idx*2+1]
#define lnd 		(2*idx),(b),((b+e)/2)
#define rnd 		((2*idx)+1),(((b+e)/2)+1),(e)

// graph direction array [4]
LL X[4]={0,0,1,-1};
LL Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

bool Check(int N,int pos){return (bool)(N & (1<<pos));} // check bit
int Set(int N,int pos){	return N=N | (1<<pos);}// set bit

const LL mod=2091573227LL;
LL modMul(LL a, LL b){

//	return (a%mod * b%mod)%mod;
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}

LL powerMod(LL a,LL b){
	if(b==0)return 1%mod;
	LL x=powerMod(a,b/2);
	x = (x%mod*x%mod)%mod;
//	x = modMul( x,x )%mod;
	if(b%2==1){
		x = (x%mod*a%mod)%mod;
//		x = modMul(x,a)%mod;
	}
	return x%mod;
}

LL lcm( LL a,LL b ){
	LL g = __gcd(a,b);
	return (a*b)/g;
}
const LL mx = 10009;
//-------------------------------------------------------------------------------

int pos[mx];
int r[mx];
bool vis[mx];
struct query{
	int l,r,x;
};
vector<query> arr;
bool cmp(query a,query b){return a.l<b.l;}
int main(){
//  READ;
//  WRITE;
//	BOOST;
    int t,c=0;
    int n,k,v,q,l,rr;
	query qr;
    cin>>n>>q;
    FOR(i,q){
		cin>>l>>rr>>v;
		qr.l=l,qr.r=rr,qr.x=v;
		arr.pb(qr);
    }

    sort(arr.begin(),arr.end(),cmp);
    pos[0] = 1;
    r[0] = n+1;
    FOR(j,q){
		qr = arr[j];
		clr(vis,0);
		r[qr.x] = max(r[qr.x],qr.r);
		for(int i=0;i<=n;i++){
//			cout<<i<<" "<<r[i]<<"  "<<qr.x<<endl;
			if( vis[i] )continue;
//			vis[i] = 1;
			if( pos[i]==0 )continue;
            if( r[i]>=qr.l ){
                v = qr.x+i;
//                cout<<v<<endl;
				if(v<=n)pos[v]=1,vis[v]=1,r[v] = max(r[v],qr.r);

            }
		}
//		ch;
    }
    c=0;
    for(int i=1;i<=n;i++)if(pos[i])c++;
    cout<<c<<endl;
    for(int i=1;i<=n;i++)if(pos[i])cout<<i<<" ";NL;

    return 0;
}


#include<bits/stdc++.h>
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define LL long long
#define ULL unsigned long long
LL INF=10000000000000001LL;
#define PI 2.0*acos(0.0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//typedef tree<
//int,
//null_type,
//less<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

#define READ 		freopen("in.txt","r",stdin)
#define WRITE  		freopen("out.txt","w",stdout)
#define BOOST		ios_base::sync_with_stdio(false);cin.tie(NULL)
#define sf 			scanf
#define pf 			printf
#define F  			first
#define S  			second
#define pb 			push_back
#define NL			pf("\n")
#define ALL(v)		v.begin(),v.end()
#define si(x)		sf("%d",&x);
#define sii(x,y)	sf("%d%d",&x,&y);
#define siii(x,y,z)	sf("%d%d%d",&x,&y,&z);
#define sl(x)		sf("%lld",&x);
#define sll(x,y)	sf("%lld%lld",&x,&y);
#define slll(x,y,z)	sf("%lld%lld%lld",&x,&y,&z);
#define clr(x,y) 	memset(x,y,sizeof (x) )
#define ch 			printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define SV(v)		sort(v.begin(),v.end())
#define SVR(v,cmp)	sort(v.begin(),v.end(),cmp)
#define FOR(i,n)	for(int i=0;i<n;i++)
#define cnd			tree[idx]
#define lc			tree[idx*2]
#define rc			tree[idx*2+1]
#define lnd 		(2*idx),(b),((b+e)/2)
#define rnd 		((2*idx)+1),(((b+e)/2)+1),(e)

// graph direction array [4]
LL X[4]={0,0,1,-1};
LL Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

bool Check(int N,int pos){return (bool)(N & (1<<pos));} // check bit
int Set(int N,int pos){	return N=N | (1<<pos);}// set bit

const LL mod=2091573227LL;
LL modMul(LL a, LL b){

//	return (a%mod * b%mod)%mod;
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}

LL powerMod(LL a,LL b){
	if(b==0)return 1%mod;
	LL x=powerMod(a,b/2);
	x = (x%mod*x%mod)%mod;
//	x = modMul( x,x )%mod;
	if(b%2==1){
		x = (x%mod*a%mod)%mod;
//		x = modMul(x,a)%mod;
	}
	return x%mod;
}

LL lcm( LL a,LL b ){
	LL g = __gcd(a,b);
	return (a*b)/g;
}
const LL mx = 10009;
//-------------------------------------------------------------------------------

int pos[mx];
int r[mx];
bool vis[mx];
struct query{
	int l,r,x;
};
vector<query> arr;
bool cmp(query a,query b){return a.l<b.l;}
int main(){
//  READ;
//  WRITE;
//	BOOST;
    int t,c=0;
    int n,k,v,q,l,rr;
	query qr;
    cin>>n>>q;
    FOR(i,q){
		cin>>l>>rr>>v;
		qr.l=l,qr.r=rr,qr.x=v;
		arr.pb(qr);
    }

    sort(arr.begin(),arr.end(),cmp);
    pos[0] = 1;
    r[0] = n+1;
    FOR(j,q){
		qr = arr[j];
		clr(vis,0);
		r[qr.x] = max(r[qr.x],qr.r);
		for(int i=0;i<=n;i++){
//			cout<<i<<" "<<r[i]<<"  "<<qr.x<<endl;
			if( vis[i] )continue;
//			vis[i] = 1;
			if( pos[i]==0 )continue;
            if( r[i]>=qr.l ){
                v = qr.x+i;
//                cout<<v<<endl;
				if(v<=n)pos[v]=1,vis[v]=1,r[v] = max(r[v],min(qr.r,r[i]));

            }
		}
//		ch;
    }
    c=0;
    for(int i=1;i<=n;i++)if(pos[i])c++;
    cout<<c<<endl;
    for(int i=1;i<=n;i++)if(pos[i])cout<<i<<" ";NL;

    return 0;
}


#include<bits/stdc++.h>
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;

#define LL long long
#define ULL unsigned long long
LL INF=10000000000000001LL;
#define PI 2.0*acos(0.0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//typedef tree<
//int,
//null_type,
//less<int>,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;

#define READ 		freopen("in.txt","r",stdin)
#define WRITE  		freopen("out.txt","w",stdout)
#define BOOST		ios_base::sync_with_stdio(false);cin.tie(NULL)
#define sf 			scanf
#define pf 			printf
#define F  			first
#define S  			second
#define pb 			push_back
#define NL			pf("\n")
#define ALL(v)		v.begin(),v.end()
#define si(x)		sf("%d",&x);
#define sii(x,y)	sf("%d%d",&x,&y);
#define siii(x,y,z)	sf("%d%d%d",&x,&y,&z);
#define sl(x)		sf("%lld",&x);
#define sll(x,y)	sf("%lld%lld",&x,&y);
#define slll(x,y,z)	sf("%lld%lld%lld",&x,&y,&z);
#define clr(x,y) 	memset(x,y,sizeof (x) )
#define ch 			printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define SV(v)		sort(v.begin(),v.end())
#define SVR(v,cmp)	sort(v.begin(),v.end(),cmp)
#define FOR(i,n)	for(int i=0;i<n;i++)
#define cnd			tree[idx]
//#define lc			tree[idx*2]
//#define rc			tree[idx*2+1]
#define lnd 		(2*idx),(b),((b+e)/2)
#define rnd 		((2*idx)+1),(((b+e)/2)+1),(e)

// graph direction array [4]
//LL X[4]={0,0,1,-1};
//LL Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};

bool Check(int N,int pos){return (bool)(N & (1<<pos));} // check bit
int Set(int N,int pos){	return N=N | (1<<pos);}// set bit

const LL mod=2091573227LL;
LL modMul(LL a, LL b){

//	return (a%mod * b%mod)%mod;
    LL ans = 0;
    a = a % mod;
    while (b > 0){
        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
        a = (a%mod * 2%mod) % mod;
        b /= 2;
    }
    return ans % mod;
}

LL powerMod(LL a,LL b){
	if(b==0)return 1%mod;
	LL x=powerMod(a,b/2);
	x = (x%mod*x%mod)%mod;
//	x = modMul( x,x )%mod;
	if(b%2==1){
		x = (x%mod*a%mod)%mod;
//		x = modMul(x,a)%mod;
	}
	return x%mod;
}

LL lcm( LL a,LL b ){
	LL g = __gcd(a,b);
	return (a*b)/g;
}
const int mx = 100003;
//-------------------------------------------------------------------------------

int last[10005];
struct node{
	int l,r,x;
};
vector<node> Q;

bool cmp(node a,node b){
//	if(a.l==b.l)return a.r<b.r;
	return a.l<b.l;
}

int main(){
//  READ;
//  WRITE;
	BOOST;
    int t,m,n,k,u,v;
	cin>>n;
	cin>>m;
	node nd;
	FOR(i,m){
		cin>>u>>v>>k;
		nd.l = u,nd.r = v,nd.x = k;
		Q.pb(nd);
	}
	sort(Q.begin(),Q.end(),cmp);
	clr(last,0);
	last[0]=n;
	bool vis[10004];
        for(int j=0;j<=n;j++){
    FOR(i,m){
		FOR(j,n+2)vis[j] = 0;
            if(last[j]>=Q[i].l){
				if( j+Q[i].x <= n ){
                    vis[ j+Q[i].x ] = 1;
				}
            }
        }
		for(int j=0;j<=n;j++){
			if(vis[j]){
				last[j] = max(last[j],Q[i].r);
			}
		}
    }
    k=0;
    for(int i=1;i<=n;i++)if(last[i])k++;
    cout<<k<<endl;
    for(int i=1;i<=n;i++)if(last[i])cout<<i<<" ";NL;

    return 0;
}