package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key, Val int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key, val int) {
	x := &Node{
		Key:  key,
		Val:  val,
		size: 1,
		t:    rand.Int(),
	}
	t.insertNode(x)
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v=%v, ", x.Key, x.Val)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) ForEach(f func(x *Node)) {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		dfs(x.r)
		x.l, x.r, x.size = nil, nil, 1
		f(x)
	}
	dfs(t.root)
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	reader := bufio.NewReader(os.Stdin)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	c := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	vout := make([][]int, n)
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, vout[i])
	//	}

	qs := make([]*quest, 0)
	qout := make([][]*quest, n)
	for i := 0; i < m; i++ {
		var v, k int
		fmt.Fscanf(reader, "%d %d\n", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, qout[i])
	//	}
	//	for _, q := range qs {
	//		fmt.Println(q)
	//	}

	merge1 := func(a, b, t2 *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			x := a.Find(y.Key)
			if x != nil {
				t2.Erase(x.Val)
				t2.Erase(y.Val)
				x.Val = x.Val + y.Val
				t2.Insert(x.Val, 0)
			} else {
				a.insertNode(y)
			}
		})
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			a.insertNode(y)
		})
		return a
	}

	visited := make([]int, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = 1
		t1 = New()
		t2 = New()
		t1.Insert(c[u], 1)
		t2.Insert(1, 0)
		for _, v := range vout[u] {
			if visited[v] == 0 {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
				visited[u] += visited[v]
			}
		}
		//		fmt.Println("post", u)
		//		t1.Print()
		//		t2.Print()
		for _, q := range qout[u] {
			if q.k <= visited[u] {
				q.ans = t2.Size() - t2.Count(q.k)
			}
		}
		return
	}

	dfs(0)

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	for _, q := range qs {
		fmt.Fprintln(writer, q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

var (
	nodes [100010]Node
	free []*Node
)

func init() {
	for i := range nodes {
		free = append(free, &nodes[i])
	}
}

type Node struct {
	l, r, p  *Node
	size, t  int
	Key int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key int) {
	x := free[len(free) - 1]
	free = free[:len(free) - 1]
	x.Key = key
	x.size = 1
	x.t = rand.Int()
	x.l, x.r, x.p = nil, nil, nil
	t.insertNode(x)
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	free = append(free, x)
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v, ", x.Key)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) ForEach(f func(x *Node)) {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		dfs(x.r)
		x.l, x.r, x.size = nil, nil, 1
		f(x)
	}
	dfs(t.root)
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	reader := bufio.NewReader(os.Stdin)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	c := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	vout := make([][]int, n)
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}

	qs := make([]quest, m)
	qout := make([][]*quest, n)
	for i := 0; i < m; i++ {
		var v int
		q := &qs[i]
		fmt.Fscanf(reader, "%d %d\n", &v, &q.k)
		v--
		qout[v] = append(qout[v], q)
	}

	merge1 := func(a, b map[int]int, t2 *Treap) map[int]int {
		if len(a) < len(b) {
			a, b = b, a
		}
		for k, vb := range b {
			if va, ok := a[k]; ok {
				t2.Erase(va)
				t2.Erase(vb)
				t2.Insert(va + vb)
				a[k] = va + vb
			} else {
				a[k] = vb
			}
		}
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			a.insertNode(y)
		})
		return a
	}

	visited := make([]int, n)
	var dfs func(u int) (t1 map[int]int, t2 *Treap)
	dfs = func(u int) (t1 map[int]int, t2 *Treap) {
		visited[u] = 1
		t1 = map[int]int{c[u]: 1}
		t2 = New()
		t2.Insert(1)
		for _, v := range vout[u] {
			if visited[v] == 0 {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
				visited[u] += visited[v]
			}
		}
		for _, q := range qout[u] {
			if q.k <= visited[u] {
				q.ans = t2.Size() - t2.Count(q.k)
			}
		}
		return
	}

	dfs(0)

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	for _, q := range qs {
		fmt.Fprintln(writer, q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key, Val int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key, val int) {
	x := &Node{
		Key:  key,
		Val:  val,
		size: 1,
		t:    rand.Int(),
	}
	t.insertNode(x)
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v=%v, ", x.Key, x.Val)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) ForEach(f func(x *Node)) {
	var traverse func(x *Node)
	traverse = func(x *Node) {
		if x == nil {
			return
		}
		traverse(x.l)
		traverse(x.r)
		x.l, x.r, x.size = nil, nil, 1
		f(x)
	}
	traverse(t.root)
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	const maxn = 100010

	var (
		n, m int
		c    [maxn]int
		vout [maxn][]int
		qs   []*quest
		qout [maxn][]*quest
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, vout[i])
	//	}

	for i := 0; i < m; i++ {
		var v, k int
		fmt.Fscanf(reader, "%d %d\n", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, qout[i])
	//	}
	//	for _, q := range qs {
	//		fmt.Println(q)
	//	}

	merge1 := func(a, b, t2 *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			x := a.Find(y.Key)
			if x != nil {
				t2.Erase(x.Val)
				t2.Erase(y.Val)
				x.Val = x.Val + y.Val
				t2.Insert(x.Val, 0)
			} else {
				a.insertNode(y)
			}
		})
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			a.insertNode(y)
		})
		return a
	}

	visited := make([]int, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = 1
		t1 = New()
		t2 = New()
		t1.Insert(c[u], 1)
		t2.Insert(1, 0)
		for _, v := range vout[u] {
			if visited[v] == 0 {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
				visited[u] += visited[v]
			}
		}
		//		fmt.Println("post", u)
		//		t1.Print()
		//		t2.Print()
		for _, q := range qout[u] {
			if q.k <= visited[u] {
				q.ans = t2.Size() - t2.Count(q.k)
			}
		}
		return
	}

	dfs(0)

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	for _, q := range qs {
		fmt.Fprintln(writer, q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"unsafe"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key, Val int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func hash(x *Node) uint64 {
	z := uintptr(unsafe.Pointer(x))
	h := uint64(0)
	for i := 0; i < 8; i++ {
		h ^= ((h << 5) + uint64(z&0xff) + (h >> 2))
		z >>= 8
	}
	return h
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key, val int) {
	x := &Node{
		Key:  key,
		Val:  val,
		size: 1,
		t:    rand.Int(),
	}
	t.insertNode(x)
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v=%v, ", x.Key, x.Val)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	const maxn = 100010

	var (
		n, m int
		c    [maxn]int
		vout [maxn][]int
		qs   []*quest
		qout [maxn][]*quest
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, vout[i])
	//	}

	for i := 0; i < m; i++ {
		var v, k int
		fmt.Fscanf(reader, "%d %d\n", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, qout[i])
	//	}
	//	for _, q := range qs {
	//		fmt.Println(q)
	//	}

	merge1 := func(a, b, t2 *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		var f func(y *Node)
		f = func(y *Node) {
			if y == nil {
				return
			}
			l, r := y.l, y.r
			y.l, y.r, y.size = nil, nil, 1
			x := a.Find(y.Key)
			if x != nil {
				t2.Erase(x.Val)
				t2.Erase(y.Val)
				x.Val = x.Val + y.Val
				t2.Insert(x.Val, 0)
			} else {
				a.insertNode(y)
			}
			f(l)
			f(r)
		}
		f(b.root)
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		var f func(y *Node)
		f = func(y *Node) {
			if y == nil {
				return
			}
			l, r := y.l, y.r
			y.l, y.r, y.size = nil, nil, 1
			a.insertNode(y)
			f(l)
			f(r)
		}
		f(b.root)
		return a
	}

	visited := make([]int, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = 1
		t1 = New()
		t2 = New()
		t1.Insert(c[u], 1)
		t2.Insert(1, 0)
		for _, v := range vout[u] {
			if visited[v] == 0 {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
				visited[u] += visited[v]
			}
		}
		//		fmt.Println("post", u)
		//		t1.Print()
		//		t2.Print()
		for _, q := range qout[u] {
			if q.k <= visited[u] {
				q.ans = t2.Size() - t2.Count(q.k)
			}
		}
		return
	}

	dfs(0)

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	for _, q := range qs {
		fmt.Fprintln(writer, q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key int) {
	x := &Node{
		Key:  key,
		size: 1,
		t:    rand.Int(),
	}
	t.insertNode(x)
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v, ", x.Key)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) ForEach(f func(x *Node)) {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		dfs(x.r)
		x.l, x.r, x.size = nil, nil, 1
		f(x)
	}
	dfs(t.root)
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	reader := bufio.NewReader(os.Stdin)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	c := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	vout := make([][]int, n)
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}

	qs := make([]quest, m)
	qout := make([][]*quest, n)
	for i := 0; i < m; i++ {
		var v int
		q := &qs[i]
		fmt.Fscanf(reader, "%d %d\n", &v, &q.k)
		v--
		qout[v] = append(qout[v], q)
	}

	merge1 := func(a, b map[int]int, t2 *Treap) map[int]int {
		if len(a) < len(b) {
			a, b = b, a
		}
		for k, vb := range b {
			if va, ok := a[k]; ok {
				t2.Erase(va)
				t2.Erase(vb)
				t2.Insert(va + vb)
				a[k] = va + vb
			} else {
				a[k] = vb
			}
		}
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			a.insertNode(y)
		})
		return a
	}

	visited := make([]int, n)
	var dfs func(u int) (t1 map[int]int, t2 *Treap)
	dfs = func(u int) (t1 map[int]int, t2 *Treap) {
		visited[u] = 1
		t1 = map[int]int{c[u]: 1}
		t2 = New()
		t2.Insert(1)
		for _, v := range vout[u] {
			if visited[v] == 0 {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
				visited[u] += visited[v]
			}
		}
		for _, q := range qout[u] {
			if q.k <= visited[u] {
				q.ans = t2.Size() - t2.Count(q.k)
			}
		}
		return
	}

	dfs(0)

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	for _, q := range qs {
		fmt.Fprintln(writer, q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key, Val int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key, val int) {
	x := &Node{
		Key:  key,
		Val:  val,
		size: 1,
		t:    rand.Int(),
	}
	t.insertNode(x)
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v=%v, ", x.Key, x.Val)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) ForEach(f func(x *Node)) {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		dfs(x.r)
		x.l, x.r, x.size = nil, nil, 1
		f(x)
	}
	dfs(t.root)
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	reader := bufio.NewReader(os.Stdin)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	c := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	vout := make([][]int, n)
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
//	fmt.Println(vout)

	qs := make([]quest, m)
	qout := make([][]*quest, n)
	for i := 0; i < m; i++ {
		var v int
		q := &qs[i]
		fmt.Fscanf(reader, "%d %d\n", &v, &q.k)
		v--
		qout[v] = append(qout[v], q)
	}
//	fmt.Println(qs)
//	fmt.Println(qout)

	merge1 := func(a, b, t2 *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			x := a.Find(y.Key)
			if x != nil {
				t2.Erase(x.Val)
				t2.Erase(y.Val)
				x.Val = x.Val + y.Val
				t2.Insert(x.Val, 0)
			} else {
				a.insertNode(y)
			}
		})
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			a.insertNode(y)
		})
		return a
	}

	visited := make([]int, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = 1
		t1 = New()
		t2 = New()
		t1.Insert(c[u], 1)
		t2.Insert(1, 0)
		for _, v := range vout[u] {
			if visited[v] == 0 {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
				visited[u] += visited[v]
			}
		}
		//		fmt.Println("post", u)
		//		t1.Print()
		//		t2.Print()
		for _, q := range qout[u] {
			if q.k <= visited[u] {
				q.ans = t2.Size() - t2.Count(q.k)
			}
		}
		return
	}

	dfs(0)

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	for _, q := range qs {
		fmt.Fprintln(writer, q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key, Val int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key, val int) {
	x := &Node{
		Key:  key,
		Val:  val,
		size: 1,
		t:    rand.Int(),
	}
	t.insertNode(x)
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v=%v, ", x.Key, x.Val)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) ForEach(f func(x *Node)) {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		dfs(x.r)
		x.l, x.r, x.size = nil, nil, 1
		f(x)
	}
	dfs(t.root)
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	reader := bufio.NewReader(os.Stdin)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	c := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	vout := make([][]int, n)
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}

	qs := make([]quest, m)
	qout := make([][]*quest, n)
	for i := 0; i < m; i++ {
		var v int
		q := &qs[i]
		fmt.Fscanf(reader, "%d %d\n", &v, &q.k)
		v--
		qout[v] = append(qout[v], q)
	}

	merge1 := func(a, b, t2 *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			x := a.Find(y.Key)
			if x != nil {
				t2.Erase(x.Val)
				t2.Erase(y.Val)
				x.Val = x.Val + y.Val
				t2.Insert(x.Val, 0)
			} else {
				a.insertNode(y)
			}
		})
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			a.insertNode(y)
		})
		return a
	}

	visited := make([]int, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = 1
		t1 = New()
		t2 = New()
		t1.Insert(c[u], 1)
		t2.Insert(1, 0)
		for _, v := range vout[u] {
			if visited[v] == 0 {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
				visited[u] += visited[v]
			}
		}
		for _, q := range qout[u] {
			if q.k <= visited[u] {
				q.ans = t2.Size() - t2.Count(q.k)
			}
		}
		return
	}

	dfs(0)

	for _, q := range qs {
		fmt.Println(q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key int
}

type Treap struct {
	root *Node
}

func New() *Treap {
	return &Treap{
		root: nil,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(p, x *Node) *Node {
	if p == nil {
		return x
	}
	if x.Key < p.Key {
		p.l = insert(p.l, x)
		p.l.p = p
	} else {
		p.r = insert(p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key int) {
	x := &Node{
		Key:  key,
		size: 1,
		t:    rand.Int(),
	}
	t.insertNode(x)
}

func (t *Treap) insertNode(x *Node) {
	insert(t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key int) *Node {
	p := t.root
	for p != nil {
		if p.Key == key {
			return p
		}
		if key < p.Key {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key int) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key int) int {
	x := t.root
	c := 0
	for x != nil {
		if x.Key >= key {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v, ", x.Key)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) ForEach(f func(x *Node)) {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		dfs(x.r)
		x.l, x.r, x.size = nil, nil, 1
		f(x)
	}
	dfs(t.root)
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	reader := bufio.NewReader(os.Stdin)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	c := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	vout := make([][]int, n)
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}

	qs := make([]quest, m)
	qout := make([][]*quest, n)
	for i := 0; i < m; i++ {
		var v int
		q := &qs[i]
		fmt.Fscanf(reader, "%d %d\n", &v, &q.k)
		v--
		qout[v] = append(qout[v], q)
	}

	merge1 := func(a, b map[int]int, t2 *Treap) map[int]int {
		if len(a) < len(b) {
			a, b = b, a
		}
		for k, vb := range b {
			if va, ok := a[k]; ok {
				t2.Erase(va)
				t2.Erase(vb)
				t2.Insert(va + vb)
				a[k] = va + vb
			} else {
				a[k] = vb
			}
		}
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		b.ForEach(func(y *Node) {
			a.insertNode(y)
		})
		return a
	}

	visited := make([]int, n)
	var dfs func(u int) (t1 map[int]int, t2 *Treap)
	dfs = func(u int) (t1 map[int]int, t2 *Treap) {
		visited[u] = 1
		t1 = map[int]int{c[u]: 1}
		t2 = New()
		t2.Insert(1)
		for _, v := range vout[u] {
			if visited[v] == 0 {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
				visited[u] += visited[v]
			}
		}
		for _, q := range qout[u] {
			if q.k <= visited[u] {
				q.ans = t2.Size() - t2.Count(q.k)
			}
		}
		return
	}

	dfs(0)

	for _, q := range qs {
		fmt.Println(q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"unsafe"
)

type Node struct {
	l, r, p *Node
	size    int
	Data    interface{}
}

type LessFunc func(a, b interface{}) bool
type ReduceFunc func(a, b interface{}) interface{}

type Treap struct {
	root   *Node
	less   LessFunc
	reduce ReduceFunc
}

func New(less LessFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
	}
}

func NewReducible(less LessFunc, reduce ReduceFunc) *Treap {
	return &Treap{
		root:   nil,
		less:   less,
		reduce: reduce,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func hash(x *Node) uint64 {
	z := uintptr(unsafe.Pointer(x))
	h := uint64(0)
	for i := 0; i < 8; i++ {
		h ^= ((h << 5) + uint64(z&0xff) + (h >> 2))
		z >>= 8
	}
	return h
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) {
	for x.p != nil && hash(x) < hash(x.p) {
		rotate(x)
	}
}

func insert(less LessFunc, reduce ReduceFunc, p, x *Node) *Node {
	if p == nil {
		return x
	}
	if reduce != nil && !less(x.Data, p.Data) && !less(p.Data, x.Data) {
		p.Data = reduce(p.Data, x.Data)
		return p
	}
	if less(x.Data, p.Data) {
		p.l = insert(less, reduce, p.l, x)
		p.l.p = p
	} else {
		p.r = insert(less, reduce, p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(data interface{}) {
	x := &Node{
		Data: data,
		size: 1,
	}
	r := insert(t.less, t.reduce, t.root, x)
	adjust(x)
	t.root = up(r)
}

func (t *Treap) Find(data interface{}) *Node {
	p := t.root
	for p != nil {
		if !t.less(p.Data, data) && !t.less(data, p.Data) {
			return p
		}
		if t.less(data, p.Data) {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(data interface{}) bool {
	x := t.Find(data)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || hash(x.l) < hash(x.r)) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(data interface{}) int {
	x := t.root
	c := 0
	for x != nil {
		if !t.less(x.Data, data) {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Print(x.Data, " ")
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func Merge(a, b *Treap) *Treap {
	less := a.less
	reduce := a.reduce

	var merge func(x, y *Node) *Node
	merge = func(x, y *Node) *Node {
		if y == nil {
			return x
		}
		l, r := y.l, y.r
		y.l, y.r = nil, nil
		y.size = 1
		x = merge(x, r)
		x = insert(less, reduce, x, y)
		adjust(y)
		x = merge(x, l)
		return x
	}

	if a.Size() < b.Size() {
		a, b = b, a
	}

	return &Treap{
		root:   up(merge(a.root, b.root)),
		less:   less,
		reduce: reduce,
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	const maxn = 100010

	var (
		n, m int
		c    [maxn]int
		vout [maxn][]int
		qs   []*quest
		qout [maxn][]*quest
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
		fmt.Println(c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		fmt.Println(u, v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, vout[i])
	//	}

	for i := 0; i < m; i++ {
		var v, k int
		fmt.Fscanf(reader, "%d %d\n", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, qout[i])
	//	}
	for _, q := range qs {
		fmt.Println(q)
	}

	type pair struct {
		color, count int
	}
	pairLess := func(a, b interface{}) bool {
		x := a.(pair)
		y := b.(pair)
		return x.color < y.color
	}

	intLess := func(a, b interface{}) bool {
		x := a.(int)
		y := b.(int)
		return x < y
	}

	type update struct {
		c1, c2 int
	}

	visited := make([]bool, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = true

		updates := []update{}
		pairReduce := func(a, b interface{}) interface{} {
			x := a.(pair)
			y := b.(pair)
			//			fmt.Println("reduce:", u, x.count, "+", y.count)
			updates = append(updates, update{x.count, y.count})
			return pair{x.color, x.count + y.count}
		}

		t1 = NewReducible(pairLess, pairReduce)
		t2 = New(intLess)
		t1.Insert(pair{c[u], 1})
		t2.Insert(1)
		for _, v := range vout[u] {
			if !visited[v] {
				u1, u2 := dfs(v)
				t1 = Merge(t1, u1)
				t2 = Merge(t2, u2)
			}
		}
		//		fmt.Println("post", u)
		//		t1.Print()
		//		t2.Print()
		//		fmt.Println(updates)
		for _, u := range updates {
			if !t2.Erase(u.c1) {
				panic(u.c1)
			}
			if !t2.Erase(u.c2) {
				panic(u.c2)
			}
			t2.Insert(u.c1 + u.c2)
		}
		//		t2.Print()
		for _, q := range qout[u] {
			q.ans = t2.Size() - t2.Count(q.k)
		}
		return
	}

	dfs(0)

	for _, q := range qs {
		fmt.Println(q.ans)
	}
}

package main

import (
	"fmt"
	"unsafe"
)

type Node struct {
	l, r, p *Node
	size    int
	Data    interface{}
}

type LessFunc func(a, b interface{}) bool
type ReduceFunc func(a, b interface{}) interface{}

type Treap struct {
	root *Node
	less LessFunc
	reduce ReduceFunc
}

func New(less LessFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
	}
}

func NewReducible(less LessFunc, reduce ReduceFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
		reduce: reduce,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func hash(x *Node) uint64 {
	z := uintptr(unsafe.Pointer(x))
	h := uint64(0)
	for i := 0; i < 8; i++ {
		h ^= ((h << 5) + uint64(z & 0xff) + (h >> 2))
		z >>= 8
	}
	return h
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) {
	for x.p != nil && hash(x) < hash(x.p) {
		rotate(x)
	}
}

func insert(less LessFunc, reduce ReduceFunc, p, x *Node) *Node {
	if p == nil {
		return x
	}
	if reduce != nil && !less(x.Data, p.Data) && !less(p.Data, x.Data) {
		p.Data = reduce(p.Data, x.Data)
		return p
	}
	if less(x.Data, p.Data) {
		p.l = insert(less, reduce, p.l, x)
		p.l.p = p
	} else {
		p.r = insert(less, reduce, p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(data interface{}) {
	x := &Node{
		Data: data,
		size: 1,
	}
	r := insert(t.less, t.reduce, t.root, x)
	adjust(x)
	t.root = up(r)
}

func (t *Treap) Find(data interface{}) *Node {
	p := t.root
	for p != nil {
		if !t.less(p.Data, data) && !t.less(data, p.Data) {
			return p
		}
		if t.less(data, p.Data) {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(data interface{}) bool {
	x := t.Find(data)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || hash(x.l) < hash(x.r)) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(data interface{}) int {
	x := t.root
	c := 0
	for x != nil {
		if !t.less(x.Data, data) {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Print(x.Data, " ")
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func Merge(a, b *Treap) *Treap {
	less := a.less
	reduce := a.reduce

	var merge func(x, y *Node) *Node
	merge = func(x, y *Node) *Node {
		if y == nil {
			return x
		}
		l, r := y.l, y.r
		y.l, y.r = nil, nil
		y.size = 1
		x = merge(x, r)
		x = insert(less, reduce, x, y)
		adjust(y)
		x = merge(x, l)
		return x
	}

	if a.Size() < b.Size() {
		a, b = b, a
	}

	return &Treap{
		root: up(merge(a.root, b.root)),
		less: less,
		reduce: reduce,
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	const maxn = 100010

	var (
		n, m    int
		c       [maxn]int
		vout    [maxn][]int
		qs      []*quest
		qout    [maxn][]*quest
	)

	fmt.Scanf("%d %d", &n, &m)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &c[i])
	}
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Scanf("%d %d", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
//	for i := 0; i < n; i++ {
//		fmt.Println(i, vout[i])
//	}

	for i := 0; i < m; i++ {
		var v, k int
		fmt.Scanf("%d %d", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
//	for i := 0; i < n; i++ {
//		fmt.Println(i, qout[i])
//	}

	type pair struct {
		color, count int
	}
	pairLess := func(a, b interface{}) bool {
		x := a.(pair)
		y := b.(pair)
		return x.color < y.color
	}

	intLess := func(a, b interface{}) bool {
		x := a.(int)
		y := b.(int)
		return x < y
	}

	type update struct {
		c1, c2 int
	}

	visited := make([]bool, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = true

		updates := []update{}
		pairReduce := func(a, b interface{}) interface{} {
			x := a.(pair)
			y := b.(pair)
//			fmt.Println("reduce:", u, x.count, "+", y.count)
			updates = append(updates, update{x.count, y.count})
			return pair{x.color, x.count + y.count}
		}

		t1 = NewReducible(pairLess, pairReduce)
		t2 = New(intLess)
		t1.Insert(pair{c[u], 1})
		t2.Insert(1)
		for _, v := range vout[u] {
			if !visited[v] {
				u1, u2 := dfs(v)
				t1 = Merge(t1, u1)
				t2 = Merge(t2, u2)
			}
		}
//		fmt.Println("post", u)
//		t1.Print()
//		t2.Print()
//		fmt.Println(updates)
		for _, u := range updates {
			if !t2.Erase(u.c1) {
				panic(u.c1)
			}
			if !t2.Erase(u.c2) {
				panic(u.c2)
			}
			t2.Insert(u.c1 + u.c2)
		}
//		t2.Print()
		for _, q := range qout[u] {
			q.ans = t2.Size() - t2.Count(q.k)
		}
		return
	}

	dfs(0)

	for _, q := range qs {
		fmt.Println(q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"unsafe"
)

type Node struct {
	l, r, p  *Node
	size     int
	Key, Val interface{}
}

type LessFunc func(a, b interface{}) bool

type Treap struct {
	root *Node
	less LessFunc
}

func New(less LessFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func hash(x *Node) uint64 {
	z := uintptr(unsafe.Pointer(x))
	h := uint64(0)
	for i := 0; i < 8; i++ {
		h ^= ((h << 5) + uint64(z&0xff) + (h >> 2))
		z >>= 8
	}
	return h
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && hash(x) < hash(x.p) {
		rotate(x)
	}
	return up(x)
}

func insert(less LessFunc, p, x *Node) *Node {
	if p == nil {
		return x
	}
	if less(x.Key, p.Key) {
		p.l = insert(less, p.l, x)
		p.l.p = p
	} else {
		p.r = insert(less, p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key, val interface{}) {
	x := &Node{
		Key:  key,
		Val:  val,
		size: 1,
	}
	insert(t.less, t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key interface{}) *Node {
	p := t.root
	for p != nil {
		if !t.less(p.Key, key) && !t.less(key, p.Key) {
			return p
		}
		if t.less(key, p.Key) {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key interface{}) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || hash(x.l) < hash(x.r)) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key interface{}) int {
	x := t.root
	c := 0
	for x != nil {
		if !t.less(x.Key, key) {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v=%v, ", x.Key, x.Val)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	const maxn = 100010

	var (
		n, m int
		c    [maxn]int
		vout [maxn][]int
		qs   []*quest
		qout [maxn][]*quest
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, vout[i])
	//	}

	for i := 0; i < m; i++ {
		var v, k int
		fmt.Fscanf(reader, "%d %d\n", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, qout[i])
	//	}
	//	for _, q := range qs {
	//		fmt.Println(q)
	//	}

	intLess := func(a, b interface{}) bool {
		x := a.(int)
		y := b.(int)
		return x < y
	}

	merge1 := func(a, b, t2 *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		for x := b.Head(); x != nil; x = x.Next() {
			y := a.Find(x.Key)
			if y != nil {
				t2.Erase(y.Key)
				t2.Erase(x.Key)
				y.Val = y.Val.(int) + x.Val.(int)
				t2.Insert(x.Key, struct{}{})
			} else {
				a.Insert(x.Key, x.Val)
			}
		}
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		for x := b.Head(); x != nil; x = x.Next() {
			a.Insert(x.Key, x.Val)
		}
		return a
	}

	visited := make([]bool, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = true
		t1 = New(intLess)
		t2 = New(intLess)
		t1.Insert(c[u], 1)
		t2.Insert(1, struct{}{})
		for _, v := range vout[u] {
			if !visited[v] {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
			}
		}
//		fmt.Println("post", u)
//		t1.Print()
//		t2.Print()
		for _, q := range qout[u] {
			q.ans = t2.Size() - t2.Count(q.k)
		}
		return
	}

	dfs(0)

	for _, q := range qs {
		fmt.Println(q.ans)
	}
}

package main

import (
	"fmt"
	"unsafe"
)

type Node struct {
	l, r, p *Node
	size    int
	Data    interface{}
}

type LessFunc func(a, b interface{}) bool
type ReduceFunc func(a, b interface{}) interface{}

type Treap struct {
	root *Node
	less LessFunc
	reduce ReduceFunc
}

func New(less LessFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
	}
}

func NewReducible(less LessFunc, reduce ReduceFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
		reduce: reduce,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func hash(x *Node) uint64 {
	z := uintptr(unsafe.Pointer(x))
	h := uint64(0)
	for i := 0; i < 8; i++ {
		h ^= ((h << 5) + uint64(z & 0xff) + (h >> 2))
		z >>= 8
	}
	return h
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) {
	for x.p != nil && hash(x) < hash(x.p) {
		rotate(x)
	}
}

func insert(less LessFunc, reduce ReduceFunc, p, x *Node) *Node {
	if p == nil {
		return x
	}
	if reduce != nil && !less(x.Data, p.Data) && !less(p.Data, x.Data) {
		p.Data = reduce(p.Data, x.Data)
		return p
	}
	if less(x.Data, p.Data) {
		p.l = insert(less, reduce, p.l, x)
		p.l.p = p
	} else {
		p.r = insert(less, reduce, p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(data interface{}) {
	x := &Node{
		Data: data,
		size: 1,
	}
	r := insert(t.less, t.reduce, t.root, x)
	adjust(x)
	t.root = up(r)
}

func (t *Treap) Find(data interface{}) *Node {
	p := t.root
	for p != nil {
		if !t.less(p.Data, data) && !t.less(data, p.Data) {
			return p
		}
		if t.less(data, p.Data) {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(data interface{}) bool {
	x := t.Find(data)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || hash(x.l) < hash(x.r)) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(data interface{}) int {
	x := t.root
	c := 0
	for x != nil {
		if !t.less(x.Data, data) {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Print(x.Data, " ")
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func Merge(a, b *Treap) *Treap {
	less := a.less
	reduce := a.reduce

	var merge func(x, y *Node) *Node
	merge = func(x, y *Node) *Node {
		if y == nil {
			return x
		}
		l, r := y.l, y.r
		y.l, y.r = nil, nil
		y.size = 1
		x = merge(x, r)
		x = insert(less, reduce, x, y)
		adjust(y)
		x = merge(x, l)
		return x
	}

	if a.Size() < b.Size() {
		a, b = b, a
	}

	return &Treap{
		root: up(merge(a.root, b.root)),
		less: less,
		reduce: reduce,
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	const maxn = 100010

	var (
		n, m    int
		c       [maxn]int
		vout    [maxn][]int
		qs      []*quest
		qout    [maxn][]*quest
	)

	fmt.Scanf("%d %d", &n, &m)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &c[i])
		fmt.Println(c[i])
	}
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Scanf("%d %d", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
		fmt.Println(u, v)
	}
//	for i := 0; i < n; i++ {
//		fmt.Println(i, vout[i])
//	}

	for i := 0; i < m; i++ {
		var v, k int
		fmt.Scanf("%d %d", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
//	for i := 0; i < n; i++ {
//		fmt.Println(i, qout[i])
//	}
	for _, q := range qs {
		fmt.Println(q)
	}

	type pair struct {
		color, count int
	}
	pairLess := func(a, b interface{}) bool {
		x := a.(pair)
		y := b.(pair)
		return x.color < y.color
	}

	intLess := func(a, b interface{}) bool {
		x := a.(int)
		y := b.(int)
		return x < y
	}

	type update struct {
		c1, c2 int
	}

	visited := make([]bool, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = true

		updates := []update{}
		pairReduce := func(a, b interface{}) interface{} {
			x := a.(pair)
			y := b.(pair)
//			fmt.Println("reduce:", u, x.count, "+", y.count)
			updates = append(updates, update{x.count, y.count})
			return pair{x.color, x.count + y.count}
		}

		t1 = NewReducible(pairLess, pairReduce)
		t2 = New(intLess)
		t1.Insert(pair{c[u], 1})
		t2.Insert(1)
		for _, v := range vout[u] {
			if !visited[v] {
				u1, u2 := dfs(v)
				t1 = Merge(t1, u1)
				t2 = Merge(t2, u2)
			}
		}
//		fmt.Println("post", u)
//		t1.Print()
//		t2.Print()
//		fmt.Println(updates)
		for _, u := range updates {
			if !t2.Erase(u.c1) {
				panic(u.c1)
			}
			if !t2.Erase(u.c2) {
				panic(u.c2)
			}
			t2.Insert(u.c1 + u.c2)
		}
//		t2.Print()
		for _, q := range qout[u] {
			q.ans = t2.Size() - t2.Count(q.k)
		}
		return
	}

	dfs(0)

	for _, q := range qs {
		fmt.Println(q.ans)
	}
}

package main

import (
	"fmt"
	"unsafe"
)

type Node struct {
	l, r, p *Node
	size    int
	Data    interface{}
}

type LessFunc func(a, b interface{}) bool
type ReduceFunc func(a, b interface{}) interface{}

type Treap struct {
	root *Node
	less LessFunc
	reduce ReduceFunc
}

func New(less LessFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
	}
}

func NewReducible(less LessFunc, reduce ReduceFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
		reduce: reduce,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func hash(x *Node) uint64 {
	z := uintptr(unsafe.Pointer(x))
	h := uint64(0)
	for i := 0; i < 8; i++ {
		h ^= ((h << 5) + uint64(z & 0xff) + (h >> 2))
		z >>= 8
	}
	return h
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) {
	for x.p != nil && hash(x) < hash(x.p) {
		rotate(x)
	}
}

func insert(less LessFunc, reduce ReduceFunc, p, x *Node) *Node {
	if p == nil {
		return x
	}
	if reduce != nil && !less(x.Data, p.Data) && !less(p.Data, x.Data) {
		p.Data = reduce(p.Data, x.Data)
		return p
	}
	if less(x.Data, p.Data) {
		p.l = insert(less, reduce, p.l, x)
		p.l.p = p
	} else {
		p.r = insert(less, reduce, p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(data interface{}) {
	x := &Node{
		Data: data,
		size: 1,
	}
	r := insert(t.less, t.reduce, t.root, x)
	adjust(x)
	t.root = up(r)
}

func (t *Treap) Find(data interface{}) *Node {
	p := t.root
	for p != nil {
		if !t.less(p.Data, data) && !t.less(data, p.Data) {
			return p
		}
		if t.less(data, p.Data) {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(data interface{}) bool {
	x := t.Find(data)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || hash(x.l) < hash(x.r)) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(data interface{}) int {
	x := t.root
	c := 0
	for x != nil {
		if !t.less(x.Data, data) {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Print(x.Data, " ")
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func Merge(a, b *Treap) *Treap {
	less := a.less
	reduce := a.reduce

	var merge func(x, y *Node) *Node
	merge = func(x, y *Node) *Node {
		if y == nil {
			return x
		}
		l, r := y.l, y.r
		y.l, y.r = nil, nil
		y.size = 1
		x = merge(x, r)
		x = insert(less, reduce, x, y)
		adjust(y)
		x = merge(x, l)
		return x
	}

	if a.Size() < b.Size() {
		a, b = b, a
	}

	return &Treap{
		root: up(merge(a.root, b.root)),
		less: less,
		reduce: reduce,
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	const maxn = 100010

	var (
		n, m    int
		c       [maxn]int
		vout    [maxn][]int
		qs      []*quest
		qout    [maxn][]*quest
	)

	fmt.Scanf("%d %d", &n, &m)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &c[i])
	}
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Scanf("%d %d", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
//	for i := 0; i < n; i++ {
//		fmt.Println(i, vout[i])
//	}

	for i := 0; i < m; i++ {
		var v, k int
		fmt.Scanf("%d %d", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
//	for i := 0; i < n; i++ {
//		fmt.Println(i, qout[i])
//	}
	for _, q := range qs {
		fmt.Println(q)
	}

	type pair struct {
		color, count int
	}
	pairLess := func(a, b interface{}) bool {
		x := a.(pair)
		y := b.(pair)
		return x.color < y.color
	}

	intLess := func(a, b interface{}) bool {
		x := a.(int)
		y := b.(int)
		return x < y
	}

	type update struct {
		c1, c2 int
	}

	visited := make([]bool, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = true

		updates := []update{}
		pairReduce := func(a, b interface{}) interface{} {
			x := a.(pair)
			y := b.(pair)
//			fmt.Println("reduce:", u, x.count, "+", y.count)
			updates = append(updates, update{x.count, y.count})
			return pair{x.color, x.count + y.count}
		}

		t1 = NewReducible(pairLess, pairReduce)
		t2 = New(intLess)
		t1.Insert(pair{c[u], 1})
		t2.Insert(1)
		for _, v := range vout[u] {
			if !visited[v] {
				u1, u2 := dfs(v)
				t1 = Merge(t1, u1)
				t2 = Merge(t2, u2)
			}
		}
//		fmt.Println("post", u)
//		t1.Print()
//		t2.Print()
//		fmt.Println(updates)
		for _, u := range updates {
			if !t2.Erase(u.c1) {
				panic(u.c1)
			}
			if !t2.Erase(u.c2) {
				panic(u.c2)
			}
			t2.Insert(u.c1 + u.c2)
		}
//		t2.Print()
		for _, q := range qout[u] {
			q.ans = t2.Size() - t2.Count(q.k)
		}
		return
	}

	dfs(0)

	for _, q := range qs {
		fmt.Println(q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"unsafe"
)

type Node struct {
	l, r, p *Node
	size    int
	Data    interface{}
}

type LessFunc func(a, b interface{}) bool
type ReduceFunc func(a, b interface{}) interface{}

type Treap struct {
	root   *Node
	less   LessFunc
	reduce ReduceFunc
}

func New(less LessFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
	}
}

func NewReducible(less LessFunc, reduce ReduceFunc) *Treap {
	return &Treap{
		root:   nil,
		less:   less,
		reduce: reduce,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func hash(x *Node) uint64 {
	z := uintptr(unsafe.Pointer(x))
	h := uint64(0)
	for i := 0; i < 8; i++ {
		h ^= ((h << 5) + uint64(z&0xff) + (h >> 2))
		z >>= 8
	}
	return h
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) {
	for x.p != nil && hash(x) < hash(x.p) {
		rotate(x)
	}
}

func insert(less LessFunc, reduce ReduceFunc, p, x *Node) *Node {
	if p == nil {
		return x
	}
	if reduce != nil && !less(x.Data, p.Data) && !less(p.Data, x.Data) {
		p.Data = reduce(p.Data, x.Data)
		return p
	}
	if less(x.Data, p.Data) {
		p.l = insert(less, reduce, p.l, x)
		p.l.p = p
	} else {
		p.r = insert(less, reduce, p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(data interface{}) {
	x := &Node{
		Data: data,
		size: 1,
	}
	r := insert(t.less, t.reduce, t.root, x)
	adjust(x)
	t.root = up(r)
}

func (t *Treap) Find(data interface{}) *Node {
	p := t.root
	for p != nil {
		if !t.less(p.Data, data) && !t.less(data, p.Data) {
			return p
		}
		if t.less(data, p.Data) {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(data interface{}) bool {
	x := t.Find(data)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || hash(x.l) < hash(x.r)) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(data interface{}) int {
	x := t.root
	c := 0
	for x != nil {
		if !t.less(x.Data, data) {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Print(x.Data, " ")
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func Merge(a, b *Treap) *Treap {
	less := a.less
	reduce := a.reduce

	var merge func(x, y *Node) *Node
	merge = func(x, y *Node) *Node {
		if y == nil {
			return x
		}
		l, r := y.l, y.r
		y.l, y.r = nil, nil
		y.size = 1
		x = merge(x, r)
		x = insert(less, reduce, x, y)
		adjust(y)
		x = merge(x, l)
		return x
	}

	if a.Size() < b.Size() {
		a, b = b, a
	}

	return &Treap{
		root:   up(merge(a.root, b.root)),
		less:   less,
		reduce: reduce,
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	const maxn = 100010

	var (
		n, m int
		c    [maxn]int
		vout [maxn][]int
		qs   []*quest
		qout [maxn][]*quest
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, vout[i])
	//	}

	for i := 0; i < m; i++ {
		var v, k int
		fmt.Fscanf(reader, "%d %d\n", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, qout[i])
	//	}
	//	for _, q := range qs {
	//		fmt.Println(q)
	//	}

	type pair struct {
		color, count int
	}
	pairLess := func(a, b interface{}) bool {
		x := a.(pair)
		y := b.(pair)
		return x.color < y.color
	}

	intLess := func(a, b interface{}) bool {
		x := a.(int)
		y := b.(int)
		return x < y
	}

	type update struct {
		c1, c2 int
	}

	visited := make([]bool, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = true

		updates := []update{}
		pairReduce := func(a, b interface{}) interface{} {
			x := a.(pair)
			y := b.(pair)
			//			fmt.Println("reduce:", u, x.count, "+", y.count)
			updates = append(updates, update{x.count, y.count})
			return pair{x.color, x.count + y.count}
		}

		t1 = NewReducible(pairLess, pairReduce)
		t2 = New(intLess)
		t1.Insert(pair{c[u], 1})
		t2.Insert(1)
		for _, v := range vout[u] {
			if !visited[v] {
				u1, u2 := dfs(v)
				t1 = Merge(t1, u1)
				t2 = Merge(t2, u2)
			}
		}
		//		fmt.Println("post", u)
		//		t1.Print()
		//		t2.Print()
		//		fmt.Println(updates)
		for _, u := range updates {
			if !t2.Erase(u.c1) {
				panic(u.c1)
			}
			if !t2.Erase(u.c2) {
				panic(u.c2)
			}
			t2.Insert(u.c1 + u.c2)
		}
		//		t2.Print()
		for _, q := range qout[u] {
			q.ans = t2.Size() - t2.Count(q.k)
		}
		return
	}

	dfs(0)

	for _, q := range qs {
		fmt.Println(q.ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"unsafe"
)

type Node struct {
	l, r, p  *Node
	size, t  int
	Key, Val interface{}
}

type LessFunc func(a, b interface{}) bool

type Treap struct {
	root *Node
	less LessFunc
}

func New(less LessFunc) *Treap {
	return &Treap{
		root: nil,
		less: less,
	}
}

func size(x *Node) int {
	if x != nil {
		return x.size
	}
	return 0
}

func (t *Treap) Size() int {
	return size(t.root)
}

func hash(x *Node) uint64 {
	z := uintptr(unsafe.Pointer(x))
	h := uint64(0)
	for i := 0; i < 8; i++ {
		h ^= ((h << 5) + uint64(z&0xff) + (h >> 2))
		z >>= 8
	}
	return h
}

func rotate(x *Node) {
	y := x.p
	g := y.p
	x.p = g
	if g != nil {
		if g.l == y {
			g.l = x
		} else {
			g.r = x
		}
	}
	y.p = x
	if x == y.l {
		y.l = x.r
		if x.r != nil {
			x.r.p = y
		}
		x.r = y
	} else {
		y.r = x.l
		if x.l != nil {
			x.l.p = y
		}
		x.l = y
	}
	y.size = size(y.l) + size(y.r) + 1
	x.size = size(x.l) + size(x.r) + 1
}

func adjust(x *Node) *Node {
	for x.p != nil && x.t < x.p.t {
		rotate(x)
	}
	return up(x)
}

func insert(less LessFunc, p, x *Node) *Node {
	if p == nil {
		return x
	}
	if less(x.Key, p.Key) {
		p.l = insert(less, p.l, x)
		p.l.p = p
	} else {
		p.r = insert(less, p.r, x)
		p.r.p = p
	}
	p.size++
	return p
}

func up(x *Node) *Node {
	if x == nil {
		return nil
	}
	for x.p != nil {
		x = x.p
	}
	return x
}

func (t *Treap) Insert(key, val interface{}) {
	x := &Node{
		Key:  key,
		Val:  val,
		size: 1,
		t:    rand.Int(),
	}
	insert(t.less, t.root, x)
	t.root = adjust(x)
}

func (t *Treap) Find(key interface{}) *Node {
	p := t.root
	for p != nil {
		if !t.less(p.Key, key) && !t.less(key, p.Key) {
			return p
		}
		if t.less(key, p.Key) {
			p = p.l
		} else {
			p = p.r
		}
	}
	return nil
}

func (t *Treap) Erase(key interface{}) bool {
	x := t.Find(key)
	if x == nil {
		return false
	}
	for x.l != nil || x.r != nil {
		if x.l != nil && (x.r == nil || x.l.t < x.r.t) {
			rotate(x.l)
		} else {
			rotate(x.r)
		}
	}
	y := x.p
	x.p = nil
	if y != nil {
		if y.l == x {
			y.l = nil
		} else {
			y.r = nil
		}
		for z := y; z != nil; z = z.p {
			z.size--
		}
	}
	t.root = up(y)
	return true
}

func (t *Treap) Count(key interface{}) int {
	x := t.root
	c := 0
	for x != nil {
		if !t.less(x.Key, key) {
			x = x.l
		} else {
			c += size(x.l) + 1
			x = x.r
		}
	}
	return c
}

func (t *Treap) Print() {
	var dfs func(x *Node)
	dfs = func(x *Node) {
		if x == nil {
			return
		}
		dfs(x.l)
		fmt.Printf("%v=%v, ", x.Key, x.Val)
		dfs(x.r)
	}
	dfs(t.root)
	fmt.Println()
}

func (t *Treap) Head() *Node {
	x := t.root
	if x == nil {
		return nil
	}
	for x.l != nil {
		x = x.l
	}
	return x
}

func (x *Node) Next() *Node {
	if x.r != nil {
		x = x.r
		for x.l != nil {
			x = x.l
		}
		return x
	} else {
		for x.p != nil && x.p.r == x {
			x = x.p
		}
		return x.p
	}
}

func main() {
	type quest struct {
		k, ans int
	}

	const maxn = 100010

	var (
		n, m int
		c    [maxn]int
		vout [maxn][]int
		qs   []*quest
		qout [maxn][]*quest
	)

	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	for i := 1; i < n; i++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		u--
		v--
		vout[u] = append(vout[u], v)
		vout[v] = append(vout[v], u)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, vout[i])
	//	}

	for i := 0; i < m; i++ {
		var v, k int
		fmt.Fscanf(reader, "%d %d\n", &v, &k)
		v--
		q := &quest{k: k}
		qs = append(qs, q)
		qout[v] = append(qout[v], q)
	}
	//	for i := 0; i < n; i++ {
	//		fmt.Println(i, qout[i])
	//	}
	//	for _, q := range qs {
	//		fmt.Println(q)
	//	}

	intLess := func(a, b interface{}) bool {
		x := a.(int)
		y := b.(int)
		return x < y
	}

	merge1 := func(a, b, t2 *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		for x := b.Head(); x != nil; x = x.Next() {
			y := a.Find(x.Key)
			if y != nil {
				t2.Erase(x.Val)
				t2.Erase(y.Val)
				y.Val = y.Val.(int) + x.Val.(int)
				t2.Insert(y.Val, struct{}{})
			} else {
				a.Insert(x.Key, x.Val)
			}
		}
		return a
	}

	merge2 := func(a, b *Treap) *Treap {
		if a.Size() < b.Size() {
			a, b = b, a
		}
		for x := b.Head(); x != nil; x = x.Next() {
			a.Insert(x.Key, x.Val)
		}
		return a
	}

	visited := make([]bool, n)
	var dfs func(u int) (t1, t2 *Treap)
	dfs = func(u int) (t1, t2 *Treap) {
		visited[u] = true
		t1 = New(intLess)
		t2 = New(intLess)
		t1.Insert(c[u], 1)
		t2.Insert(1, struct{}{})
		for _, v := range vout[u] {
			if !visited[v] {
				u1, u2 := dfs(v)
				t2 = merge2(t2, u2)
				t1 = merge1(t1, u1, t2)
			}
		}
		//		fmt.Println("post", u)
		//		t1.Print()
		//		t2.Print()
		for _, q := range qout[u] {
			q.ans = t2.Size() - t2.Count(q.k)
		}
		return
	}

	dfs(0)

	writer := bufio.NewWriter(os.Stdout)
	for _, q := range qs {
		fmt.Fprintln(writer, q.ans)
	}
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var x1, x2, y1, y2 int
		fmt.Fscan(reader, &x1, &y1, &x2, &y2)

		dist := 0
		if x1 == x2 {
			dist = abs(y1 - y2)
		} else if y1 == y2 {
			dist = abs(x1 - x2)
		} else {
			dist = abs(x1-x2) + abs(y1-y2) + 2
		}

		fmt.Println(dist)
	}
}

func abs(a int) int {
	if a < 0 {
		return -a
	}

	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const (
	initialBufSize = 1e4
	maxBufSize     = 1e6 + 7
)

var buf []byte = make([]byte, initialBufSize)
var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(buf, maxBufSize)
}

// 問題によって値は調整する
const (
	mod     = int(1e9) + 7
	maxsize = 510000
)

func main() {
	N := nextInt()
	for i := 0; i < N; i++ {
		var ans int
		x1, y1, x2, y2 := nextInt(), nextInt(), nextInt(), nextInt()
		if x1 == x2 {
			ans = Abs(y2 - y1)
		} else if y1 == y2 {
			ans = Abs(x2 - x1)
		} else {
			ans = Abs(x2-x1) + Abs(y2-y1) + 2
		}
		fmt.Println(ans)
	}
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

// Math Utilities
// https://play.golang.org/p/bm7uZi0zCN

// Abs Absolute Value
func Abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

// Pow Integer power: compute a**b using binary powering algorithm
// See Donald Knuth, The Art of Computer Programming, Volume 2, Section 4.6.3
func Pow(a, b int) int {
	p := 1
	for b > 0 {
		if b&1 != 0 {
			p *= a
		}
		b >>= 1
		a *= a
	}
	return p
}

// PowMod Modular integer power: compute a**b mod m using binary powering algorithm
func PowMod(a, b, m int) int {
	a = a % m
	p := 1 % m
	for b > 0 {
		if b&1 != 0 {
			p = (p * a) % m
		}
		b >>= 1
		a = (a * a) % m
	}
	return p
}

func ceil(a, b int) int {
	return (a + (b - 1)) / b
}

// MinOf 与えられたintのうち最小のものを返す
func MinOf(vars ...int) int {
	min := vars[0]
	for _, i := range vars {
		if min > i {
			min = i
		}
	}
	return min
}

// MaxOf 与えられたintのうち最大のものを返す
func MaxOf(vars ...int) int {
	max := vars[0]
	for _, i := range vars {
		if max < i {
			max = i
		}
	}
	return max
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for t > 0 {
		t--
		func() {
			x1 := getNextInt(scanner)
			y1 := getNextInt(scanner)
			x2 := getNextInt(scanner)
			y2 := getNextInt(scanner)
			ans := abs(x2-x1) + abs(y2-y1)
			if x1 == x2 || y1 == y2 {
				fmt.Fprintln(writer, ans)
				return
			}
			fmt.Fprintln(writer, ans+2)
		}()
	}
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(in, a...)
}

var err error

func next() (n string) {
	scan(&n)
	return n
}

func nextInt() (n int) {
	scan(&n)
	return n
}

func nextLong() (n int64) {
	scan(&n)
	return n
}

func nextDouble() (n float64) {
	scan(&n)
	return n
}

func Max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func Abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

const mod = 1000000007

func Add(a, b int64) int64 {
	return (a + b) % mod
}

func Mult(a, b int64) int64 {
	return (a * b) % mod
}

func solve() {
	a, b, c, d := nextLong(), nextLong(), nextLong(), nextLong()
	res := Abs(c-a) + Abs(d-b)
	if a != c && b != d {
		res += 2
	}
	printf("%d\n", res)
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	name := "in"
	file1, err := os.Open(name + ".in")
	if err == nil {
		in = bufio.NewReaderSize(file1, 8192)
	}
	// file2, err := os.Create(name + ".out")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// out = bufio.NewWriterSize(file2, 8192)
	defer out.Flush()
	// start := time.Now()

	for t, i := nextInt(), 1; t > 0; t, i = t-1, i+1 {
		// printf("Case #%d:\n", i)
		solve()
	}

	// printf("%v\n", time.Now().Sub(start))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	defer _w.Flush()
	t := ReadInt()
	for i := 0; i < t; i++ {
		x1, y1, x2, y2 := ReadInt(), ReadInt(), ReadInt(), ReadInt()
		Println(solve(x1, y1, x2, y2))
	}

}

func solve(x1, y1, x2, y2 int) int {
	if x1 == x2 {
		return Abs(y2 - y1)
	}
	if y1 == y2 {
		return Abs(x2 - x1)
	}
	return Abs(x2-x1) + Abs(y2-y1) + 2
}

func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

var _r, _w = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func Print(a ...interface{})            { fmt.Fprint(_w, a...) }
func Printf(f string, a ...interface{}) { fmt.Fprintf(_w, f, a...) }
func Println(a ...interface{})          { fmt.Fprintln(_w, a...) }
func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(_r, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt())
	}
	return
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt64())
	}
	return
}

/*
URL:
https://codeforces.com/contest/1428/problem/A
*/

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	t int

	x1, y1, x2, y2 int64
)

func main() {
	defer stdout.Flush()

	t = readi()
	for tc := 0; tc < t; tc++ {
		x1, y1, x2, y2 = readll4()

		solve()
	}
}

func solve() {
	dx := AbsInt(x1 - x2)
	dy := AbsInt(y1 - y2)

	ans := dx + dy
	if dx == 0 || dy == 0 {
		printf("%d\n", ans)
	} else {
		printf("%d\n", ans+2)
	}
}

// AbsInt is integer version of math.Abs
func AbsInt(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

/*******************************************************************/

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
	EPS     = 1e-10
)

/********** bufio setting **********/

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	reads = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

// mod can calculate a right residual whether value is positive or negative.
func mod(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// chmin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func chmin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// chmax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func chmax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// sum returns multiple integers sum.
func sum(integers ...int) int {
	var s int
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  func() string
	stdout *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

func _readInt64() int64 {
	i, err := strconv.ParseInt(reads(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// readis returns an integer slice that has n integers.
func readis(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readi()
	}
	return b
}

// readlls returns as int64 slice that has n integers.
func readlls(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = readll()
	}
	return b
}

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

func _readFloat64() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func readfs(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = readf()
	}
	return b
}

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// Printf is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func printf(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

/*********** Debugging ***********/

// debugf is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func debugf(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

package main

import "fmt"

func dis(a, b int) int {
	if a > b {
		return a - b
	} else {
		return b - a
	}
}

func main() {

	t := 0
	fmt.Scanf("%d\n", &t)

	for t > 0 {
		var x1, y1, x2, y2 int
		fmt.Scanf("%d %d %d %d\n", &x1, &y1, &x2, &y2)
		dx, dy := dis(x2, x1), dis(y2, y1)
		ans := dx + dy
		if dx > 0 && dy > 0 {
			ans += 2
		}
		fmt.Printf("%d\n", ans)
		t -= 1
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
)

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var x1, y1, x2, y2 float64
		fmt.Fscan(input, &x1, &y1, &x2, &y2)

		ans := int(math.Abs(x1-x2) + math.Abs(y1-y2))

		if x1 != x2 && y1 != y2 {
			ans += 2
		}

		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var x1, x2, y1, y2 int
		fmt.Fscan(reader, &x1, &y1, &x2, &y2)

		dist := 0
		dx := abs(x1 - x2)
		dy := abs(y1 - y2)

		dist += dx
		if dy > 0 {
			dist += 2
		}
		dist += dy

		fmt.Println(dist)
	}
}

func abs(a int) int {
	if a < 0 {
		return -a
	}

	return a
}


package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)
	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for ; T != 0; T-- {
		var n, x int
		fmt.Fscanf(reader, "%d%d\n", &n, &x)
		var d, h []int

		maxDi := 0
		for i := 0; i < n; i++ {
			var di, hi int
			fmt.Fscanf(reader, "%d%d\n", &di, &hi)
			d = append(d, di)
			h = append(h, hi)
			if maxDi < d[i] {
				maxDi = d[i]
			}
		}

		x -= maxDi
		if x <= 0 {
			fmt.Println(1)
			continue
		}

		for i := 0; i < n; i++ {
			d[i] -= h[i]
		}

		sort.Sort(sort.Reverse(sort.IntSlice(d)))
		if d[0] <= 0 {
			fmt.Println(-1)
			continue
		}
		ans := x/d[0] + 1
		if x%d[0] != 0 {
			ans++
		}
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type point struct {
	x, y int
}

var ans int64

const (
	mod = int(1e9 + 7)
)

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {
		n, h := 0, 0
		fmt.Fscanf(in, "%d %d\n", &n, &h)
		maxm := 0
		maxh := 0
		for j := 0; j < n; j++ {
			a, b := 0, 0
			fmt.Fscanf(in, "%d %d\n", &a, &b)
			if a-b > maxm {
				maxm = a - b
			}
			if a > maxh {
				maxh = a
			}
		}
		if maxm <= 0 && maxh < h {
			fmt.Fprintf(out, "-1\n")
			continue
		}
		ans := 0
		ans++
		h -= maxh
		if h > 0 {
			tmp := h / maxm
			for maxm*tmp < h {
				tmp++
			}
			ans += tmp
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

func help(i int, n int, total int, t []int, g []int, cur int, c1, c2, c3 int, remaining int) {
	//fmt.Println(i, cur)
	if i == n {
		if cur == total {
			cal(c1, c2, c3)
		}
		return
	}
	if cur > total {
		return
	}
	if cur+remaining < total {
		return
	}
	switch g[i] {
	case 1:
		help(i+1, n, total, t, g, cur+t[i], c1+1, c2, c3, remaining-t[i])
	case 2:
		help(i+1, n, total, t, g, cur+t[i], c1, c2+1, c3, remaining-t[i])
	case 3:
		help(i+1, n, total, t, g, cur+t[i], c1, c2, c3+1, remaining-t[i])
	}
	help(i+1, n, total, t, g, cur, c1, c2, c3, remaining-t[i])
}

func cal(c1, c2, c3 int) {
	a := []int{c1, c2, c3}
	sort.Ints(a)
	tmp := int64(try(c1, c2, c3, 0))
	tmp %= int64(mod)
	for i := 1; i <= c1; i++ {
		tmp *= int64(i)
		tmp %= int64(mod)
	}
	for i := 1; i <= c2; i++ {
		tmp *= int64(i)
		tmp %= int64(mod)
	}
	for i := 1; i <= c3; i++ {
		tmp *= int64(i)
		tmp %= int64(mod)
	}
	ans += tmp
	ans %= int64(mod)
}

func try(a, b, c int, last int) int64 {
	if a == 0 && b == 0 && c == 0 {
		return 1
	}
	if a < 0 || b < 0 || c < 0 {
		return 0
	}
	if last == 0 {
		return try(a-1, b, c, 1) + try(a, b-1, c, 2) + try(a, b, c-1, 3)
	}
	if last == 1 {
		return try(a, b-1, c, 2) + try(a, b, c-1, 3)
	}
	if last == 2 {
		return try(a-1, b, c, 1) + try(a, b, c-1, 3)
	}
	return try(a-1, b, c, 1) + try(a, b-1, c, 2)
}

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	firstRow, err := reader.ReadString('\n')
	if err != nil {
		log.Fatal(err)
	}

	var firstRowfields = strings.Fields(firstRow)
	n, err := strconv.Atoi(firstRowfields[0])
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < n; i++ {
		line, err := reader.ReadString('\n')
		ints, err := parseInts(line)
		if err != nil {
			log.Fatal(err)
		}

		hitsCount, headsCount := ints[0], ints[1]

		mostPowerfullHit := int(0)
		mostEffectiveHit := int(0)

		for i := 0; i < hitsCount; i++ {
			line, err := reader.ReadString('\n')
			ints, err := parseInts(line)
			if err != nil {
				log.Fatal(err)
			}

			if ints[0] > mostPowerfullHit {
				mostPowerfullHit = ints[0]
			}

			if (ints[0] - ints[1]) > mostEffectiveHit {
				mostEffectiveHit = ints[0] - ints[1]
			}

		}

		result := solution(headsCount, mostPowerfullHit, mostEffectiveHit)

		fmt.Println(result)
	}
}

func parseInts(s string) ([]int, error) {
	var (
		fields = strings.Fields(s)
		ints   = make([]int, len(fields))
		err    error
	)
	for i, f := range fields {
		ints[i], err = strconv.Atoi(f)
		if err != nil {
			return nil, err
		}
	}
	return ints, nil
}

func solution(headsCount int, mostPowerfullHit int, mostEffectiveHit int) int {
	if mostPowerfullHit >= headsCount {
		return 1
	}

	if mostEffectiveHit <= 0 {
		return -1
	}

	if (headsCount - mostPowerfullHit)%mostEffectiveHit == 0 {
		return (headsCount - mostPowerfullHit)/mostEffectiveHit + 1
	} else {
		return (headsCount - mostPowerfullHit)/mostEffectiveHit + 2
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	T, n, x int
	d       = make([]int, 0, 105)
	h       = make([]int, 0, 105)
)

const maxAns = 1000*1000*1000 + 5

func main() {
	fmt.Fscan(reader, &T)
	for cs := 0; cs < T; cs++ {
		solve()
	}
}

func solve() {
	fmt.Fscan(reader, &n, &x)
	d = d[0:n]
	h = h[0:n]
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &d[i], &h[i])
	}

	ans := maxAns
	for i := 0; i < n; i++ {
		dec := d[i] - h[i]
		if dec > 0 {
			ans = min(ans, (x+dec-1)/dec)
		}
	}

	maxD := d[0]
	for i := 1; i < n; i++ {
		if d[i] > maxD {
			maxD = d[i]
		}
	}
	newx := x - maxD
	if newx <= 0 {
		ans = min(ans, 1)
	} else {
		for i := 0; i < n; i++ {
			dec := d[i] - h[i]
			if dec > 0 {
				ans = min(ans, 1+(newx+dec-1)/dec)
			}
		}
	}

	if ans == maxAns {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

// Author: sighduck
// URL: https://codeforces.com/contest/1217/problem/B

package main

import (
	"bufio"
	"fmt"
	"os"
)

func Solve(n int, x int, d []int, h []int) int {
	maxD := -1
	maxDelta := -1
	for i := 0; i < n; i++ {
		delta := d[i] - h[i]
		if maxD == -1 || d[i] > maxD {
			maxD = d[i]
		}
		if maxDelta == -1 || delta > maxDelta {
			maxDelta = delta
		}
	}
	if maxD >= x {
		return 1
	}
	if maxDelta <= 0 {
		return -1
	}
	return (x-maxD+maxDelta-1)/maxDelta + 1
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n, x int
		fmt.Fscanf(reader, "%d %d\n", &n, &x)
		d, h := make([]int, n), make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d %d\n", &d[i], &h[i])
		}
		fmt.Fprintf(writer, "%d\n", Solve(n, x, d, h))
	}
}

package main

import "fmt"

func main() {

	var t, n, curx, d, h int

	fmt.Scanln(&t)

	for i:=0; i < t; i++ {
		fmt.Scanln(&n, &curx)
		ans := 2000000000
		for j := 0; j < n; j++ {
			fmt.Scanln(&d, &h)
			tmpans := 2000000001
			if curx > d {
				if h-d < 0 {
					tmpans = (curx-d)/(d-h)+1
				}
				if tmpans == 1 {
					tmpans = 2
				}
			} else {
				tmpans = 1
			}
			if ans > tmpans {
				ans = tmpans
			}
		}
		if ans == 2000000000 {
			fmt.Println(-1)
		} else {
			fmt.Println(ans)
		}
	}
	// min(x, d)  if x>d  x = x-d+h  else ok
	// x  d h
	// 10 6 3   x = 7 // x=4
	// 10 8 2   x = 4
	// 10 1 4   x = -1

	// 10 4 1   7 4 1
	// 10 3 2   9 8 7 6 5 4 3 2
	// 10 2 6   -1
	// 10 1 100   -1
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)
	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for ; T != 0; T-- {
		var n, x int
		fmt.Fscanf(reader, "%d%d\n", &n, &x)
		var d, h []int

		maxDi := 0
		for i := 0; i < n; i++ {
			var di, hi int
			fmt.Fscanf(reader, "%d%d\n", &di, &hi)
			d = append(d, di)
			h = append(h, hi)
			if maxDi < d[i] {
				maxDi = d[i]
			}
		}

		x -= maxDi
		if x < 0 {
			fmt.Println(1)
			continue
		}

		for i := 0; i < n; i++ {
			d[i] -= h[i]
		}

		sort.Sort(sort.Reverse(sort.IntSlice(d)))
		if d[0] <= 0 {
			fmt.Println(-1)
			continue
		}
		ans := x/d[0] + 1
		if x%d[0] != 0 {
			ans++
		}
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	T, n, x int
	d       = make([]int, 0, 105)
	h       = make([]int, 0, 105)
)

const maxAns = 1000*1000*1000 + 5

func main() {
	fmt.Fscan(reader, &T)
	for cs := 0; cs < T; cs++ {
		solve()
	}
}

func solve() {
	fmt.Fscan(reader, &n, &x)
	d = d[0:n]
	h = h[0:n]
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &d[i], &h[i])
	}

	ans := maxAns
	for i := 0; i < n; i++ {
		dec := d[i] - h[i]
		if dec > 0 {
			ans = min(ans, (x+dec-1)/dec)
		}
	}

	maxD := d[0]
	for i := 1; i < n; i++ {
		if d[i] > maxD {
			maxD = d[i]
		}
	}
	newx := x - maxD
	for i := 0; i < n; i++ {
		dec := d[i] - h[i]
		if dec > 0 {
			ans = min(ans, 1+(newx+dec-1)/dec)
		}
	}

	if ans == maxAns {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}

}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}