int t, n, st, ed; int edgeCount; int head[MAXN], toNode[MAXN], nextEdge[MAXN]; LL l[MAXN], r[MAXN], f[MAXN], maxVal[MAXN];
inlineintread(){ int x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x; } inlinevoidaddEdge(int from, int to){ edgeCount++; toNode[edgeCount] = to; nextEdge[edgeCount] = head[from]; head[from] = edgeCount; return; } inlinevoiddfs(int from){ bool isLeaf = true; for (int i = head[from]; i; i = nextEdge[i]) { isLeaf = false; dfs(toNode[i]); maxVal[from] += maxVal[toNode[i]]; f[from] += f[toNode[i]]; } if (isLeaf) f[from] = 1, maxVal[from] = r[from]; if (maxVal[from] < l[from]) f[from]++, maxVal[from] = r[from]; maxVal[from] = min(maxVal[from], r[from]); return; }
intmain(){ t = read(); while (t--) { n = read(); st = ed + 1, ed = ed + n; for (int i = st + 1; i <= ed; ++i) addEdge(st - 1 + read(), i); for (int i = st; i <= ed; ++i) l[i] = read(), r[i] = read(); dfs(st); cout << f[st] << '\n'; } return0; }