Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

You are given a rooted tree consisting of N nodes. The nodes are numbered from 1 to N, and node 1 is the root. At each node of the tree, you can put zero or one coin such that the following property is satisfied for the tree:

For each node of the tree, starting from the original configuration, we should be able to get two coins on the node by applying at most two operations of the following kind:
Take a coin from a node and move it to an adjacent node.
  
While trying to get to a particular node, if the same coin is moved in two operations, both those operations should either be towards the root, or both of them should be away from the root. That is, you cannot move a coin to its parent in one operation and then take it to another child of the parent in the second operation.

Find the minimum total number of coins which can be used to get a valid configuration.

------ Input ------ 

The first line of the input contains an integer T.
For each test case, the first line contains an integer N.
Each of the next N - 1 lines, contains two space separated integers u, v, denoting that there is an edge between node u and node v of the tree.

------ Output ------ 

For each test case, output a single integer corresponding to the minimum number of coins that can be used. If it's not possible to achieve this, output -1.

------ Constraints ------ 

$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$1 ≤  u, v ≤ N$

------ Subtasks ------ 

Subtask #1 (40 points)

$1 ≤ N ≤ 1000$

Subtask #2 (60 points)

$Original constraints$

----- Sample Input 1 ------ 
2

3

1 2

1 3

5

1 2

1 3

3 4

3 5
----- Sample Output 1 ------ 
3

4
----- explanation 1 ------ 
Example case 1. 

The nodes which have a coin in them are shown by blue color. You can see that each node satisfies the desired property.

Example case 2.

Difficulty: VERY_HARD
Tags: Tree algorithms

Graph algorithms

Data structures

Dynamic programming
Skill types: Dynamic programming

Data structures

Starter code:

Solutions:
def main():
	from sys import stdin, stderr
	from collections import deque

	def findout(graph):
		if len(graph) <= 2:
			return 2

		def bfs(graph):
			N = len(graph)
			q = deque([0])
			visit = []
			parent = [-1] * N
			while q:
				v = q.popleft()
				visit += [v]
				for adj in graph[v]:
					if adj != parent[v]:
						q.append(adj)
						parent[adj] = v
			return (parent, visit)
		N = len(graph)
		(parent, visit) = bfs(graph)
		bfsvisit = visit[::-1]
		can1 = [0] * N
		can2 = [0] * N
		coin = [0] * N
		for x in bfsvisit:
			if parent[x] != -1:
				if len(graph[x]) == 1:
					coin[x] = 1
					can2[x] = 0
					can1[x] = 1
				else:
					need = 0
					count = 0
					for y in graph[x]:
						if need == 1:
							break
						if y != parent[x]:
							if coin[y] == 0:
								if can2[y] == 0:
									need = 1
									break
								else:
									for z in graph[y]:
										if z != parent[y]:
											if coin[z] == 1:
												if can2[z] == 0:
													need = 1
													break
					if need == 1:
						coin[x] = 1
						count = 0
						for y in graph[x]:
							if y != parent[x]:
								if coin[y] == 1:
									count = 1
									break
						can2[x] = 1
					else:
						count = 0
						for y in graph[x]:
							if y != parent[x]:
								if coin[y] == 1:
									count += 1
						if count == 1:
							coin[x] = 0
							can1[x] = 1
							can2[x] = 0
						elif count >= 2:
							coin[x] = 0
							can2[x] = 1
							can1[x] = 1
						else:
							coin[x] = 1
							can2[x] = 1
							can1[x] = 1
			elif len(graph[x]) == 1:
				coin[x] = 1
				can2[x] = 0
				can1[x] = 1
			else:
				need = 0
				count = 0
				for y in graph[x]:
					if need == 1:
						break
					if y != parent[x]:
						if coin[y] == 0:
							if can2[y] == 0:
								need = 1
								break
							else:
								for z in graph[y]:
									if z != parent[y]:
										if coin[z] == 1:
											if can2[z] == 0:
												need = 1
												break
						elif can2[y] == 0:
							need = 1
				if need == 1:
					coin[x] = 1
				else:
					count = 0
					for y in graph[x]:
						if coin[y] == 1:
							count += 1
					if count > 1:
						coin[x] = 0
					else:
						coin[x] = 1
		return sum(coin)
	T = int(input())
	for _ in range(T):
		N = int(input())
		graph = [[] for i in range(N)]
		for _ in range(N - 1):
			(u, v) = map(int, stdin.readline().strip().split())
			(u, v) = (u - 1, v - 1)
			graph[u] += [v]
			graph[v] += [u]
		if N > 1:
			print(findout(graph))
		else:
			print(-1)
main()