Kawasiro Nitori is excellent in engineering. Thus she has been appointed to help maintain trains.There are $$$n$$$ models of trains, and Nitori's department will only have at most one train of each model at any moment. In the beginning, there are no trains, at each of the following $$$m$$$ days, one train will be added, or one train will be removed. When a train of model $$$i$$$ is added at day $$$t$$$, it works for $$$x_i$$$ days (day $$$t$$$ inclusive), then it is in maintenance for $$$y_i$$$ days, then in work for $$$x_i$$$ days again, and so on until it is removed.In order to make management easier, Nitori wants you to help her calculate how many trains are in maintenance in each day.On a day a train is removed, it is not counted as in maintenance.

The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n,m \le 2 \cdot 10^5$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i,y_i$$$ ($$$1 \le x_i,y_i \le 10^9$$$). Each of the next $$$m$$$ lines contains two integers $$$op$$$, $$$k$$$ ($$$1 \le k \le n$$$, $$$op = 1$$$ or $$$op = 2$$$). If $$$op=1$$$, it means this day's a train of model $$$k$$$ is added, otherwise the train of model $$$k$$$ is removed. It is guaranteed that when a train of model $$$x$$$ is added, there is no train of the same model in the department, and when a train of model $$$x$$$ is removed, there is such a train in the department.

Print $$$m$$$ lines, The $$$i$$$-th of these lines contains one integers, denoting the number of trains in maintenance in the $$$i$$$-th day.

NoteConsider the first example:The first day: Nitori adds a train of model $$$3$$$. Only a train of model $$$3$$$ is running and no train is in maintenance.The second day: Nitori adds a train of model $$$1$$$. A train of model $$$1$$$ is running and a train of model $$$3$$$ is in maintenance.The third day: Nitori removes a train of model $$$1$$$. The situation is the same as the first day.The fourth day: Nitori removes a train of model $$$3$$$. There are no trains at all.

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 5)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					if i > l {
						prefs[i]--
					} else {
						prefs[l]--
					}

					if i >= r + 1 {
						prefs[i]++
					} else {
						prefs[r + 1]++
					}

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 5)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					if i > l && i <= r + 1 {
						prefs[i]--
					} else {
						prefs[l]--
					}

					prefs[r + 1]++

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 5)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					if i > l && i <= r {
						prefs[i]--
					} else {
						prefs[l]--
					}

					prefs[r + 1]++

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 5)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					if i > l {
						prefs[i]--
					} else {
						prefs[l]--
					}

					prefs[r + 1]++

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 5)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		subOne := false

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j <= m; {
					l := j
					r := min(j + y - 1, m)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j <= m; {
					l := j
					r := min(j + y - 1, m)

					prefs[l]--
					prefs[r + 1]++

					if l < i && i <= r + 1{
						subOne = true
					}

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		if subOne {
			tr1--
		}

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 5)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		subOne := false

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m)

					prefs[l]--
					prefs[r + 1]++

					if l < i && i <= r + 1{
						subOne = true
					}

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		if subOne {
			tr1--
		}

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 1)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		subOne := false

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]--
					prefs[r + 1]++

					if l < i && i <= r + 1{
						subOne = true
					}

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		if subOne {
			tr1--
		}

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 1)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		subOne := false

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]--
					prefs[r]++

					if l < i && i <= r {
						subOne = true
					}

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		if subOne {
			tr1--
		}

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 1)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		subOne := false

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]--
					prefs[r + 1]++

					if l < i && i <= r {
						subOne = true
					}

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		if subOne {
			tr1--
		}

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 1)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		subOne := false

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]--
					prefs[r + 1]++

					if l <= i && r >= i {
						subOne = true
					}

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		if subOne {
			tr1--
		}

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 1)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]--
					prefs[r]++

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 1)

	for i := 0; i < m; i++ {

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]--
					prefs[r + 1]++

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		tr1 := prefs[i]

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var dayOn []int

var w *bufio.Writer

