← Home
```go
package main

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

const MAXN = 100005

var (
	parent [MAXN]int
	last   [MAXN]int
	adj    [MAXN][]int
	tin    [MAXN]int
	tout   [MAXN]int
	depth  [MAXN]int
	timer  int
)

func dfs(u, d int) {
	timer++
	tin[u] = timer
	depth[u] = d
	for _, v := range adj[u] {
		dfs(v, d+1)
	}
	tout[u] = timer
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 2048*1024), 2048*1024)
	scanner.Split(bufio.ScanWords)

	readInt := func() int {
		scanner.Scan()
		val, _ := strconv.Atoi(scanner.Text())
		return val
	}

	n := readInt()
	_ = readInt()
	k := readInt()
	q := readInt()

	for i := 0; i < k; i++ {
		a := readInt()
		b := readInt()
		p := last[b]
		if p != 0 {
			parent[a] = p
		}
		last[b] = a
	}

	for i := 1; i <= n; i++ {
		p := parent[i]
		if p != 0 {
			adj[p] = append(adj[p], i)
		}
	}

	for i := 1; i <= n; i++ {
		if parent[i] == 0 {
			dfs(i, 0)
		}
	}

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	for i := 0; i < q; i++ {
		x := readInt()
		y := readInt()

		v := last[y]
		ans := 0

		if v != 0 {
			if tin[x] <= tin[v] && tout[x] >= tout[v] {
				ans = depth[v] - depth[x] + 1
			}
		}
		fmt.Fprintln(writer, ans)
	}
}
```