```go
package main
import (
"bufio"
"fmt"
"os"
)
type FastScanner struct {
r *bufio.Reader
}
func NewFastScanner() *FastScanner {
return &FastScanner{r: bufio.NewReader(os.Stdin)}
}
func (fs *FastScanner) NextInt() int {
sign := 1
val := 0
c, _ := fs.r.ReadByte()
for (c < '0' || c > '9') && c != '-' {
c, _ = fs.r.ReadByte()
}
if c == '-' {
sign = -1
c, _ = fs.r.ReadByte()
}
for c >= '0' && c <= '9' {
val = val*10 + int(c-'0')
c, _ = fs.r.ReadByte()
}
return val * sign
}
type BIT struct {
n int
t []int
}
func NewBIT(n int) *BIT {
return &BIT{n: n, t: make([]int, n+2)}
}
func (b *BIT) Add(i, delta int) {
for i <= b.n {
b.t[i] += delta
i += i & -i
}
}
func (b *BIT) Sum(i int) int {
s := 0
for i > 0 {
s += b.t[i]
i -= i & -i
}
return s
}
func (b *BIT) FindByOrder(k int) int {
idx := 0
bit := 1
for (bit << 1) <= b.n {
bit <<= 1
}
for bit > 0 {
next := idx + bit
if next <= b.n && b.t[next] < k {
k -= b.t[next]
idx = next
}
bit >>= 1
}
return idx + 1
}
func main() {
fs := NewFastScanner()
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
t := fs.NextInt()
for ; t > 0; t-- {
n, k, d := fs.NextInt(), fs.NextInt(), fs.NextInt()
a := make([]int, n)
for i := 0; i < n; i++ {
a[i] = fs.NextInt()
}
v := make([]int, k)
for i := 0; i < k; i++ {
v[i] = fs.NextInt()
}
// buckets of indices by value of v[i]
indicesByVal := make([][]int, n+1)
for i := 0; i < k; i++ {
w := v[i]
if w > n {
w = n
}
indicesByVal[w] = append(indicesByVal[w], i)
}
bit := NewBIT(k)
alive := 0
base0 := 0
counts := make(map[int]int) // T -> multiplicity, for first segment events
// process thresholds s from n down to 1
for s := n; s >= 1; s-- {
// add indices with v == s
for _, idx := range indicesByVal[s] {
bit.Add(idx+1, 1)
alive++
}
rNeed := s - a[s-1]
if rNeed == 0 {
base0++
} else if rNeed > 0 && alive > 0 {
cnt := alive
q := (rNeed - 1) / cnt
rem := (rNeed - 1) % cnt
idx1 := bit.FindByOrder(rem + 1) // 1..k
posWithinCycle := idx1
T := q*k + posWithinCycle
if T <= d-1 {
counts[T]++
}
}
}
ans := 0
// candidate t = 0
if d-1 >= 0 {
remDays := d - (0 + 1)
total := base0 + remDays/2
if total > ans {
ans = total
}
}
// other candidates from counts
for T, mult := range counts {
if T > d-1 {
continue
}
remDays := d - (T + 1)
if remDays < 0 {
continue
}
total := mult + remDays/2
if total > ans {
ans = total
}
}
fmt.Fprintln(out, ans)
}
}
```