#include <bits/stdc++.h>
using namespace std;
void solve(map<int, vector<int>> mp, map<int, int>& cnt, int p) {
  if (mp.find(p) == mp.end()) {
    cnt[p] = 1;
    return;
  }
  for (int i = 0; i < mp[p].size(); i++) solve(mp, cnt, mp[p][i]);
  cnt[p] = 1;
  for (int i = 0; i < mp[p].size(); i++) cnt[p] *= (cnt[mp[p][i]] + 1);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    map<int, vector<int>> mp;
    int root = 0;
    set<int> s;
    for (int i = 1; i < n; i++) {
      int u, v;
      cin >> u >> v;
      s.insert(u);
      s.insert(v);
      root ^= v;
      mp[u].push_back(v);
    }
    for (auto x : s) root ^= x;
    map<int, int> cnt;
    solve(mp, cnt, root);
    int c = 0;
    for (auto x : cnt) c += x.second;
    cout << c << endl;
  }
  return 0;
}