var prefs []int

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {

	dayOn = make([]int, n + 1)

	prefs = make([]int, m + 1)

	for i := 0; i < m; i++ {

		if i != 0 {
			prefs[i] += prefs[i - 1]
		}

		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				dayOn[tr] = i

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]++
					prefs[r + 1]--

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]++
				}

			}

		} else if o == REM {

			if x + y > BLOCK {

				d := dayOn[tr]

				for j := d + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					prefs[l]--
					prefs[r + 1]++

					j = r + 1 + x
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < rem + x + y; j++ {
					r := j % sum
					cnt[sum][r]--
				}
			}

		} else {
			panic(o)
		}

		tr1 := prefs[i]

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var intervals [][][2]int

var dayOn []int

var w *bufio.Writer

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func solve_stupid() {

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {
	t = make([]int, 4 * m + 1)

	intervals = make([][][2]int, n)
	dayOn = make([]int, n)

	for i := 0; i < m; i++ {
		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				for j := i + x; j < m; {
					l := j
					r := min(j + y - 1, m - 1)

					intervals[tr] = append(intervals[tr], [2]int{l, r})

					update(1, 0,  m - 1, l, r, 1)

					j = r + 1 + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < sum; j++ {
					cnt[sum][j]++
				}

				for j := 0; j < rem; j++ {
					cnt[sum][j]++
				}
			}

		} else if o == REM {

			if x + y > BLOCK {

				for len(intervals[tr]) > 0 {

					interval := intervals[tr][0]

					intervals[tr] = intervals[tr][1 :]

					update(1, 0,  m - 1, interval[0], interval[1], -1)
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < sum; j++ {
					cnt[sum][j]--
				}

				for j := 0; j < rem; j++ {
					cnt[sum][j]--
				}
			}

		} else {
			panic(o)
		}

		tr1 := get(1, 0, m - 1, i)

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var intervals [][][2]int

var dayOn []int

var w *bufio.Writer

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {
	t = make([]int, 4 * m + 1)

	intervals = make([][][2]int, n)
	dayOn = make([]int, n)

	for i := 0; i < m; i++ {
		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				for j := i + x; j < m; {
					l := j
					r := min(j + y, m - 1)

					intervals[tr] = append(intervals[tr], [2]int{l, r})

					update(1, 0,  m - 1, l, r, 1)

					j = r + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < sum; j++ {
					cnt[sum][j]++
				}

				for j := 0; j < rem; j++ {
					cnt[sum][j]++
				}
			}

		} else if o == REM {

			if x + y > BLOCK {

				for len(intervals[tr]) > 0 {

					interval := intervals[tr][0]

					intervals[tr] = intervals[tr][1 :]

					update(1, 0,  m - 1, interval[0], interval[1], -1)
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < sum; j++ {
					cnt[sum][j]--
				}

				for j := 0; j < rem; j++ {
					cnt[sum][j]--
				}
			}

		} else {
			panic(o)
		}

		tr1 := get(1, 0, m - 1, i)

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}

package main

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

var n, m int

var x []int
var y []int

var op []int
var k []int

const ADD = 1
const REM = 2

const BLOCK = 500

var cnt [BLOCK + 1][BLOCK + 1] int

var t []int

var intervals [][][2]int

var dayOn []int

var w *bufio.Writer

func main() {

	read := bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	fmt.Fscan(read, &n, &m)

	x = make([]int, n)
	y = make([]int, n)

	for j := 0; j < n; j++ {
		fmt.Fscan(read, &x[j], &y[j])
	}

	op = make([]int, m)
	k = make([]int, m)

	for j := 0; j < m; j++ {
		fmt.Fscan(read, &op[j], &k[j])
	}

	solve()

}

func build(a []int, v int, tl int, tr int) {
	if tl == tr {
		t[v] = a[tl];
	} else {
		tm := (tl + tr) / 2
		build(a, v*2, tl, tm)
		build(a, v*2+1, tm+1, tr)
		t[v] = 0
	}
}

func update(v int, tl, tr, l int, r int, add int) {
	if l > r {
		return
	}

	if l == tl && r == tr {
		t[v] += add;
	} else {
		tm := (tl + tr) / 2
		update(v*2, tl, tm, l, min(r, tm), add)
		update(v*2+1, tm+1, tr, max(l, tm+1), r, add)
	}
}

func get(v int, tl, tr, pos int) int {
	if tl == tr {
		return t[v]
	}
	tm := (tl + tr) / 2;
	if pos <= tm {
		return t[v] + get(v*2, tl, tm, pos)
	} else {
		return t[v] + get(v*2+1, tm+1, tr, pos)
	}
}

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

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

func solve() {
	t = make([]int, 4 * m + 1)

	intervals = make([][][2]int, n)
	dayOn = make([]int, n)

	for i := 0; i < m; i++ {
		o := op[i]
		tr := k[i]
		tr--

		x, y := x[tr], y[tr]

		if o == ADD {

			if x + y > BLOCK {

				for j := i + x; j < m; {
					l := j
					r := min(j + y, m - 1)

					intervals[tr] = append(intervals[tr], [2]int{l, r})

					update(1, 0,  m - 1, l, r, 1)

					j = r + x
				}

			} else {
				sum := x + y

				dayOn[tr] = i

				rem := i % sum

				for j := rem + x; j < sum; j++ {
					cnt[sum][j]++
				}

				for j := 0; j < rem; j++ {
					cnt[sum][j]++
				}
			}

		} else if o == REM {

			if x + y > BLOCK {

				for j := 0; j < len(intervals[tr]); j++ {

					interval := intervals[tr][j]

					update(1, 0,  m - 1, interval[0], interval[1], -1)
				}

			} else {
				sum := x + y

				d := dayOn[tr]

				rem := d % sum

				for j := rem + x; j < sum; j++ {
					cnt[sum][j]--
				}

				for j := 0; j < rem; j++ {
					cnt[sum][j]--
				}
			}

		} else {
			panic(o)
		}

		tr1 := get(1, 0, m - 1, i)

		var tr2 int

		for j := 2; j <= BLOCK; j++ {

			c :=  cnt[j][(i) % j]

			tr2 += c
		}

		fmt.Fprintln(w, tr1 + tr2)
	}

	w.Flush()
}


Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following:  Add the integer xi to the first ai elements of the sequence.  Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1)  Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 ≤ ti ≤ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| ≤ 103; 1 ≤ ai). If ti = 2, it will be followed by a single integer ki (|ki| ≤ 103). If ti = 3, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.

Output n lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.

NoteIn the second sample, the sequence becomes

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

func main() {
	var val [210000]int
	var add [210000]int
	var n,m,j,t,x int
	var sum int64
	input:= bufio.NewScanner(os.Stdin)
	input.Split(bufio.ScanWords)
	out := bufio.NewWriter(os.Stdout)
	fmt.Scan(&n)
	m=1
	sum=0
 	for i:=0;i<n;i++ {
 	        input.Scan()
 	        aa,_:=strconv.Atoi(input.Text())
 	        t=aa
 	        if t==1 {
 	        	input.Scan()
 	        	bb,_:=strconv.Atoi(input.Text())
 	        	j=bb
 	        	input.Scan()
 	        	cc,_:=strconv.Atoi(input.Text())
 	        	x=cc
 	        	add[j-1]+=x
 	        	sum+=int64(j*x)
 	        } else if t==2 {
 	                input.Scan()
 	                dd,_:=strconv.Atoi(input.Text())
 	                x=dd
 	                val[m]=x
 	                add[m]=0
 	                m++
 	                sum+=int64(x)
 	        } else {
 	        	m--
 	        	sum-=int64(val[m]+add[m])
 	        	add[m-1]+=add[m]
 	        }
 	        var abc float64
 	        abc=float64(sum)/float64(m)
 	        out.WriteString(strconv.FormatFloat(abc,'f',6,64)+"\n")
 	}
 	out.Flush()
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,t,a,x,k int
    ScanInts(&n)
    sum, s, add := int64(0), make([]int,1,n), make([]int,n)
    for i := 0; i < n; i++ {
        ScanInts(&t)
        if t == 1 {
            ScanInts(&a,&x)
            sum += int64(a)*int64(x)
            add[a-1] += x
        } else if t == 2 {
            ScanInts(&k)
            sum += int64(k)
            s = append(s,k)
        } else {
            sum -= int64(s[len(s)-1]+add[len(s)-1])
            add[len(s)-2] += add[len(s)-1]
            add[len(s)-1] = 0
            s = s[:len(s)-1]
        }
        writer.WriteString(strconv.FormatFloat(float64(sum)/float64(len(s)),'f',6,64)+"\n")
    }
    writer.Flush()
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/283/A

package main

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

type AddQuery struct {
	queryType int
	xi        int64
	ai        int64
}

type AppendQuery struct {
	queryType int
	ki        int64
}

type DeleteQuery struct {
	queryType int
}

func Solve(n int, queries []interface{}) []float64 {
	sum := int64(0)
	length := int64(1)
	ans := make([]float64, n)
	delta := []int64{}
	last := int64(0)
	for i, query := range queries {
		switch query.(type) {
		case AddQuery:
			q := query.(AddQuery)
			sum += (q.xi * q.ai)
			if q.xi-1 < int64(len(delta)) {
				delta[q.xi-1] += q.ai
			} else {
				last += q.ai
			}
		case AppendQuery:
			q := query.(AppendQuery)
			sum += q.ki
			length += 1
			delta = append(delta, last-q.ki)
			last = q.ki
		case DeleteQuery:
			length -= 1
			sum -= last
			last += delta[len(delta)-1]
			delta = delta[:len(delta)-1]
		}
		ans[i] = float64(sum) / float64(length)
	}
	return ans
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	queries := make([]interface{}, n)
	for i := 0; i < n; i++ {
		var queryType int
		fmt.Fscanf(reader, "%d", &queryType)
		if queryType == 1 {
			q := AddQuery{1, -1, -1}
			fmt.Fscanf(reader, "%d %d", &q.xi, &q.ai)
			queries[i] = q
		} else if queryType == 2 {
			q := AppendQuery{2, -1}
			fmt.Fscanf(reader, "%d", &q.ki)
			queries[i] = q
		} else {
			queries[i] = DeleteQuery{3}
		}
		fmt.Fscanf(reader, "\n")
	}

	for _, elem := range Solve(n, queries) {
		fmt.Fprintf(writer, "%f\n", elem)
	}
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,t,a,x,k int
    ScanInts(&n)
    sum, s, add := 0, make([]int,1,n), make([]int,n)
    for i := 0; i < n; i++ {
        ScanInts(&t)
        if t == 1 {
            ScanInts(&a,&x)
            sum += (a*x)
            add[a-1] += x
        } else if t == 2 {
            ScanInts(&k)
            sum += k
            s = append(s,k)
        } else {
            sum -= (s[len(s)-1]+add[len(s)-1])
            add[len(s)-2] += add[len(s)-1]
            add[len(s)-1] = 0
            s = s[:len(s)-1]
        }
        writer.WriteString(strconv.FormatFloat(float64(sum)/float64(len(s)),'f',7,64)+"\n")
    }
    writer.Flush()
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    writer := bufio.NewWriter(os.Stdout)

    ScanInts := func(a... *int) {
        for i := 0; i < len(a); i++ {
            scanner.Scan()
            *(a[i]), _ = strconv.Atoi(scanner.Text())
        }
    }

    var n,t,a,x,k int
    ScanInts(&n)
    sum, s, add := 0, make([]int,1,n), make([]int,n)
    for i := 0; i < n; i++ {
        ScanInts(&t)
        if t == 1 {
            ScanInts(&a,&x)
            sum += (a*x)
            add[a-1] += x
        } else if t == 2 {
            ScanInts(&k)
            sum += k
            s = append(s,k)
        } else {
            sum -= (s[len(s)-1]+add[len(s)-1])
            add[len(s)-2] += add[len(s)-1]
            add[len(s)-1] = 0
            s = s[:len(s)-1]
        }
        writer.WriteString(strconv.FormatFloat(float64(sum)/float64(len(s)),'f',6,64)+"\n")
    }
    writer.Flush()
}


The only difference with E2 is the question of the problem..Vlad built a maze out of $$$n$$$ rooms and $$$n-1$$$ bidirectional corridors. From any room $$$u$$$ any other room $$$v$$$ can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.Vlad invited $$$k$$$ friends to play a game with them.Vlad starts the game in the room $$$1$$$ and wins if he reaches a room other than $$$1$$$, into which exactly one corridor leads.Friends are placed in the maze: the friend with number $$$i$$$ is in the room $$$x_i$$$, and no two friends are in the same room (that is, $$$x_i \neq x_j$$$ for all $$$i \neq j$$$). Friends win if one of them meets Vlad in any room or corridor before he wins.For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time. Friends know the plan of a maze and intend to win. Vlad is a bit afraid of their ardor. Determine if he can guarantee victory (i.e. can he win in any way friends play).In other words, determine if there is such a sequence of Vlad's moves that lets Vlad win in any way friends play.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. The input contains an empty string before each test case. The first line of the test case contains two numbers $$$n$$$ and $$$k$$$ ($$$1 \le k < n \le 2\cdot 10^5$$$) — the number of rooms and friends, respectively. The next line of the test case contains $$$k$$$ integers $$$x_1, x_2, \dots, x_k$$$ ($$$2 \le x_i \le n$$$) — numbers of rooms with friends. All $$$x_i$$$ are different. The next $$$n-1$$$ lines contain descriptions of the corridors, two numbers per line $$$v_j$$$ and $$$u_j$$$ ($$$1 \le u_j, v_j \le n$$$) — numbers of rooms that connect the $$$j$$$ corridor. All corridors are bidirectional. From any room, you can go to any other by moving along the corridors. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test is not greater than $$$2\cdot10^5$$$.

Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be "YES" if Vlad can guarantee himself a victory and "NO" otherwise. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" will all be recognized as positive answers).

NoteIn the first test case, regardless of the strategy of his friends, Vlad can win by going to room $$$4$$$. The game may look like this:  The original locations of Vlad and friends. Vlad is marked in green, friends — in red.   Locations after one unit of time.   End of the game. Note that if Vlad tries to reach the exit at the room $$$8$$$, then a friend from the $$$3$$$ room will be able to catch him.

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, k int
	scan(&n, &k)
	friends := make(map[int]struct{}, k)
	for x, i := 0, 0; i < k; i++ {
		scan(&x)
		friends[x-1] = struct{}{}
	}
	adj := make([][]int, n)
	for i := 0; i < n-1; i++ {
		var u, v int
		scan(&u, &v)
		adj[u-1] = append(adj[u-1], v-1)
		adj[v-1] = append(adj[v-1], u-1)
	}

	minDist := GetMinDist(adj, friends)

	ans := false
	var walk func(parent, node, d int)
	walk = func(parent, node, d int) {
		if minDist[node] <= d {
			return
		}
		if node != 0 && len(adj[node]) == 1 {
			ans = true
		}
		for _, next := range adj[node] {
			if next == parent {
				continue
			}
			walk(node, next, d+1)
		}
	}
	walk(-1, 0, 0)

	if ans {
		printf("YES\n")
	} else {
		printf("NO\n")
	}
}

func GetMinDist(adj [][]int, friends map[int]struct{}) []int {
	dist := make([]int, len(adj))
	for i := range dist {
		dist[i] = (1 << 30)
	}
	var walk func(parent, node int)
	walk = func(parent, node int) {
		if _, ok := friends[node]; ok {
			dist[node] = 0
			return
		}
		for _, next := range adj[node] {
			if next == parent {
				continue
			}
			walk(node, next)
			if dist[node] > dist[next]+1 {
				dist[node] = dist[next] + 1
			}
		}
	}
	walk(-1, 0)
	return dist
}

package main

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

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)
)

