```go
package main
import (
"bufio"
"os"
"strconv"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
scanInt := func() int {
scanner.Scan()
val, _ := strconv.Atoi(scanner.Text())
return val
}
if !scanner.Scan() {
return
}
t, _ := strconv.Atoi(scanner.Text())
for i := 0; i < t; i++ {
n := scanInt()
m := scanInt()
k := scanInt()
q := scanInt()
xs := make([]int, q)
ys := make([]int, q)
for j := 0; j < q; j++ {
xs[j] = scanInt()
ys[j] = scanInt()
}
rowSeen := make(map[int]bool)
colSeen := make(map[int]bool)
rCnt := 0
cCnt := 0
activeOps := 0
for j := q - 1; j >= 0; j-- {
x := xs[j]
y := ys[j]
rExists := rowSeen[x]
cExists := colSeen[y]
isEffective := false
if !rExists && cCnt < m {
isEffective = true
}
if !cExists && rCnt < n {
isEffective = true
}
if isEffective {
activeOps++
}
if !rExists {
rowSeen[x] = true
rCnt++
}
if !cExists {
colSeen[y] = true
cCnt++
}
}
res := int64(1)
base := int64(k)
exp := int64(activeOps)
mod := int64(998244353)
for exp > 0 {
if exp%2 == 1 {
res = (res * base) % mod
}
base = (base * base) % mod
exp /= 2
}
writer.WriteString(strconv.FormatInt(res, 10))
writer.WriteByte('\n')
}
}
```