1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define vec vector
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

#define yare cout<<"TAK\n";

#define nare cout<<"NIE\n";

const int inf=1e18;

void slv(){
	int n,q;
	cin>>n>>q;

	vec<vec<pii>> adj(n);
	rep(i,n-1){
		int u,v,w;
		cin>>u>>v>>w;
		u--,v--;
		adj[u].pb({w,v});
		adj[v].pb({w,u});
	}

	bool fnd=0;
	vec<vi> dp(n,vi(8,-inf));
	auto gap=[&](){
		fnd=0;
		rep(i,n){
			rep(msk,1<<3){
				dp[i][msk]=-inf;
			}
			dp[i][0]=0;
		}
	};

	int num=0;
	vi usd(n,0),chs(n,0);
	auto refill=[&](auto self,int v,int par)->void{
		num+=1;
		chs[v]=1;
		for(auto [w,u]:adj[v]){
			if(u==par or usd[u]) continue;
			self(self,u,v);
			chs[v]+=chs[u];
		}
	};
	auto find_centroid=[&](auto self,int v,int par)->int{
		// print(v);
		for(auto [w,u]:adj[v]){
			if(u==par or usd[u]) continue;
			// print(chs[u]);
			if(chs[u]*2>num){
				return self(self,u,v);
			}
		}
		return v;
	};
	refill(refill,0,-1);
	int rt=find_centroid(find_centroid,0,-1);

	vi par(n,-1);
	vi ord;
	auto dfs=[&](auto self,int v)->void{
		for(auto [w,u]:adj[v]){
			if(u==par[v]) continue;
			par[u]=v;
			self(self,u);
		}
		ord.pb(v);
	};
	dfs(dfs,rt);

	vi a(3);
	vi sum(1<<3);
	auto work=[&](){
		vec<vi> f(1<<3,vi(2,-inf));
		auto nf=f;

		bool debug=0;
		auto push=[&](int msk1,int w,int type,int pass=0){
			rep(msk,1<<3){
				int now=sum[msk];
				// print(now);
				if(w>=now){
					int new_msk=msk|msk1;
					int nw=w-now;
					if(type==1) nw=0;
					// if(debug) print(new_msk,nw,pass,type);
					nf[new_msk][type]=max(nf[new_msk][type],nw);
					if(type==0){
						nf[new_msk][type]=max(nf[new_msk][type],pass);
					}
				}
			}
			debug=0;
		};

		int lst=n;
		rep(i,n){
			int v=ord[i];
			rep(msk,1<<3){
				rep(t,2){
					f[msk][t]=-inf;
					nf[msk][t]=-inf;
				}
			}
			f[0][0]=0;
			
			for(auto [w,u]:adj[v]){
				if(u==par[v]) continue;
				nf=f;

				rep(msk,1<<3){
					rep(msk1,1<<3){
						if(dp[u][msk1]<0) continue;
						// don't merge and go up
						int new_msk=msk|msk1;
						if(f[msk][0]>=0){
							nf[new_msk][0]=max({nf[new_msk][0],f[msk][0],dp[u][msk1]+w});
						}
						// don't merge and don't go up: create new mask
						if(f[msk][0]>=0){
							// if(v==0 and u==4 and msk==0 and msk1==0) debug=1;
							push(new_msk,dp[u][msk1]+w,0,f[msk][0]); // over all masks satisfied lets update nf
						}

						// merged and don't go up: create new mask
						if(f[msk][1]>=0){
							push(new_msk,dp[u][msk1]+w,1);
						}

						// merged and don't go up: don't create anything
						if(f[msk][1]>=0){
							nf[new_msk][1]=0;
						}

						// merge
						if(f[msk][0]>=0){
							push(new_msk,f[msk][0]+dp[u][msk1]+w,1);
						}
					}
				}
				// if(v==0 and u==1) print(nf[0][0]);
				// if(v==0 and u==4) print(nf[2][0]);
				// if(v==1 and (nf[7][0] or nf[7][1]))  print(u,"wtf");
				f.swap(nf);
			}
			rep(msk,1<<3){
				dp[v][msk]=max(f[msk][0],f[msk][1]);
			}
			if(dp[v][7]>=0){
				lst=i+1;
				fnd=1;
				break;
			}
		}

		rep(i,lst){
			int v=ord[i];
			rep(msk,1<<3){
				dp[v][msk]=-inf;
			}
			dp[v][0]=0;
		}
	};

	auto debug=[&](){
		rep(i,n){
			cout<<"debugging for vertex : "<<i+1<<" {\n";
			rep(msk,1<<3){
				cout<<" msk = "<<msk<<" "<<dp[i][msk]<<"\n";
			}
			cout<<"} \n";
			cout<<"~ ~ ~\n";
		}
	};

	rep(iter,q){
		rep(j,3){
			cin>>a[j];
		}
		sum=vi(1<<3);
		rep(msk,1<<3){
			rep(j,3){
				if(msk>>j&1){
					// print(a[j]);
					sum[msk]+=a[j];
				}
			}
		}
		// gap();
		fnd=0;
		
		work();
		
		// debug();

		if(fnd){
			yare;
		}else{
			nare;
		}
	}
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0);

	int t;
	t=1;
	// cin>>t;

	rep(cs,t){
		slv();
	}
}