const (
	MaxN int = 2e5 + 5
)

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	return &Graph{
		adj: make([][]int, n+1),
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	T         int
	n, k      int
	g         *Graph
	deadPoint []bool
)

func calcDeadPoint(u int, dep int, par int) int {
	deadP := 0
	if deadPoint[u] {
		deadP = (dep + 1) / 2
	}
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		vDeadP := calcDeadPoint(v, dep+1, u)
		if (deadP == 0 || vDeadP < deadP) && vDeadP != 0 {
			deadP = vDeadP
		}
	}
	if dep == deadP {
		deadPoint[u] = true
	}
	return deadP
}

func calcPath(u int, par int) bool {
	if deadPoint[u] {
		return false
	}

	isLeaf := true

	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}

		isLeaf = false
		if calcPath(v, u) {
			return true
		}
	}

	return isLeaf
}

func Init() {
	g = NewGraph(n)
	deadPoint = make([]bool, n+1)
}

func main() {
	_, _ = fmt.Fscan(rr, &T)
	for iCase := 0; iCase < T; iCase++ {
		_, _ = fmt.Fscan(rr, &n, &k)

		Init()

		for i := 0; i < k; i++ {
			var u int
			_, _ = fmt.Fscan(rr, &u)
			deadPoint[u] = true
		}

		for i := 1; i < n; i++ {
			var u, v int
			_, _ = fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		calcDeadPoint(1, 0, 0)
		if calcPath(1, 0) {
			_, _ = fmt.Fprintln(ww, "YES")
		} else {
			_, _ = fmt.Fprintln(ww, "NO")
		}
	}
	_ = ww.Flush()
}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

var tree [][]int
var mark []bool
var markDist []int
var queue []int
var seen []bool
var queueHead int
var queueTail int

func findPath(node, parent, dist int) bool {
	if markDist[node] <= dist {
		return false
	}
	if len(tree[node]) == 1 && node != 1 {
		return true
	}
	for _, child := range tree[node] {
		if child != parent && findPath(child, node, dist+1) {
			return true
		}
	}
	return false
}

func bfsPush(node int) {
	queue[queueTail] = node
	seen[node] = true
	queueTail++
}

func solveCase() bool {
	var n, k, a, b int
	fmt.Fscan(in, &n, &k)
	tree = make([][]int, n+1)
	mark = make([]bool, n+1)
	seen = make([]bool, n+1)
	queue = make([]int, n+1)
	markDist = make([]int, n+1)
	queueHead = 0
	queueTail = 0

	for i := 0; i < k; i++ {
		fmt.Fscan(in, &a)
		mark[a] = true
		bfsPush(a)
	}

	for i := 0; i < n-1; i++ {
		fmt.Fscan(in, &a, &b)
		tree[a] = append(tree[a], b)
		tree[b] = append(tree[b], a)
	}

	for ; queueHead < queueTail; queueHead++ {
		var node = queue[queueHead]
		for _, nextNode := range tree[node] {
			if !seen[nextNode] {
				bfsPush(nextNode)
				markDist[nextNode] = markDist[node] + 1
			}
		}
	}

	return findPath(1, 1, 0)
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		if solveCase() {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
	out.Flush()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1611E2(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, k, v, w int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		fr := make([]bool, n)
		for ; k > 0; k-- {
			Fscan(in, &v)
			fr[v-1] = true
		}
		g := make([][]int, n)
		for i := 1; i < n; i++ {
			Fscan(in, &v, &w)
			v--
			w--
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}

		minDep := make([]int, n)
		size := make([]int, n)
		var build func(v, fa, d int)
		build = func(v, fa, d int) {
			minDep[v] = 1e9
			if fr[v] {
				minDep[v] = d
				size[v] = 1
			}
			for _, w := range g[v] {
				if w != fa {
					build(w, v, d+1)
					minDep[v] = min(minDep[v], minDep[w])
					size[v] += size[w]
				}
			}
		}
		build(0, -1, 0)

		var f func(v, fa, d int) int
		f = func(v, fa, d int) (ans int) {
			if d*2 >= minDep[v] {
				return 1
			}
			if size[v] == 0 {
				return -1
			}
			for _, w := range g[v] {
				if w != fa {
					res := f(w, v, d+1)
					if res < 0 {
						return -1
					}
					ans += res
				}
			}
			return
		}
		if f(0, -1, 0) < 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

func main() { CF1611E2(os.Stdin, os.Stdout) }

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

package main

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

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)
)

const (
	MaxN int = 2e5 + 5
)

type Edge struct {
	u, v int
}

type Graph struct {
	adj [][]int
	e   []*Edge
}

func NewGraph(n int) *Graph {
	return &Graph{
		adj: make([][]int, n+1),
		e:   make([]*Edge, 0),
	}
}

func (g *Graph) AddEdge(u, v int) {
	g.adj[u] = append(g.adj[u], len(g.e))
	g.e = append(g.e, &Edge{u, v})
}

var (
	T         int
	n, k      int
	g         *Graph
	deadPoint []bool
)

func calcDeadPoint(u int, dep int, par int) int {
	deadP := 0
	if deadPoint[u] {
		deadP = (dep + 1) / 2
	}
	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}
		vDeadP := calcDeadPoint(v, dep+1, u)
		if (deadP == 0 || vDeadP < deadP) && vDeadP != 0 {
			deadP = vDeadP
		}
	}
	if dep == deadP {
		deadPoint[u] = true
	}
	return deadP
}

