package main import . "fmt" import . "os" import bf "bufio" func main() { rd := bf.NewReader(Stdin) wr := bf.NewWriter(Stdout) defer wr.Flush() var n int Fscan(rd, &n) ds := make([]int, n) sum := make([]int, n+1) for i := range ds { Fscan(rd, &ds[i]) sum[i+1] = sum[i]+ds[i] } var q int Fscan(rd, &q) for ; q > 0; q-- { var y,m,d,k int Fscan(rd, &y, &m, &d, &k) y += k / sum[n] k = k % sum[n] if k <= ds[m-1]-d { Fprintln(wr, y, m, d+k) continue } r := sum[n]-sum[m]+ds[m-1]-d if k > r { k -= r+1 y++ m, d = 1, 1 } else { k -= ds[m-1]-d+1 m++ d = 1 } p := 0 for i := 15; i >= 0; i-- { t := p | (1 << i) if t > n { continue } if sum[t]-sum[m-1]-1 < k { p = t } } d += k-(sum[p]-sum[m-1]) m = p+1 if d>ds[p] { d-=ds[p] m++ } Fprintln(wr, y, m, d) } }