#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e12 + 7;
int n, t, x, y;
vector<int> Edge[N];
int Hash(int x, int f, int t) {
if (t == 1) {
map <int, int> s;
for (int i : Edge[x])
if (i != f) {
int h = Hash(i, x, 0);
if (s[h])
s.erase(h);
else
s[h] = i;
}
if (s.size() >= 2) return 0;
if (s.size() == 0) return 1;
return Hash(s.begin()->second, x, 1);
} else {
int ans = N;
vector <int> q;
for (int i : Edge[x])
if (i != f)
q.push_back(Hash(i, x, 0));
sort(q.begin(), q.end());
for (int i : q) ans = (ans * 2333 + i) % mod;
return (ans * 2333 * N + 1) % mod;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) Edge[i].clear();
for (int i = 1; i < n; i++) {
cin >> x >> y;
Edge[x].push_back(y);
Edge[y].push_back(x);
}
cout << (Hash(1, 0, 1) ? "YES\n" : "NO\n");
}
return 0;
}