Cyclic shift of the matrix by k positions to the right

JavaScript Hard loops

Task «Cyclic shift of the matrix by k positions to the right»

Given a rectangular matrix of integers of size N x M. It is necessary to perform a cyclic shift of each row of the matrix to the right by k positions. The shift is performed independently for each row. If k is greater than M, the shift is equivalent to the remainder of k divided by M. It is required to output the resulting matrix. Use only loops (for, while) without built-in array shift functions.

Input Format

First line: two integers N and M (1 ≤ N, M ≤ 100). Next N lines, each contains M integers separated by spaces. Last line: an integer k (0 ≤ k ≤ 10^9).

Output Format

N lines, each containing M integers separated by spaces — the resulting matrix after the shift.

Examples

Example 1

INPUT
3 2 10 20 30 40 50 60 3
OUTPUT
20 10 40 30 60 50

Example 2

INPUT
1 4 7 8 9 10 2
OUTPUT
9 10 7 8
main.js