func calcPath(u int, par int) bool {
	if deadPoint[u] {
		return false
	}

	isLeaf := true

	for _, i := range g.adj[u] {
		v := g.e[i].v
		if v == par {
			continue
		}

		isLeaf = false
		if calcPath(v, u) {
			return true
		}
	}

	return isLeaf
}

func Init() {
	g = NewGraph(n)
	deadPoint = make([]bool, n+1)
}

func main() {
	_, _ = fmt.Fscan(rr, &T)
	for iCase := 0; iCase < T; iCase++ {
		_, _ = fmt.Fscan(rr, &n, &k)

		Init()

		for i := 0; i < k; i++ {
			var u int
			_, _ = fmt.Fscan(rr, &u)
			deadPoint[u] = true
		}

		for i := 1; i < n; i++ {
			var u, v int
			_, _ = fmt.Fscan(rr, &u, &v)
			g.AddEdge(u, v)
			g.AddEdge(v, u)
		}

		calcDeadPoint(1, 0, 0)
		if calcPath(1, 0) {
			_, _ = fmt.Fprintln(ww, "YES")
		} else {
			_, _ = fmt.Fprintln(ww, "NO")
		}
		fmt.Println(deadPoint)
	}
	_ = ww.Flush()
}


This is an interactive problem.$$$n$$$ people sitting in a circle are trying to shuffle a deck of cards. The players are numbered from $$$1$$$ to $$$n$$$, so that players $$$i$$$ and $$$i+1$$$ are neighbours (as well as players $$$1$$$ and $$$n$$$). Each of them has exactly $$$k$$$ cards, where $$$k$$$ is even. The left neighbour of a player $$$i$$$ is player $$$i - 1$$$, and their right neighbour is player $$$i + 1$$$ (except for players $$$1$$$ and $$$n$$$, who are respective neighbours of each other).Each turn the following happens: if a player has $$$x$$$ cards, they give $$$\lfloor x / 2 \rfloor$$$ to their neighbour on the left and $$$\lceil x / 2 \rceil$$$ cards to their neighbour on the right. This happens for all players simultaneously.However, one player $$$p$$$ is the impostor and they just give all their cards to their neighbour on the right. You know the number of players $$$n$$$ and the number of cards $$$k$$$ each player has initially, but $$$p$$$ is unknown to you. Your task is to determine the value of $$$p$$$, by asking questions like "how many cards does player $$$q$$$ have?" for an index $$$q$$$ of your choice. After each question all players will make exactly one move and give their cards to their neighbours. You need to find the impostor by asking no more than $$$1000$$$ questions.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$4 \le n \le 10^5$$$, $$$2 \le k \le 10^9$$$, $$$k$$$ is even) — the number of players and the number of cards.

