#include <bits/stdc++.h>
using namespace std;
void fast(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
const int N = 200005;
vector<int> adj[N];
int vis[N];
int in[N];
bool bad = false;
void dfs(int node){
vis[node] = 1;
int cnt = 0;
for(auto ch : adj[node]){
if(vis[ch] == 0){
cnt++;
}
}
if(cnt > 2) bad = true;
for(auto ch : adj[node]){
if(vis[ch] == 0){
dfs(ch);
}
}
}
int main(){
fast();
int t;
cin >> t;
while(t--){
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
adj[i].clear();
vis[i] = 0;
in[i] = 0;
}
bad = false;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
in[u]++;
in[v]++;
}
int leaf = 0;
for (int i = 1; i <= n; ++i) {
if(in[i] == 1){
leaf = i;
break;
}
}
dfs(leaf);
if(bad){
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIGZhc3QoKXsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShudWxscHRyKTsKICAgIGNvdXQudGllKG51bGxwdHIpOwp9Cgpjb25zdCBpbnQgTiA9IDIwMDAwNTsKdmVjdG9yPGludD4gYWRqW05dOwppbnQgdmlzW05dOwppbnQgaW5bTl07CmJvb2wgYmFkID0gZmFsc2U7Cgp2b2lkIGRmcyhpbnQgbm9kZSl7CiAgICB2aXNbbm9kZV0gPSAxOwogICAgaW50IGNudCA9IDA7CiAgICBmb3IoYXV0byBjaCA6IGFkaltub2RlXSl7CiAgICAgICAgaWYodmlzW2NoXSA9PSAwKXsKICAgICAgICAgICAgY250Kys7CiAgICAgICAgfQogICAgfQogICAgaWYoY250ID4gMikgYmFkID0gdHJ1ZTsKICAgIGZvcihhdXRvIGNoIDogYWRqW25vZGVdKXsKICAgICAgICBpZih2aXNbY2hdID09IDApewogICAgICAgICAgICBkZnMoY2gpOwogICAgICAgIH0KICAgIH0KfQoKaW50IG1haW4oKXsKICAgIGZhc3QoKTsKICAgIGludCB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSh0LS0pewogICAgICAgIGludCBuOwogICAgICAgIGNpbiA+PiBuOwoKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpIHsKICAgICAgICAgICAgYWRqW2ldLmNsZWFyKCk7CiAgICAgICAgICAgIHZpc1tpXSA9IDA7CiAgICAgICAgICAgIGluW2ldID0gMDsKICAgICAgICB9CiAgICAgICAgYmFkID0gZmFsc2U7CgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbiAtIDE7ICsraSkgewogICAgICAgICAgICBpbnQgdSwgdjsKICAgICAgICAgICAgY2luID4+IHUgPj4gdjsKICAgICAgICAgICAgYWRqW3VdLnB1c2hfYmFjayh2KTsKICAgICAgICAgICAgYWRqW3ZdLnB1c2hfYmFjayh1KTsKICAgICAgICAgICAgaW5bdV0rKzsKICAgICAgICAgICAgaW5bdl0rKzsKICAgICAgICB9CgogICAgICAgIGludCBsZWFmID0gMDsKCiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgKytpKSB7CiAgICAgICAgICAgIGlmKGluW2ldID09IDEpewogICAgICAgICAgICAgICAgbGVhZiA9IGk7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAKICAgICAgICBkZnMobGVhZik7CiAgICAgICAgCiAgICAgICAgaWYoYmFkKXsKICAgICAgICAgICAgY291dCA8PCAiTk8iIDw8IGVuZGw7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY291dCA8PCAiWUVTIiA8PCBlbmRsOwogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gMDsKfQ==