#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef pair<vi, int> pvii;
const int MAXN = 100005;
int N;
vvi edges[2], levels[2];
int ts[MAXN], label[2][MAXN], parent[2][MAXN];
vi centroid[2];
int findCentroid(const int tID, const int u, const int p) {
int children = 0, curr;
for (auto& e : edges[tID][u]) {
if (e != p) {
curr = findCentroid(tID, e, u);
if (curr > (N >> 1))
break;
children += curr;
}//if
}//for
if (N - children - 1 <= (N >> 1))
centroid[tID].push_back(u);
return ts[u] = children + 1;
}//findCentroid
int setLevels(const int tID, const int u, const int p, const int d) {
parent[tID][u] = p;
levels[tID][d].push_back(u);
int mx = d;
for (auto& e : edges[tID][u])
if (e != p)
mx = max(mx, setLevels(tID, e, u, d + 1));
return mx;
}//setLevels
bool isoCheck(const int lvl) {
for (int it = lvl; it >= 0; it--) {
vector<pvii> order[2];
for (int i = 0; i < 2; i++) {
for (auto& u : levels[i][it]) {
order[i].push_back(pvii(vi(), u));
for (auto& e : edges[i][u])
if (e != parent[i][u])
order[i].back().first.push_back(label[i][e]);
}//for
}//for
if ((int)order[0].size() != ((int)order[1].size()))
return 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < (int)order[0].size(); j++)
sort(order[i][j].first.begin(), order[i][j].first.end());
sort(order[i].begin(), order[i].end());
}//for
int labelID = 0;
for (int i = 0; i < (int)order[0].size(); i++) {
if (order[0][i].first != order[1][i].first)
return 0;
if (i && order[0][i].first == order[0][i - 1].first) {
label[0][order[0][i].second] = label[1][order[1][i].second] = labelID;
continue;
}//if
label[0][order[0][i].second] = label[1][order[1][i].second] = ++labelID;
}//for
}//for
return 1;
}//isoCheck
int main() {
int u, v;
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &N);
memset(ts, 0, sizeof(int) * (N + 2));
for (int i = 0; i < 2; i++) {
edges[i].assign(N + 5, vi());
levels[i].assign(N + 5, vi());
memset(label[i], 0, sizeof(int) * (N + 2));
memset(parent[i], 0, sizeof(int) * (N + 2));
centroid[i].clear();
for (int j = 0; j < N - 1; j++) {
scanf("%d %d", &u, &v);
edges[i][u].push_back(v);
edges[i][v].push_back(u);
}//for
findCentroid(i, edges[i][0].empty() ? 1 : 0, -1);
}//for
if (edges[0][0].empty())
N++;
if ((int)centroid[0].size() != (int)centroid[1].size()) {
puts("NO");
continue;
}//if
if ((int)centroid[0].size() == 2) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
edges[i][centroid[i][j]].erase(std::remove(edges[i][centroid[i][j]].begin(), edges[i][centroid[i][j]].end(), centroid[i][!j]), edges[i][centroid[i][j]].end());
edges[i][centroid[i][j]].push_back(N);
edges[i][N].push_back(centroid[i][j]);
}//for
centroid[i][0] = N;
}//for
}//if
int d[2];
for (int i = 0; i < 2; i++)
d[i] = setLevels(i, centroid[i][0], -1, 0);
if (d[0] != d[1]) {
puts("NO");
continue;
}//if
if (d[0] >= 0)
isoCheck(d[0] - 1) ? puts("YES") : puts("NO");
}//while
return 0;
}//main
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgdmVjdG9yPHZlY3RvcjxpbnQ+PiB2dmk7CnR5cGVkZWYgdmVjdG9yPGludD4gdmk7CnR5cGVkZWYgcGFpcjx2aSwgaW50PiBwdmlpOwpjb25zdCBpbnQgTUFYTiA9IDEwMDAwNTsKCmludCBOOwp2dmkgZWRnZXNbMl0sIGxldmVsc1syXTsKaW50IHRzW01BWE5dLCBsYWJlbFsyXVtNQVhOXSwgcGFyZW50WzJdW01BWE5dOwp2aSBjZW50cm9pZFsyXTsKCmludCBmaW5kQ2VudHJvaWQoY29uc3QgaW50IHRJRCwgY29uc3QgaW50IHUsIGNvbnN0IGludCBwKSB7CiAgIGludCBjaGlsZHJlbiA9IDAsIGN1cnI7CiAgIGZvciAoYXV0byYgZSA6IGVkZ2VzW3RJRF1bdV0pIHsKICAgICAgaWYgKGUgIT0gcCkgew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gICAgICBvcmRlcltpXS5iYWNrKCkuZmlyc3QucHVzaF9iYWNrKGxhYmVsW2ldW2VdKTsKICAgICAgICAgfS8vZm9yCiAgICAgIH0vL2ZvcgogICAgICBpZiAoKGludClvcmRlclswXS5zaXplKCkgIT0gKChpbnQpb3JkZXJbMV0uc2l6ZSgpKSkKICAgICAgICAgcmV0dXJuIDA7CiAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgMjsgaSsrKSB7CiAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgKGludClvcmRlclswXS5zaXplKCk7IGorKykKICAgICAgICAgICAgc29ydChvcmRlcltpXVtqXS5maXJzdC5iZWdpbigpLCBvcmRlcltpXVtqXS5maXJzdC5lbmQoKSk7CiAgICAgICAgIHNvcnQob3JkZXJbaV0uYmVnaW4oKSwgb3JkZXJbaV0uZW5kKCkpOwogICAgICB9Ly9mb3IKICAgICAgaW50IGxhYmVsSUQgPSAwOwogICAgICBmb3IgKGludCBpID0gMDsgaSA8IChpbnQpb3JkZXJbMF0uc2l6ZSgpOyBpKyspIHsKICAgICAgICAgaWYgKG9yZGVyWzBdW2ldLmZpcnN0ICE9IG9yZGVyWzFdW2ldLmZpcnN0KQogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICAgaWYgKGkgJiYgb3JkZXJbMF1baV0uZmlyc3QgPT0gb3JkZXJbMF1baSAtIDFdLmZpcnN0KSB7CiAgICAgICAgICAgIGxhYmVsWzBdW29yZGVyWzBdW2ldLnNlY29uZF0gPSBsYWJlbFsxXVtvcmRlclsxXVtpXS5zZWNvbmRdID0gbGFiZWxJRDsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgIH0vL2lmCiAgICAgICAgIGxhYmVsWzBdW29yZGVyWzBdW2ldLnNlY29uZF0gPSBsYWJlbFsxXVtvcmRlclsxXVtpXS5zZWNvbmRdID0gKytsYWJlbElEOwogICAgICB9Ly9mb3IKICAgfS8vZm9yCiAgIHJldHVybiAxOwp9Ly9pc29DaGVjawoKaW50IG1haW4oKSB7CiAgIGludCB1LCB2OwogICBpbnQgVDsKICAgc2NhbmYoIiVkIiwgJlQpOwogICB3aGlsZSAoVC0tKSB7CiAgICAgIHNjYW5mKCIlZCIsICZOKTsKICAgICAgbWVtc2V0KHRzLCAwLCBzaXplb2YoaW50KSAqIChOICsgMikpOwogICAgICBmb3IgKGludCBpID0gMDsgaSA8IDI7IGkrKykgewogICAgICAgICBlZGdlc1tpXS5hc3NpZ24oTiArIDUsIHZpKCkpOwogICAgICAgICBsZXZlbHNbaV0uYXNzaWduKE4gKyA1LCB2aSgpKTsKICAgICAgICAgbWVtc2V0KGxhYmVsW2ldLCAwLCBzaXplb2YoaW50KSAqIChOICsgMikpOwogICAgICAgICBtZW1zZXQocGFyZW50W2ldLCAwLCBzaXplb2YoaW50KSAqIChOICsgMikpOwogICAgICAgICBjZW50cm9pZFtpXS5jbGVhcigpOwogICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IE4gLSAxOyBqKyspIHsKICAgICAgICAgICAgc2NhbmYoIiVkICVkIiwgJnUsICZ2KTsKICAgICAgICAgICAgZWRnZXNbaV1bdV0ucHVzaF9iYWNrKHYpOwogICAgICAgICAgICBlZGdlc1tpXVt2XS5wdXNoX2JhY2sodSk7CiAgICAgICAgIH0vL2ZvcgogICAgICAgICBmaW5kQ2VudHJvaWQoaSwgZWRnZXNbaV1bMF0uZW1wdHkoKSA/IDEgOiAwLCAtMSk7CiAgICAgIH0vL2ZvcgogICAgICBpZiAoZWRnZXNbMF1bMF0uZW1wdHkoKSkKICAgICAgICAgTisrOwogICAgICBpZiAoKGludCljZW50cm9pZFswXS5zaXplKCkgIT0gKGludCljZW50cm9pZFsxXS5zaXplKCkpIHsKICAgICAgICAgcHV0cygiTk8iKTsKICAgICAgICAgY29udGludWU7CiAgICAgIH0vL2lmCiAgICAgIGlmICgoaW50KWNlbnRyb2lkWzBdLnNpemUoKSA9PSAyKSB7CiAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgMjsgaSsrKSB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgMjsgaisrKSB7CiAgICAgICAgICAgICAgIGVkZ2VzW2ldW2NlbnRyb2lkW2ldW2pdXS5lcmFzZShzdGQ6OnJlbW92ZShlZGdlc1tpXVtjZW50cm9pZFtpXVtqXV0uYmVnaW4oKSwgZWRnZXNbaV1bY2VudHJvaWRbaV1bal1dLmVuZCgpLCBjZW50cm9pZFtpXVshal0pLCBlZGdlc1tpXVtjZW50cm9pZFtpXVtqXV0uZW5kKCkpOwogICAgICAgICAgICAgICBlZGdlc1tpXVtjZW50cm9pZFtpXVtqXV0ucHVzaF9iYWNrKE4pOwogICAgICAgICAgICAgICBlZGdlc1tpXVtOXS5wdXNoX2JhY2soY2VudHJvaWRbaV1bal0pOwogICAgICAgICAgICB9Ly9mb3IKICAgICAgICAgICAgY2VudHJvaWRbaV1bMF0gPSBOOwogICAgICAgICB9Ly9mb3IKICAgICAgfS8vaWYKICAgICAgaW50IGRbMl07CiAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgMjsgaSsrKQogICAgICAgICBkW2ldID0gc2V0TGV2ZWxzKGksIGNlbnRyb2lkW2ldWzBdLCAtMSwgMCk7CiAgICAgIGlmIChkWzBdICE9IGRbMV0pIHsKICAgICAgICAgcHV0cygiTk8iKTsKICAgICAgICAgY29udGludWU7CiAgICAgIH0vL2lmCiAgICAgIGlmIChkWzBdID49IDApCiAgICAgICAgIGlzb0NoZWNrKGRbMF0gLSAxKSA/IHB1dHMoIllFUyIpIDogcHV0cygiTk8iKTsKICAgfS8vd2hpbGUKICAgcmV0dXJuIDA7Cn0vL21haW4K