NoteIn the example the cards are transferred in the following way:  $$$2$$$ $$$2$$$ $$$2$$$ $$$2$$$ — player $$$1$$$ has $$$2$$$ cards. $$$1$$$ $$$2$$$ $$$3$$$ $$$2$$$ — player $$$1$$$ has $$$1$$$ card.After this turn the number of cards remains unchanged for each player.

package main

import (
	. "fmt"
	"math"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	var n, k, v int
	Scan(&n, &k)
	step := int(math.Sqrt(float64(n)))
	for gcd(step, n) > 1 {
		step++
	}
	for i := 0; ; i += step {
		Println("?", i%n+1)
		Scan(&v)
		if v == k {
			continue
		}
		if v < k {
			for j := i + 1; ; j++ {
				Println("?", j%n+1)
				Scan(&v)
				if v == k {
					Println("!", j%n+1)
					return
				}
			}
		} else {
			for j := i - 1; ; j-- {
				Println("?", (j%n+n)%n+1)
				Scan(&v)
				if v == k {
					Println("!", (j%n+n)%n+1)
					return
				}
			}
		}
	}
}

func gcd(a, b int) int {
	for a != 0 {
		a, b = b%a, a
	}
	return b
}

package main

import (
	. "fmt"
	"math"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	gcd := func(a, b int) int {
		for a != 0 {
			a, b = b%a, a
		}
		return b
	}
	var n, k, v int
	Scan(&n, &k)
	step := int(math.Sqrt(float64(n)))
	for ; gcd(step, n) > 1; step++ {
	}
	for i := 0; ; i += step {
		Println("?", i%n+1)
		Scan(&v)
		if v == k {
			continue
		}
		if v < k {
			for j := i + 1; ; j++ {
				Println("?", j%n+1)
				Scan(&v)
				if v == k {
					Println("!", j%n+1)
					return
				}
			}
		} else {
			for j := i - 1; ; j-- {
				Println("?", (j%n+n)%n+1)
				Scan(&v)
				if v == k {
					Println("!", (j%n+n)%n+1)
					return
				}
			}
		}
	}
}

