You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to n. Then we represent the color of vertex v as cv. The tree root is a vertex with number 1.In this problem you need to answer to m queries. Each query is described by two integers vj, kj. The answer to query vj, kj is the number of such colors of vertices x, that the subtree of vertex vj contains at least kj vertices of color x.You can find the definition of a rooted tree by the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory).

The first line contains two integers n and m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105). The next line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105). The next n - 1 lines contain the edges of the tree. The i-th line contains the numbers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — the vertices connected by an edge of the tree. Next m lines contain the queries. The j-th line contains two integers vj, kj (1 ≤ vj ≤ n; 1 ≤ kj ≤ 105).

Print m integers — the answers to the queries in the order the queries appear in the input.

NoteA subtree of vertex v in a rooted tree with root r is a set of vertices {u : dist(r, v) + dist(v, u) = dist(r, u)}. Where dist(x, y) is the length (in edges) of the shortest path between vertices x and y.

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"
	"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"
	"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"
	"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)
	}
}


Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point $$$(x_1,y_1)$$$ to the point $$$(x_2,y_2)$$$.He has a rope, which he can use to pull the box. He can only pull the box if he stands exactly $$$1$$$ unit away from the box in the direction of one of two coordinate axes. He will pull the box to where he is standing before moving out of the way in the same direction by $$$1$$$ unit.   For example, if the box is at the point $$$(1,2)$$$ and Wabbit is standing at the point $$$(2,2)$$$, he can pull the box right by $$$1$$$ unit, with the box ending up at the point $$$(2,2)$$$ and Wabbit ending at the point $$$(3,2)$$$.Also, Wabbit can move $$$1$$$ unit to the right, left, up, or down without pulling the box. In this case, it is not necessary for him to be in exactly $$$1$$$ unit away from the box. If he wants to pull the box again, he must return to a point next to the box. Also, Wabbit can't move to the point where the box is located.Wabbit can start at any point. It takes $$$1$$$ second to travel $$$1$$$ unit right, left, up, or down, regardless of whether he pulls the box while moving.Determine the minimum amount of time he needs to move the box from $$$(x_1,y_1)$$$ to $$$(x_2,y_2)$$$. Note that the point where Wabbit ends up at does not matter.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$: the number of test cases. The description of the test cases follows. Each of the next $$$t$$$ lines contains four space-separated integers $$$x_1, y_1, x_2, y_2$$$ $$$(1 \leq x_1, y_1, x_2, y_2 \leq 10^9)$$$, describing the next test case.

For each test case, print a single integer: the minimum time in seconds Wabbit needs to bring the box from $$$(x_1,y_1)$$$ to $$$(x_2,y_2)$$$.

NoteIn the first test case, the starting and the ending points of the box are $$$(1,2)$$$ and $$$(2,2)$$$ respectively. This is the same as the picture in the statement. Wabbit needs only $$$1$$$ second to move as shown in the picture in the statement.In the second test case, Wabbit can start at the point $$$(2,1)$$$. He pulls the box to $$$(2,1)$$$ while moving to $$$(3,1)$$$. He then moves to $$$(3,2)$$$ and then to $$$(2,2)$$$ without pulling the box. Then, he pulls the box to $$$(2,2)$$$ while moving to $$$(2,3)$$$. It takes $$$4$$$ seconds.

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
		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 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
}


You are given a simple weighted connected undirected graph, consisting of $$$n$$$ vertices and $$$m$$$ edges.A path in the graph of length $$$k$$$ is a sequence of $$$k+1$$$ vertices $$$v_1, v_2, \dots, v_{k+1}$$$ such that for each $$$i$$$ $$$(1 \le i \le k)$$$ the edge $$$(v_i, v_{i+1})$$$ is present in the graph. A path from some vertex $$$v$$$ also has vertex $$$v_1=v$$$. Note that edges and vertices are allowed to be included in the path multiple times.The weight of the path is the total weight of edges in it.For each $$$i$$$ from $$$1$$$ to $$$q$$$ consider a path from vertex $$$1$$$ of length $$$i$$$ of the maximum weight. What is the sum of weights of these $$$q$$$ paths?Answer can be quite large, so print it modulo $$$10^9+7$$$.

