pair
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int, int>
#define MP make_pair
#define ls i << 1
#define rs ls | 1
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 100010
#define M 200020
int fst[N], vv[M], nxt[M], e;
pii p[N][3];
int n;
pii operator + (const pii &a, const pii &b){
return MP(a.first + b.first, a.second + b.second);
}
void init(){
memset(fst, -1, sizeof fst); e = 0;
}
void add(int u, int v){
vv[e] = v, nxt[e] = fst[u], fst[u] = e++;
}
void dfs(int u, int fa){
for(int i = 0; i < 3; ++i) p[u][i] = MP(0, 0);
pii tmp[3];
for(int i = fst[u]; ~i; i = nxt[i]){
int v = vv[i];
if(v == fa) continue;
dfs(v, u);
for(int j = 0; j < 3; ++j) tmp[j] = MP(inf, inf);
tmp[0] = min(tmp[0], p[u][1] + p[v][1] + MP(1, 1));
tmp[0] = min(tmp[0], p[u][2] + p[v][2] + MP(2, 2));
tmp[1] = min(tmp[1], tmp[0]);
tmp[1] = min(tmp[1], p[u][0] + p[v][1] + MP(0, 1));
tmp[1] = min(tmp[1], p[u][1] + p[v][2] + MP(1, 2));
tmp[1] = min(tmp[1], p[u][2] + p[v][1] + MP(1, 1));
tmp[2] = min(tmp[1], tmp[2]);
tmp[2] = min(tmp[2], p[u][0] + p[v][2] + MP(0, 2));
tmp[2] = min(tmp[2], p[u][1] + p[v][1] + MP(0, 1));
tmp[2] = min(tmp[2], p[u][2] + p[v][2] + MP(1, 2));
for(int j = 0; j < 3; ++j)
p[u][j] = tmp[j];
}
}
int main(){
int cas;
scanf("%d", &cas);
while(cas--){
scanf("%d", &n);
init();
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(1, -1);
printf("%d\n", p[1][0].second);
}
return 0;
}