package main

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

type (
	input struct{ n, k int }
	req   struct{ i int }
	resp  struct{ v int }
	guess struct{ ans int }
)

// github.com/EndlessCheng/codeforces-go
func CF1470C(in input, Q func(req) resp) (gs guess) {
	q := func(i int) int { return Q(req{i}).v }
	n, k := in.n, in.k
	step := int(math.Sqrt(float64(n)))
	gcd := func(a, b int) int {
		for a != 0 {
			a, b = b%a, a
		}
		return b
	}
	for ; gcd(step, n) > 1; step++ {
	}
	for i := 0; ; i += step {
		v := q(i%n + 1)
		if v == k {
			continue
		}
		if v < k {
			for j := i + 1; ; j++ {
				v := q(j%n + 1)
				if v == k {
					gs.ans = j%n + 1
					return
				}
			}
		} else {
			for j := i - 1; ; j-- {
				v := q((j%n+n)%n + 1)
				if v == k {
					gs.ans = (j%n+n)%n + 1
					return
				}
			}
		}
	}
}

func ioq70() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	Q := func(req req) (resp resp) {
		Fprintln(out, "?", req.i)
		out.Flush()
		Fscan(in, &resp.v)
		return
	}

	d := input{}
	Fscan(in, &d.n, &d.k)
	gs := CF1470C(d, Q)
	Fprintln(out, "!", gs.ans)
	out.Flush()
}

func main() { ioq70() }

package main

import (
	. "fmt"
	"math"
)

func gcd(a, b int) int {
	for a != 0 {
		a, b = b%a, a
	}
	return b
}

func main() {
	var n, k, v int
	Scan(&n, &k)
	step := int(math.Sqrt(float64(n)))
	for gcd(step, n) > 1 {
		step++
	}
	for i := 0; ; i += step {
		Println("?", i%n+1)
		Scan(&v)
		if v == k {
			continue
		}
		if v < k {
			for j := i + 1; ; j++ {
				Println("?", j%n+1)
				Scan(&v)
				if v == k {
					Println("!", j%n+1)
					return
				}
			}
		} else {
			for j := i - 1; ; j-- {
				Println("?", (j%n+n)%n+1)
				Scan(&v)
				if v == k {
					Println("!", (j%n+n)%n+1)
					return
				}
			}
		}
	}
}