The first line contains a three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$2 \le n \le 2000$$$; $$$n - 1 \le m \le 2000$$$; $$$m \le q \le 10^9$$$) — the number of vertices in the graph, the number of edges in the graph and the number of lengths that should be included in the answer. Each of the next $$$m$$$ lines contains a description of an edge: three integers $$$v$$$, $$$u$$$, $$$w$$$ ($$$1 \le v, u \le n$$$; $$$1 \le w \le 10^6$$$) — two vertices $$$v$$$ and $$$u$$$ are connected by an undirected edge with weight $$$w$$$. The graph contains no loops and no multiple edges. It is guaranteed that the given edges form a connected graph.

Print a single integer — the sum of the weights of the paths from vertex $$$1$$$ of maximum weights of lengths $$$1, 2, \dots, q$$$ modulo $$$10^9+7$$$.

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)

	n, m, q := readThreeNums(reader)
	E := make([][]int, m)

	for i := 0; i < m; i++ {
		E[i] = readNNums(reader, 3)
	}
	fmt.Println(solve(n, m, q, E))
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

const INF = 1 << 62
const MOD = 1000000007
const inv2 = (MOD + 1) / 2

func add(a int, b int) int {
	a += b
	if a >= MOD {
		a -= MOD
	}
	if a < 0 {
		a += MOD
	}
	return a
}

func mul(a int, b int) int {
	A := int64(a)
	B := int64(b)

	return int((A * B) % MOD)
}

func calc(a1, d, n int) int {
	return mul(mul(n, inv2), add(mul(2, a1), mul(add(n, -1), d)))
}

type Frac struct {
	x, y int64
}

func NewFrac(x, y int64) Frac {
	if y < 0 {
		return Frac{-x, -y}
	}
	return Frac{x, y}
}

func (this Frac) Less(that Frac) bool {
	return this.x*that.y <= this.y*that.x
}

type Line struct {
	m, c int64
}

func (this Line) Intersect(that Line) Frac {
	return NewFrac(this.c-that.c, that.m-this.m)
}

type Lines []Line

func (lines Lines) Len() int {
	return len(lines)
}

func (lines Lines) Less(i, j int) bool {
	a, b := lines[i], lines[j]
	if a.m != b.m {
		return a.m < b.m
	}
	return a.c < b.c
}

func (lines Lines) Swap(i, j int) {
	lines[i], lines[j] = lines[j], lines[i]
}

func solve(n int, m int, q int, E [][]int) int {
	hv := make([]int, n)

	for _, e := range E {
		v, u, w := e[0], e[1], e[2]
		v--
		u--
		hv[v] = max(hv[v], w)
		hv[u] = max(hv[u], w)
	}

	var ans int

	d := make([]int64, n)
	for i := 0; i < n; i++ {
		d[i] = -INF
	}
	d[0] = 0
	nd := make([]int64, n)

	for val := 0; val < m; val++ {
		var mx int64

		for i := 0; i < n; i++ {
			mx = max2(mx, d[i])
		}
		if val > 0 {
			ans = add(ans, int(mx%MOD))
		}
		copy(nd, d)

		for _, e := range E {
			v, u, w := e[0], e[1], e[2]
			v--
			u--
			nd[v] = max2(nd[v], d[u]+int64(w))
			nd[u] = max2(nd[u], d[v]+int64(w))
		}
		copy(d, nd)
	}

	fin := make([]Line, n)

	for i := 0; i < n; i++ {
		fin[i] = Line{int64(hv[i]), d[i]}
	}

	sort.Sort(Lines(fin))

	var j int

	for i := 1; i <= n; i++ {
		if i == n || fin[i].m > fin[i-1].m {
			fin[j] = fin[i-1]
			j++
		}
	}

	fin = fin[:j]

	ch := make([]Line, j)
	var p int
	for _, cur := range fin {
		for p > 1 && cur.Intersect(ch[p-1]).Less(ch[p-1].Intersect(ch[p-2])) {
			p--
		}

		ch[p] = cur
		p++
	}

	var prv int64
	Q := int64(q - m)

	for i := 0; i < p-1; i++ {
		f := ch[i].Intersect(ch[i+1])

		if f.x < 0 {
			continue
		}

		lst := min2(Q, f.x/f.y)

		if lst < prv {
			continue
		}

		ans = add(ans, calc(int((ch[i].c+ch[i].m*prv)%MOD), int(ch[i].m%MOD), int(lst-prv+1)))

		prv = lst + 1
	}

	ans = add(ans, calc(int((ch[p-1].c+ch[p-1].m*prv)%MOD), int(ch[p-1].m%MOD), int(Q-prv+1)))

	return ans
}

func min2(a, b int64) int64 {
	if a <= b {
		return a
	}
	return b
}

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

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