#include<bits/stdc++.h>
#define ll long long int
//#define g getchar_unlocked
using namespace std;
/*int readnum() {
    int n = 0;
    char c = g();
    while(c < '0' || c > '9') c = g();
    while(c >= '0' && c <= '9') n = 10 * n + c - '0', c = g();
    return n;
}*/
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int n;
    cin >> n;
    int cnt = 1, start, end, req;
    int odd = 1, even = 2, flag = 0;
    for(int i = 0 ; i < n ; ++i) {
        req = cnt;
        if(flag || cnt == n) {
            cnt -= 2;
            flag = 1;
        }
        else
            cnt += 2;
        start = (n - req) / 2;
        end = n - start;
        for(int j = 0 ; j < n ; ++j) {
            if(j >= start && j < end) {
                cout << odd << " ";
                odd += 2;
            }
            else {
                cout << even << " ";
                even += 2;
            }
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
//#define g getchar_unlocked
using namespace std;
/*int readnum() {
    int n = 0;
    char c = g();
    while(c < '0' || c > '9') c = g();
    while(c >= '0' && c <= '9') n = 10 * n + c - '0', c = g();
    return n;
}*/
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int n;
    cin >> n;
    int odd, even;
    if(n % 4 == 3)
        odd = 1, even = 2;
    else
        odd = 2, even = 1;
    for(int i = 0 ; i < n ; ++i) {
        for(int j = 0 ; j < n ; ++j) {
            if((i == j && i&1) || ((i + j) & 1)) {
                cout << odd << " ";
                odd += 2;
            }
            else {
                cout << even << " ";
                even += 2;
            }
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
//#define g getchar_unlocked
using namespace std;
/*int readnum() {
    int n = 0;
    char c = g();
    while(c < '0' || c > '9') c = g();
    while(c >= '0' && c <= '9') n = 10 * n + c - '0', c = g();
    return n;
}*/
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int n;
    cin >> n;
    int cnt = 1, start, end, req;
    int odd = 1, even = 2;
    for(int i = 0 ; i < n ; ++i) {
        req = cnt;
        if(cnt == n)
            cnt -= 2;
        else
            cnt += 2;
        start = (n - req) / 2;
        end = n - start;
        for(int j = 0 ; j < n ; ++j) {
            if(j >= start && j < end) {
                cout << odd << " ";
                odd += 2;
            }
            else {
                cout << even << " ";
                even += 2;
            }
        }
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
//#define g getchar_unlocked
using namespace std;
/*int readnum() {
    int n = 0;
    char c = g();
    while(c < '0' || c > '9') c = g();
    while(c >= '0' && c <= '9') n = 10 * n + c - '0', c = g();
    return n;
}*/

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int n;
    cin >> n;
    cout << "TESTING STOPSTALK";
    int cnt = 1, start, end, req;
    int odd = 1, even = 2, flag = 0;
    for(int i = 0 ; i < n ; ++i) {
        req = cnt;
        if(flag || cnt == n) {
            cnt -= 2;
            flag = 1;
        }
        else
            cnt += 2;
        start = (n - req) / 2;
        end = n - start;
        for(int j = 0 ; j < n ; ++j) {
            if(j >= start && j < end) {
                cout << odd << " ";
                odd += 2;
            }
            else {
                cout << even << " ";
                even += 2;
            }
        }
        cout << endl;
    }
    return 0;
}


//IO
#include <iostream>
#include <cstdio>
#include <iomanip>

//General
#include <cmath>
#include <cstdlib>
#include <cstring>

//STL
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <algorithm>

using namespace std;

const int INF = 1E9;

int Magic[100][100];
int H3[3][3] = {{2, 1, 4}, {3, 5, 7}, {6, 9, 8}};

void BuildMagic (int h) {
	if (h > 3) {
		BuildMagic (h - 2);
		int num[2];
		if (h % 2) {
			num[1] = h * h;
			num[0] = num[1] - 1;
		} else {
			num[1] = h * h - 1;
			num[0] = num[1] + 1;
		}
		for (int i = 0; i <= h - 3; i++)
			for (int j = h - 2; j <= h - 1; j++) {
				Magic[i][j] = num[i % 2];
				num[i % 2] -= 2;
			}
		for (int j = 0; j <= h - 3; j++)
			for (int i = h - 2; i <= h - 1; i++) {
				Magic[i][j] = num[(i + 1) % 2];
				num[(i + 1) % 2] -= 2;
			}
		Magic[h - 2][h - 2] = num[1];
		num[1] -= 2;
		Magic[h - 1][h - 1] = num[1];
		Magic[h - 2][h - 1] = num[0];
		num[0] -= 2;
		Magic[h - 1][h - 2] = num[0];
	}
	if (h == 1) {
		Magic[0][0] = 1;
	}
	if (h == 3) {
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				Magic[i][j] = H3[i][j];
	}
}

int main () {
	ios::sync_with_stdio (false);
	cin.tie (0);
	cout.tie (0);
	int N;
	cin >> N;
	BuildMagic (N);
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cout << Magic[i][j] << " \n"[j == N - 1];
	return 0;
}


//IO
#include <iostream>
#include <cstdio>
#include <iomanip>

//General
#include <cmath>
#include <cstdlib>
#include <cstring>

//STL
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <algorithm>

using namespace std;

const int INF = 1E9;

int Magic[100][100];
int H3[3][3] = {{2, 1, 4}, {3, 5, 7}, {6, 9, 8}};

void BuildMagic (int h) {
	if (h > 3) {
		BuildMagic (h - 2);
		int num[2];
		if (h % 2) {
			num[1] = h * h;
			num[0] = num[1] - 1;
		} else {
			num[1] = h * h - 1;
			num[0] = num[1] + 1;
		}
		for (int i = 0; i <= h - 3; i++)
			for (int j = h - 2; j <= h - 1; j++) {
				Magic[i][j] = num[i % 2];
				num[i % 2] -= 2;
			}
		for (int j = 0; j <= h - 3; j++)
			for (int i = h - 2; i <= h - 1; i++) {
				Magic[i][j] = num[(i + 1) % 2];
				num[(i + 1) % 2] -= 2;
			}
		Magic[h - 2][h - 2] = num[1];
		num[1] -= 2;
		Magic[h - 1][h - 1] = num[1];
		Magic[h - 2][h - 1] = num[0];
		num[0] -= 2;
		Magic[h - 1][h - 2] = num[0];
	}
	if (h == 2) {
		Magic[1][0] = 1;
		Magic[0][0] = 2;
		Magic[1][1] = 4;
		Magic[0][1] = 3;
	}
	if (h == 3) {
		for (int i = 0; i < 3; i++)
			for (int j = 0; j < 3; j++)
				Magic[i][j] = H3[i][j];
	}
}

int main () {
	ios::sync_with_stdio (false);
	cin.tie (0);
	cout.tie (0);
	int N;
	cin >> N;
	BuildMagic (N);
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cout << Magic[i][j] << " \n"[j == N - 1];
	return 0;
}


//IO
#include <iostream>
#include <cstdio>
#include <iomanip>

//General
#include <cmath>
#include <cstdlib>
#include <cstring>

//STL
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <algorithm>

using namespace std;

const int INF = 1E9;

int N;
int Magic[100][100];

int main () {
	ios::sync_with_stdio (false);
	cin.tie (0);
	cout.tie (0);
	cin >> N;
	int odd = 1, even = 2;
	for (int i = 0; i < N; i++) {
		int j;
		for (j = 0; j < abs (N - 2 * i - 1) / 2; j++) {
			Magic[i][j] = even;
			even += 2;
		}
		for (; j < N - abs (N - 2 * i - 1) / 2; j++) {
			Magic[i][j] = odd;
			odd += 2;
		}
		for (; j < N; j++) {
			Magic[i][j] = even;
			even += 2;
		}
	}
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cout << Magic[i][j] << " \n"[j == N - 1];
	return 0;
}


#include <iostream>

using namespace std;
int even1=0;
int even(){
    even1+=2;
    return even1;
}
int odd1=-1;
int odd(){
    odd1+=2;
    return odd1;
}
int n;
bool ans[54][54];
void show(){
    int s=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout <<ans[i][j]<<" ";
            if(ans[i][j])
                s++;
        }
        cout <<endl;
    }
    cout <<s;
}
int main(int argc, char *argv[]) {

    cin >>n;
    //end input
    //	for(int i=1;i<=n;i++)
    //	ans[i][n-i+1]=1;
    //
    if(n%4==1){
        int k=n/4;
        for(int i=1;i<=n;i++)
            ans[i][n-i+1]=1;
        for(int i=1;i<=2*k;i++){
            for(int j=1;j<=2*k;j++)
                ans[i][j]=1;
        }
        for(int i=n;i>n-2*k;i--){
            for(int j=n;j>n-2*k;j--)
                ans[i][j]=1;
        }
        //show();
    }
    else {
        for(int i=1;i<=n;i++){
            ans[n/2+1][i]=1;
            ans[i][n/2+1]=1;
        }
        int k=n/4;
        for(int i=1;i<=k;i++){
            for(int j=1;j<=2*k+1;j++)
                ans[i][j]=1;
        }
        for(int i=1;i<=k;i++){
            for(int j=2*k+2;j<=n;j++)
                ans[i][j]=1;
        }
        for(int i=n;i>n-k;i--){
            for(int j=1;j<=2*k+1;j++)
                ans[i][j]=1;
        }
        for(int i=n;i>n-k;i--){
            for(int j=n;j>n-2*k-1;j--)
                ans[i][j]=1;
        }

        //cout<<k<<endl;
        //show();
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(ans[i][j]==1)
                cout <<odd()<<" ";
            else
                cout <<even()<<" ";
        }
        cout <<endl;
    }
}


#include <iostream>

using namespace std;
int ans[52][52];
int n;
int n2;
int i=0;
int give(){
	i++;
	return i;
}
int main(int argc, char *argv[]) {

	cin >>n;
	n2=n*n;
	//
	if(n%4==1){
			for(int i=0;i<n;i++){
				int k=i;
				for(int j=0;j<=i;j++){
					ans[k][j]=give();
					k--;
				}
			}
			//
			for(int i=1;i<n;i++){
				int k=i;
				for(int j=n-1;k<n;j--){
					ans[j][k]=give();
					k++;
				}
			}
	}
	else {
		for(int i=0;i<n;i++){
			int k=i;
			for(int j=0;j<=i;j++){
				ans[j][k]=give();
				k--;
			}
		}
		//
		for(int i=1;i<n;i++){
			int k=i;
			for(int j=n-1;k<n;j--){
				ans[k][j]=give();
				k++;
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		cout<<ans[i][j]<<" ";
		cout <<endl;
	}

}


#include <iostream>
int n;
int ans[53][53];
using namespace std;
int odd=-1;
int even=0;
int give_even(){
    even+=2;
    return even;
}
int give_odd(){
    odd+=2;
    return odd;
}

int main(int argc, char *argv[]) {
    cin >>n;
    ans[1][1]=give_even();
    ans[1][2]=give_odd();
    ans[1][3]=give_even();
    ans[2][1]=give_odd();
    ans[2][2]=give_odd();
    ans[2][3]=give_odd();
    ans[3][1]=give_even();
    ans[3][2]=give_odd();
    ans[3][3]=give_even();
    //end input
    bool con=false;
    if(n==1){
        cout <<1;
        return 0;
    }
    for(int i=4;i<=n;i+=2){
        if(con){
            con=false;
            ans[i][i]=give_even();
            ans[i+1][i]=give_even();
            ans[i][i+1]=give_even();
            ans[i+1][i+1]=give_even();
            //end
            bool ch=true;
            for(int j=i-1;j>=1;j--){
                if(ch){
                    ch=false;
                    ans[i][j]=give_odd();
                    ans[i+1][j]=give_odd();
                    ans[j][i]=give_odd();
                    ans[j][i+1]=give_odd();
                }
                else {
                    ch=true;
                    ans[i][j]=give_even();
                    ans[i+1][j]=give_even();
                    ans[j][i]=give_even();
                    ans[j][i+1]=give_even();
                }

            }

        }
        else {
            con=true;
            //
            ans[i][i]=give_odd();
            ans[i+1][i]=give_odd();
            ans[i][i+1]=give_odd();
            ans[i+1][i+1]=give_odd();
            //end
            bool ch=true;
            for(int j=i-1;j>=1;j--){
                if(ch){
                    ch=false;
                    ans[i][j]=give_even();
                    ans[i+1][j]=give_even();
                    ans[j][i]=give_even();
                    ans[j][i+1]=give_even();
                }
                else {
                    ch=true;
                    ans[i][j]=give_odd();
                    ans[i+1][j]=give_odd();
                    ans[j][i]=give_odd();
                    ans[j][i+1]=give_odd();
                }

            }

        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            cout <<ans[i][j]<<" ";
        cout <<endl;
    }

}


#include <iostream>
int n;
int ans[53][53];
using namespace std;
int odd=-1;
int even=0;
int give_even(){
    even+=2;
    return even;
}
int give_odd(){
    odd+=2;
    return odd;
}

int main(int argc, char *argv[]) {
    cin >>n;
    ans[1][1]=give_even();
    ans[1][2]=give_odd();
    ans[1][3]=give_even();
    ans[2][1]=give_odd();
    ans[2][2]=give_odd();
    ans[2][3]=give_odd();
    ans[3][1]=give_even();
    ans[3][2]=give_odd();
    ans[3][3]=give_even();
    //end input
    bool con=true;

    for(int i=4;i<=n;i+=2){
        if(con){
            con=false;
            ans[i][i]=give_even();
            ans[i+1][i]=give_even();
            ans[i][i+1]=give_even();
            ans[i+1][i+1]=give_even();
            //end
            bool ch=true;
            for(int j=i-1;j>=1;j--){
                if(ch){
                    ch=false;
                    ans[i][j]=give_odd();
                    ans[i+1][j]=give_odd();
                    ans[j][i]=give_odd();
                    ans[j][i+1]=give_odd();
                }
                else {
                    ch=true;
                    ans[i][j]=give_even();
                    ans[i+1][j]=give_even();
                    ans[j][i]=give_even();
                    ans[j][i+1]=give_even();
                }

            }

        }
        else {
            con=true;
            //
            ans[i][i]=give_odd();
            ans[i+1][i]=give_odd();
            ans[i][i+1]=give_odd();
            ans[i+1][i+1]=give_odd();
            //end
            bool ch=true;
            for(int j=i-1;j>=1;j--){
                if(ch){
                    ch=false;
                    ans[i][j]=give_even();
                    ans[i+1][j]=give_even();
                    ans[j][i]=give_even();
                    ans[j][i+1]=give_even();
                }
                else {
                    ch=true;
                    ans[i][j]=give_odd();
                    ans[i+1][j]=give_odd();
                    ans[j][i]=give_odd();
                    ans[j][i+1]=give_odd();
                }

            }

        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            cout <<ans[i][j]<<" ";
        cout <<endl;
    }

}


#include <iostream>
int n;
int ans[53][53];
using namespace std;
int odd=-1;
int even=0;
int give_even(){
    even+=2;
    return even;
}
int give_odd(){
    odd+=2;
    return odd;
}

int main(int argc, char *argv[]) {
    cin >>n;
    ans[1][1]=give_even();
    ans[1][2]=give_odd();
    ans[1][3]=give_even();
    ans[2][1]=give_odd();
    ans[2][2]=give_odd();
    ans[2][3]=give_odd();
    ans[3][1]=give_even();
    ans[3][2]=give_odd();
    ans[3][3]=give_even();
    //end input
    bool con=true;
    if(n==1){
        cout <<1;
        return 0;
    }
    for(int i=4;i<=n;i+=2){
        if(con){
            con=false;
            ans[i][i]=give_even();
            ans[i+1][i]=give_even();
            ans[i][i+1]=give_even();
            ans[i+1][i+1]=give_even();
            //end
            bool ch=true;
            for(int j=i-1;j>=1;j--){
                if(ch){
                    ch=false;
                    ans[i][j]=give_odd();
                    ans[i+1][j]=give_odd();
                    ans[j][i]=give_odd();
                    ans[j][i+1]=give_odd();
                }
                else {
                    ch=true;
                    ans[i][j]=give_even();
                    ans[i+1][j]=give_even();
                    ans[j][i]=give_even();
                    ans[j][i+1]=give_even();
                }

            }

        }
        else {
            con=true;
            //
            ans[i][i]=give_odd();
            ans[i+1][i]=give_odd();
            ans[i][i+1]=give_odd();
            ans[i+1][i+1]=give_odd();
            //end
            bool ch=true;
            for(int j=i-1;j>=1;j--){
                if(ch){
                    ch=false;
                    ans[i][j]=give_even();
                    ans[i+1][j]=give_even();
                    ans[j][i]=give_even();
                    ans[j][i+1]=give_even();
                }
                else {
                    ch=true;
                    ans[i][j]=give_odd();
                    ans[i+1][j]=give_odd();
                    ans[j][i]=give_odd();
                    ans[j][i+1]=give_odd();
                }

            }

        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            cout <<ans[i][j]<<" ";
        cout <<endl;
    }

}


#include <iostream>
int n;
int ans[53][53];
using namespace std;
int odd=-1;
int even=0;
int give_even(){
    even+=2;
    return even;
}
int give_odd(){
    odd+=2;
    return odd;
}

int main(int argc, char *argv[]) {
    cin >>n;
    ans[1][1]=give_even();
    ans[1][2]=give_odd();
    ans[1][3]=give_even();
    ans[2][1]=give_odd();
    ans[2][2]=give_odd();
    ans[2][3]=give_odd();
    ans[3][1]=give_even();
    ans[3][2]=give_odd();
    ans[3][3]=give_even();
    //end input
    bool con=false;

    for(int i=4;i<=n;i+=2){
        if(con){
            con=false;
            ans[i][i]=give_even();
            ans[i+1][i]=give_even();
            ans[i][i+1]=give_even();
            ans[i+1][i+1]=give_even();
            //end
            bool ch=true;
            for(int j=i-1;j>=1;j--){
                if(ch){
                    ch=false;
                    ans[i][j]=give_odd();
                    ans[i+1][j]=give_odd();
                    ans[j][i]=give_odd();
                    ans[j][i+1]=give_odd();
                }
                else {
                    ch=true;
                    ans[i][j]=give_even();
                    ans[i+1][j]=give_even();
                    ans[j][i]=give_even();
                    ans[j][i+1]=give_even();
                }

            }

        }
        else {
            con=true;
            //
            ans[i][i]=give_odd();
            ans[i+1][i]=give_odd();
            ans[i][i+1]=give_odd();
            ans[i+1][i+1]=give_odd();
            //end
            bool ch=true;
            for(int j=i-1;j>=1;j--){
                if(ch){
                    ch=false;
                    ans[i][j]=give_even();
                    ans[i+1][j]=give_even();
                    ans[j][i]=give_even();
                    ans[j][i+1]=give_even();
                }
                else {
                    ch=true;
                    ans[i][j]=give_odd();
                    ans[i+1][j]=give_odd();
                    ans[j][i]=give_odd();
                    ans[j][i+1]=give_odd();
                }

            }

        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++)
            cout <<ans[i][j]<<" ";
        cout <<endl;
    }

}


#include <iostream>
int n;
bool ans[53][53];
using namespace std;
int odd=-1;
int even=0;
int give_even(){
    even+=2;
    return even;
}
int give_odd(){
    odd+=2;
    return odd;
}

int main(int argc, char *argv[]) {

    cin >>n;
    if(n%4==1){
        for(int i=1;i<=n;i++){
            ans[i][n/2+1]=1;
            ans[n/2+1][i]=1;
        }
        for(int i=1;i<=n/2;i++)
            for(int j=1;j<=n/2;j++)
                ans[i][j]=1;
    }
    else {

        ans[n/2][n/2]=1;
        ans[n/2+2][n/2+2]=1;
        ans[n/2][n/2+2]=1;
        ans[n/2+2][n/2]=1;
        for(int i=1;i<=n;i++){
            ans[i][n/2+1]=1;
            ans[n/2+1][i]=1;
        }
        for(int i=1;i<n/2;i++)
            for(int j=1;j<n/2;j++)
                ans[i][j]=1;
        //
        for(int i=1;i<n/2;i++)
            for(int j=n/2+3;j<=n;j++)
                ans[i][j]=1;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(ans[i][j]==1)
                cout <<give_odd()<<" ";
            else
                cout <<give_even()<<" ";
        }
        cout <<endl;
    }
    //

}


#include <stdio.h>
#include <conio.h>
int main()
{
int n, i, j, c, a[9][9] ;

scanf("%d", &n) ;
if (n % 2 == 0)
{
printf("\nMagic square is not possible") ;
goto end ;
}

j = (n + 1) / 2 ;
i = 1 ;
for(c = 1 ; c <= n * n ; c++)
{
a[i][j] = c ;
if(c % n == 0)
{
i = (i + 1);
goto loop ;
}
if(i == 1)
i = n ;
else
i = i - 1 ;
if(j == n)
j = 1;
else
j = j + 1 ;
loop : ;
}
for (i = 1 ; i <= n ; i++)
{
for (j = 1 ; j <= n ; j++)
{
printf("%d ", a[i][j]) ;
}
printf("\n") ;
}
end : ;
getch() ;
}


#include<bits/stdc++.h>

using namespace std;

void generateSquare(int n)
{
    int magicSquare[n][n];

    // set all slots as 0
    memset(magicSquare, 0, sizeof(magicSquare));

    // Initialize position for 1
    int i = n/2;
    int j = n-1;

    // One by one put all values in magic square
    for (int num=1; num <= n*n; )
    {
        if (i==-1 && j==n) //3rd condition
        {
            j = n-2;
            i = 0;
        }
        else
        {
            //1st condition helper if next number goes to out of square's right side
            if (j == n)
                j = 0;
            //1st condition helper if next number is goes to out of square's upper side
            if (i < 0)
                i=n-1;
        }
        if (magicSquare[i][j]) //2nd condition
        {
            j -= 2;
            i++;
            continue;
        }
        else
            magicSquare[i][j] = num++; //set number

        j++;  i--; //1st condition
    }

    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
            printf("%d", magicSquare[i][j]);
        printf("\n");
    }
}

int main()
{
    int n;

    while(cin>>n)
    {
        generateSquare(n);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

void generateSquare(int n)
{
    int magicSquare[n][n];

    // set all slots as 0
    memset(magicSquare, 0, sizeof(magicSquare));

    // Initialize position for 1
    int i = n/2;
    int j = n-1;

    // One by one put all values in magic square
    for (int num=1; num <= n*n; )
    {
        if (i==-1 && j==n) //3rd condition
        {
            j = n-2;
            i = 0;
        }
        else
        {
            //1st condition helper if next number goes to out of square's right side
            if (j == n)
                j = 0;
            //1st condition helper if next number is goes to out of square's upper side
            if (i < 0)
                i=n-1;
        }
        if (magicSquare[i][j]) //2nd condition
        {
            j -= 2;
            i++;
            continue;
        }
        else
            magicSquare[i][j] = num++; //set number

        j++;  i--; //1st condition
    }

    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            printf("%d", magicSquare[i][j]);

            if(j!=n-1)
                printf(" ");
        }
        printf("\n");
    }
}

int main()
{
    int n;

    while(cin>>n)
    {
        generateSquare(n);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    vector<int>cycle;

    int n,c;
    int ver;
    int vertice[100010];

    while(cin>>n)
    {
        for(int i=0; i<n; i++)
        {
            cin>>ver;
            vertice[i]=ver;
        }

        for(int j=0; j<n; j++)
        {

            for(int in=1; in<=vertice[j]; in++)
                cycle.push_back(in);

            c=1;

            bool first=true;
            bool second= true;
            int num;

            while(first==true && second==true)
            {
                for(int i=0; i<cycle.size(); i++)
                    if(cycle[i]==1)
                        cycle.erase(cycle.begin()+i);

                if(!cycle.empty())
                {
                    num=cycle.back();
                    cycle.pop_back();

                    cycle.push_back(num-1);
                }
                else
                {
                    if(c%2==1)
                        first=false;
                    else
                        second=false;
                }

                c++;

            }
            if(first)
                cout<<"1"<<endl;
            else
                cout<<"2"<<endl;

            cycle.clear();

        }

    }
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

int n, num = 1, a[50][50], cnt = 1;

int main() {
	scanf("%d", &n);
	for (int i = (n+1)/2, j = i; i <= n; i += 2, j -= 2) {
		for (int k = cnt; k <= n-cnt; k++) {
			if (!a[i][k]) a[i][k] = num, num += 2;
			if (!a[k][i]) a[k][i] = num, num += 2;
			if (!a[j][k]) a[j][k] = num, num += 2;
			if (!a[k][j]) a[k][j] = num, num += 2;
		}
		cnt++;
	}
	num = 2;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++) {
			if (!a[i][j]) a[i][j] = num, num += 2;
			printf("%d%c", a[i][j], " \n"[j==n]);
		}
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

int n, num = 1, a[50][50], cnt = 1;

int main() {
	scanf("%d", &n);
	for (int i = (n+1)/2, j = i; i <= n; i += 2, j -= 2) {
		for (int k = cnt; k <= n-cnt+1; k++) {
			if (!a[i][k]) a[i][k] = num, num += 2;
			if (!a[k][i]) a[k][i] = num, num += 2;
			if (!a[j][k]) a[j][k] = num, num += 2;
			if (!a[k][j]) a[k][j] = num, num += 2;
		}
		cnt++;
	}
	num = 2;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++) {
			if (!a[i][j]) a[i][j] = num, num += 2;
			printf("%d%c", a[i][j], " \n"[j==n]);
		}
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

int n, num = 1, a[50][50], cnt = 1;

int main() {
	scanf("%d", &n);
	for (int i = (n+1)/2, j = i; i <= n; i++, j--) {
		for (int k = cnt; k <= n-cnt+1; k++) {
			if (!a[i][k]) a[i][k] = num, num += 2;
			if (!a[k][i]) a[k][i] = num, num += 2;
			if (!a[j][k]) a[j][k] = num, num += 2;
			if (!a[k][j]) a[k][j] = num, num += 2;
		}
		cnt++;
	}
	num = 2;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++) {
			if (!a[i][j]) a[i][j] = num, num += 2;
			printf("%d%c", a[i][j], " \n"[j==n]);
		}
	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

int n, num = 1, a[50][50];

int main() {
	scanf("%d", &n);
	for (int i = (n+1)/2, j = i; i <= n; i += 2, j -= 2) {
		for (int k = 1; k <= n; k++) {
			if (!a[i][k]) a[i][k] = num, num += 2;
			if (!a[k][i]) a[k][i] = num, num += 2;
			if (!a[j][k]) a[j][k] = num, num += 2;
			if (!a[k][j]) a[k][j] = num, num += 2;
		}
	}
	num = 2;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++) {
			if (!a[i][j]) a[i][j] = num, num += 2;
			printf("%d%c", a[i][j], " \n"[j==n]);
		}
	return 0;
}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mod 1000000007
#define mt(A) memset(A,0,sizeof(A))
using namespace std;
typedef long long LL;
const int N=1000000+10;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
int a[100][100];
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    int i,j,k=2,n,m;
    cin>>n;
    mt(a);
    for(i=1;i<=n;i+=2)
    {
        if(i%2)
        {
            for(j=1;j<=n;j+=2)
            {
                if((i==(n/2+1)&&(j==(n/2+1))))continue;
                a[i][j]=k;
                k+=2;
            }
        }
        else
        {
            for(j=2;j<=n;j+=2)
            {
                a[i][j]=k;
                k+=2;
            }
        }
    }
    k=1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(!a[i][j])
            {
                a[i][j]=k;
                k+=2;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(j==n)cout<<a[i][j]<<endl;
            else cout<<a[i][j]<<" ";
        }
    }
    return 0;

}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mod 1000000007
#define mt(A) memset(A,0,sizeof(A))
using namespace std;
typedef long long LL;
const int N=1000000+10;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
int a[100][100];
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    int i,j,k=2,n,m;
    cin>>n;
    mt(a);
    for(i=1;i<=n;i++)
    {
        if(i!=(n/2)+1)
        {
            a[1][i]=k;
            k+=2;
            a[n][i]=k;
            k+=2;
        }
    }
    for(i=2;i<=n-1;i++)
    {
        if(i!=(n/2)+1)
        {
            a[i][1]=k;
            k+=2;
            a[i][n]=k;
            k+=2;
        }
    }
    k=1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(!a[i][j])
            {
                a[i][j]=k;
                k+=2;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(j==n)cout<<a[i][j]<<endl;
            else cout<<a[i][j]<<" ";
        }
    }
    return 0;

}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mod 1000000007
#define mt(A) memset(A,0,sizeof(A))
using namespace std;
typedef long long LL;
const int N=1000000+10;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
int a[100][100];
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    int i,j,k=2,n,m;
    cin>>n;
    mt(a);
    for(i=1;i<=n;i++)
    {
        if(i%2)
        {
            for(j=1;j<=n;j+=2)
            {
                if((i==(n/2+1)&&(j==(n/2+1))))continue;
                a[i][j]=k;
                k+=2;
            }
        }
        else
        {
            for(j=2;j<=n;j+=2)
            {
                a[i][j]=k;
                k+=2;
            }
        }
    }
    k=1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(!a[i][j])
            {
                a[i][j]=k;
                k+=2;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(j==n)cout<<a[i][j]<<endl;
            else cout<<a[i][j]<<" ";
        }
    }
    return 0;

}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mod 1000000007
#define mt(A) memset(A,0,sizeof(A))
using namespace std;
typedef long long LL;
const int N=1000000+10;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
int a[100][100];
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    int i,j,k=2,n,m,p,q,d;
    cin>>n;
    mt(a);
    for(i=1;i<=n;i++)
    {
        if(i!=(n/2)+1)
        {
            a[1][i]=k;
            k+=2;
            a[n][i]=k;
            k+=2;
        }
    }
    for(i=2;i<=n-1;i++)
    {
        if(i!=(n/2)+1)
        {
            a[i][1]=k;
            k+=2;
            a[i][n]=k;
            k+=2;
        }
    }
    p=(n*n/2-(4*n-8))/4;
    for(i=2,d=2;p;i++)
    {
        a[i][d]=k;k+=2;
        a[i][n-d+1]=k;k+=2;
        a[n-i+1][d]=k;k+=2;
        a[n-i+1][n-d+1]=k;k+=2;
        d++;p--;
    }
    k=1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(!a[i][j])
            {
                a[i][j]=k;
                k+=2;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(j==n)cout<<a[i][j]<<endl;
            else cout<<a[i][j]<<" ";
        }
    }
    return 0;

}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mod 1000000007
#define mt(A) memset(A,0,sizeof(A))
using namespace std;
typedef long long LL;
const int N=100+10;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
int a[N][N]= {0};
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    int i=0,j,k,n;    /*先令所有元素都为0*/
    scanf("%d",&n);
    j=(n-1)/2;
    i=0;
    for(k=1; k<=n*n;)   /*开始处理*/
    {
        if((i<0)&&(j==n))   /*前一个数是第一行第N列时,把下一个数放在上一个数的下面*/
        {
            i=i+2;
            j=j-1;
        }
        else  if(i<0)   /*当行数减到第一行，返回到最后一行*/
            i=n-1;
        else  if(j>n-1)   /*当列数加到最后一行，返回到第一行*/
            j=0;
        else  if(!a[i][j])      /*如果该元素为0,继续执行程序*/
        {
            a[i][j]=k++;
            i=i-1;
            j=j+1;
        }
        else     /*如果该元素不为0,就说明要填的数的位置已经被占,则该数放在上一个数的下面*/
        {
            i=i+2;
            j=j-1;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(j==n-1)cout<<a[i][j]<<endl;
            else cout<<a[i][j]<<" ";
        }
    }

}


#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mod 1000000007
#define mt(A) memset(A,0,sizeof(A))
using namespace std;
typedef long long LL;
const int N=1000000+10;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
int a[100][100];
int main()
{
#ifdef Local
    freopen("data.txt","r",stdin);
#endif
    int n=0,i,j,k=1,p,q,a[20][20]= {0};
    scanf("%d",&n);
    i=0;
    j=n/2;
    for(p=0; p<n; p++)
    {
        for(q=0; q<n; q++)
        {
            if(q>0)
            {
                i=(i+n-1)%n;
                j=(j+1)%n;
            }
            a[i][j]=k++;
        }
        i=(i+1)%n;
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            if(j==n-1)printf("%d\n",a[i][j]);
            else printf("%d ",a[i][j]);
        }

    }
    return 0;

}


#include <iostream>

using namespace std;

int main()
{
    int n;
    int arr[50][50];
    for(int i = 0; i <= 50; ++i)
        for(int j = 0; j <= 50; ++j)
            arr[i][j] = 0;
    cin >> n;
    int k = 1;
    for(int j = 1; j <= n; ++j)
    {
        arr[n/2+1][j] = k;
        k += 2;
    }
    for(int i = 1; i <= n; ++i)
    {
        if(i == n/2+1)
            continue;
        for(int j = min(i+1, n-i+2); j <= max(i-1, n-i); ++j)
        {
            arr[i][j] = k;
            k += 2;
        }
    }
    k = 2;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            if(arr[i][j] == 0)
            {
                arr[i][j] = k;
                k += 2;
            }
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j < n; ++j)
            cout << arr[i][j] << " ";
        cout << arr[i][n] << endl;
    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    int arr[50][50];
    for(int i = 0; i < 50; ++i)
        for(int j = 0; j < 50; ++j)
            arr[i][j] = 0;
    cin >> n;
    int k = 1;
    for(int j = 1; j <= n; ++j)
    {
        arr[n/2+1][j] = k;
        k += 2;
    }
    for(int i = 1; i <= n; ++i)
    {
        if(i == n/2+1)
            continue;
        for(int j = min(i+1, n-i+2); j <= max(i-1, n-i); ++j)
        {
            arr[i][j] = k;
            k += 2;
        }
    }
    k = 2;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            if(arr[i][j] == 0)
            {
                arr[i][j] = k;
                k += 2;
            }
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j < n; ++j)
            cout << arr[i][j] << " ";
        cout << arr[i][n] << endl;
    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    int arr[50][50];
    cin >> n;
    int k = 1;
    for(int j = 1; j <= n; ++j)
    {
        arr[n/2+1][j] = k;
        k += 2;
    }
    for(int i = 1; i <= n; ++i)
    {
        if(i == n/2+1)
            continue;
        for(int j = min(i+1, n-i+2); j <= max(i-1, n-i); ++j)
        {
            arr[i][j] = k;
            k += 2;
        }
    }
    k = 2;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            if(arr[i][j] == 0)
            {
                arr[i][j] = k;
                k += 2;
            }
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j < n; ++j)
            cout << arr[i][j] << " ";
        cout << arr[i][n] << endl;
    }
    return 0;
}


# include <bits/stdc++.h>

using namespace std;

const int MAXN = 50;

int a[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int A = 1, B = 2;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if ((i + j) % 2 == 0)
                a[i][j] = A, A += 2;
            else
                a[i][j] = B, B += 2;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << a[i][j] << " ";
        cout << '\n';
    }

    return 0;
}


# include <bits/stdc++.h>

using namespace std;

const int MAXN = 50;

int a[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int A = 1, B = 2;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (i % 2 == 1 || j == i)
                a[i][j] = A, A += 2;
            else
                a[i][j] = B, B += 2;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << a[i][j] << " ";
        cout << '\n';
    }

    return 0;
}


# include <bits/stdc++.h>

using namespace std;

const int MAXN = 50;

int a[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int A = 1, B = 2;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (i % 2 == 1 || j == (n - 1) / 2)
                a[i][j] = A, A += 2;
            else
                a[i][j] = B, B += 2;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << a[i][j] << " ";
        cout << '\n';
    }

    return 0;
}


# include <bits/stdc++.h>

using namespace std;

const int MAXN = 50;

int a[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int A = 1, B = 2;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (i % 2 == 1 || j == n - 1)
                a[i][j] = A, A += 2;
            else
                a[i][j] = B, B += 2;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << a[i][j] << " ";
        cout << '\n';
    }

    return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="good_task" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/good_task" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/good_task" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="input.txt" />
		<Unit filename="main.cpp" />
		<Unit filename="output.txt" />
		<Extensions>
			<code_completion />
			<envvars />
			<debugger />
			<lib_finder disable_auto="1" />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


#include<bits/stdc++.h>
using namespace std;
int ara[50][50];
int main()
{
    int n,i,j,cnt,k;
    while(cin>>n){
    cnt=1;
    for(i=1;i<=n/2;i++){
    for(j=i+1;j<=n-i;j++){
    ara[i][j]=cnt;
    cnt+=2;
    }
    }

    for(i=n/2+1;i<=n;i++){
    for(j=i-n/2;j<=n-(i-n/2)+1;j++){
    ara[i][j]=cnt;
    cnt+=2;
    }
    }

    cnt=2;
    for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
    if(ara[i][j]==0){ara[i][j]=cnt;cnt+=2;}
    }
    }

    for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
    cout<<ara[i][j]<<" ";
    }
    cout<<endl;
    }
    memset(ara,0,sizeof ara);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,j,cnt;
    while(cin>>n){
    cnt=0;
    for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
    cout<<++cnt<<" ";
    }
    cout<<endl;
    }
    }
    return 0;
}


#include<bits/stdc++.h>
/*
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <vector>
#include <numeric>
#include <set>
*/
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned long long int ULL;
const int INF = 0x3f3f3f3f;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repu(i, a, b) for(int i = (a); i < (b); i++)
#define repf(i, a, b) for(int i = (a); i <= (b); i++)
#define repd(i, a, b) for(int i = (a); i >= (b); i--)
#define MEM(x,i) memset((x),(i),sizeof(x))
#define mid ((l+r)>>1)
const double PI = acos(-1.0);
const double E = exp(1);
const double EPS = 1e-5;
const int maxn = 200000+5;
const int mod =1000000000+7;
int arr[100][100]={0};
int main(void){
	int n;
	scanf("%d",&n);
		int tmp=(n>>1);
		int bg=1;
		for(int i=0;i<n;i++){
			arr[i][tmp]=bg;
			bg+=2;
		}
		for(int i=0;i<n;i++){
			if(!arr[tmp][i]){
			arr[tmp][i]=bg;
			bg+=2;
		}
		}
	int lf=2;
	rep(i,n)
		rep(j,n)
			if(!arr[i][j]){
				arr[i][j]=lf;
				lf+=2;
			}
	rep(i,n){
		rep(j,n){
			if(j==0) cout<<arr[i][j];
			else cout<<" "<<arr[i][j];
		}
		puts("");
	}
	return 0;
}


#include<bits/stdc++.h>
/*
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <vector>
#include <numeric>
#include <set>
*/
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned long long int ULL;
const int INF = 0x3f3f3f3f;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repu(i, a, b) for(int i = (a); i < (b); i++)
#define repf(i, a, b) for(int i = (a); i <= (b); i++)
#define repd(i, a, b) for(int i = (a); i >= (b); i--)
#define MEM(x,i) memset((x),(i),sizeof(x))
#define mid ((l+r)>>1)
const double PI = acos(-1.0);
const double E = exp(1);
const double EPS = 1e-5;
const int maxn = 200000+5;
const int mod =1000000000+7;
int arr[100][100]={0};
int main(void){
	int n;
	scanf("%d",&n);
	if((n-3)%4==0){
		int tmp=(n>>1);
		int bg=1;
		for(int j=1;j<n;j+=2)
		for(int i=0;i<n;i++){
			arr[i][j]=bg;
			bg+=2;
		}
		for(int i=0;i<n;i++){
			if(!arr[tmp][i]){
			arr[tmp][i]=bg;
			bg+=2;
		}
		}
	int lf=2;
	rep(i,n)
		rep(j,n)
			if(!arr[i][j]){
				arr[i][j]=lf;
				lf+=2;
			}
	rep(i,n){
		rep(j,n){
			if(j==0) cout<<arr[i][j];
			else cout<<" "<<arr[i][j];
		}
		puts("");
	}
}else{
int tmp=(n>>1);
		int bg=1;
		for(int j=0;j<n;j++)
		for(int i=abs(tmp-j);i<n-abs(tmp-j);i++){
			arr[j][i]=bg;
			bg+=2;
		}
		for(int i=0;i<n;i++){
			if(!arr[tmp][i]){
			arr[tmp][i]=bg;
			bg+=2;
		}
		}
	int lf=2;
	rep(i,n)
		rep(j,n)
			if(!arr[i][j]){
				arr[i][j]=lf;
				lf+=2;
			}
	rep(i,n){
		rep(j,n){
			if(j==0) cout<<arr[i][j];
			else cout<<" "<<arr[i][j];
		}
		puts("");
	}
}
	return 0;
}


#include<bits/stdc++.h>
/*
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <vector>
#include <numeric>
#include <set>
*/
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned long long int ULL;
const int INF = 0x3f3f3f3f;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repu(i, a, b) for(int i = (a); i < (b); i++)
#define repf(i, a, b) for(int i = (a); i <= (b); i++)
#define repd(i, a, b) for(int i = (a); i >= (b); i--)
#define MEM(x,i) memset((x),(i),sizeof(x))
#define mid ((l+r)>>1)
const double PI = acos(-1.0);
const double E = exp(1);
const double EPS = 1e-5;
const int maxn = 200000+5;
const int mod =1000000000+7;
int arr[10][10]={0};
int main(void){
	int n;
	scanf("%d",&n);
		int tmp=(n>>1);
		int bg=1;
		for(int i=0;i<n;i++){
			arr[i][tmp]=bg;
			bg+=2;
		}
		for(int i=0;i<n;i++){
			if(!arr[tmp][i]){
			arr[tmp][i]=bg;
			bg+=2;
		}
		}
	int lf=2;
	rep(i,n)
		rep(j,n)
			if(!arr[i][j]){
				arr[i][j]=lf;
				lf+=2;
			}
	rep(i,n){
		rep(j,n){
			if(j==0) cout<<arr[i][j];
			else cout<<" "<<arr[i][j];
		}
		puts("");
	}
	return 0;
}


#include<bits/stdc++.h>
/*
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <iomanip>
#include <vector>
#include <numeric>
#include <set>
*/
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned long long int ULL;
const int INF = 0x3f3f3f3f;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repu(i, a, b) for(int i = (a); i < (b); i++)
#define repf(i, a, b) for(int i = (a); i <= (b); i++)
#define repd(i, a, b) for(int i = (a); i >= (b); i--)
#define MEM(x,i) memset((x),(i),sizeof(x))
#define mid ((l+r)>>1)
const double PI = acos(-1.0);
const double E = exp(1);
const double EPS = 1e-5;
const int maxn = 200000+5;
const int mod =1000000000+7;
int arr[100][100]={0};
int main(void){
	int n;
	scanf("%d",&n);
		int tmp=(n>>1);
		int bg=1;
		for(int j=1;j<n;j+=2)
		for(int i=0;i<n;i++){
			arr[i][j]=bg;
			bg+=2;
		}
		for(int i=0;i<n;i++){
			if(!arr[tmp][i]){
			arr[tmp][i]=bg;
			bg+=2;
		}
		}
	int lf=2;
	rep(i,n)
		rep(j,n)
			if(!arr[i][j]){
				arr[i][j]=lf;
				lf+=2;
			}
	rep(i,n){
		rep(j,n){
			if(j==0) cout<<arr[i][j];
			else cout<<" "<<arr[i][j];
		}
		puts("");
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,a[50][50],x,y;
int main()
{
    scanf("%d",&n);
    y=-1;
    int c=(n+1)/2;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            if(j<=abs(c-i)) x+=2,a[i][j]=x;
            else if(j>n-abs(c-i)) x+=2,a[i][j]=x;
            else y+=2,a[i][j]=y;
        }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            printf("%d ",a[i][j]);
        puts("");
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,a[50][50],x,y;
int main()
{
    scanf("%d",&n);
    y=1;
    int c=(n+1)/2;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            if(j<=abs(c-i)) x+=2,a[i][j]=x;
            else if(j>n-abs(c-i)) x+=2,a[i][j]=x;
            else y+=2,a[i][j]=y;
        }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            printf("%d ",a[i][j]);
        puts("");
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,a[50][50],tot,top;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            a[i][j]=++tot;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            printf("%d ",a[i][j]);
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int A[50][50];
int a[2];
int main()
{
    int n;
    while(scanf("%d",&n)==1){
        a[0] = 2;a[1] = 1;
        memset(A,0,sizeof(A));
        int m = (n+1)/2;
        for(int i=1;i<=n;i++){
            int x = min(i-1,n-i);
            for(int j=-x;j<=x;j++){
                A[i][j+m] = 1;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                int t = A[i][j];
                printf("%d%c",a[t],(j==n?'\n':' '));
                a[t]+=2;
            }
        }

    }return 0;
}


#include<iostream>

using namespace std;

int T[50][50];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	int x = 1;
	int i = 0, j = n/2;
	while(x <= n*n)
	{
        T[i][j] = x++;
        int i1 = (i >= 1 ? i - 1 : n - 1);
        int j1 = (j < n - 1 ? j + 1 : 0);
        if (T[i1][j1] != 0)
			i = (i < n - 1 ? i + 1 : 0);
		else
			i = i1, j = j1;
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			cout << T[i][j] << " ";
		cout << "\n";
	}

	return 0;
}


#include<iostream>

using namespace std;

int T[50][50];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	int k = n/2;
	int odd = 1;
	int even = 2;
	for (int i = 0; i <= n/2; i++)
	{
        for (int j = k - i; j <= k + i; j++)
		{
			T[i][j] = odd;
			odd += 2;
			if (i != n/2)
			{
				T[n - 1 - i][j] = odd;
				odd += 2;
			}
		}
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (T[i][j] == 0)
			{
				cout << even << " ";
				even += 2;
			}
			else
				cout << T[i][j] << " ";
		}
		cout << "\n";
	}

	return 0;
}


#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
        {
    int l,r,d,co=1,fl=0,max=-1,min,i,su;
    scanf("%d %d %d",&l,&r,&d);
    int a[2];
    a[0]=l;a[1]=r;
    for(i=0;i<2;i++)
    {
        if(a[i]>max)
        max=a[i];
    }
    min=max+1;
    for(i=0;i<2;i++)
    {
        if(a[i]<min)
        min=a[i];
    }
    if(max<d)
    printf("%d\n",d);
    else if(min>d)
    printf("%d\n",d);
     else
         printf("%d\n",((r/d)+1)*d);
        }
    return 0;
}


#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
        {
    int l,r,d,x,sum=0,co=0,fl=0,max=-1,min,i;
    scanf("%d %d %d",&l,&r,&d);
    int a[2];
    a[0]=l;a[1]=r;
    for(i=0;i<2;i++)
    {
        if(a[i]>max)
        max=a[i];
    }
    min=max+1;
    for(i=0;i<2;i++)
    {
        if(a[i]<min)
        min=a[i];
    }
    if(d==1)
    {
        if(min>d)
        printf("%d\n",d);
        else
        printf("%d\n",max+1);
    }
    else {
    for(i=1;i<=10000000;i++)
    {
        sum=d*i;
        if(sum>=min && sum<=max)
        continue;
        else {
            printf("%d\n",sum);
            break;
        }
    }
    }
        }
    return 0;
}


#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
        {
    int l,r,d,co=1,fl=0,max=-1,min,i,su;
    scanf("%d %d %d",&l,&r,&d);
    int a[2];
    a[0]=l;a[1]=r;
    for(i=0;i<2;i++)
    {
        if(a[i]>max)
        max=a[i];
    }
    min=max+1;
    for(i=0;i<2;i++)
    {
        if(a[i]<min)
        min=a[i];
    }
    if(max<d)
    printf("%d\n",d);
    else if(min>d)
    printf("%d\n",d);
else if(d>=min && d<=max && d==1)
{
    printf("%d\n",max+1);
}
    else if(d>=min && d<=max)
    {
       while(d<=max)
       {
           su=co*d;
           if(su>max)
           {
           break;
           }
           else
           co++;
    }
       if(fl==0)
       printf("%d\n",su);
        }
        }
    return 0;
}


#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
        {
    int l,r,d,x,sum=0,co=0,fl=0,max=-1,min,i;
    scanf("%d %d %d",&l,&r,&d);
    int a[2];
    a[0]=l;a[1]=r;
    for(i=0;i<2;i++)
    {
        if(a[i]>max)
        max=a[i];
    }
    min=max+1;
    for(i=0;i<2;i++)
    {
        if(a[i]<min)
        min=a[i];
    }
    if(d==1)
    {
        if(min>d)
        printf("%d",d);
        else
        printf("%d",max+1);
    }
    else {
    for(i=1;i<=10000000;i++)
    {
        sum=d*i;
        if(sum>=min && sum<=max)
        continue;
        else {
            printf("%d\n",sum);
            break;
        }
    }
    }
        }
    return 0;
}


#include <stdio.h>
int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int l,r,d;
        scanf("%d%d%d",&l,&r,&d);
        printf("%d\n",d<l?d:(r/d+1)*d);
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int n;
    int l,r,d;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d%d",&l,&r,&d);
        int xx=1;
        for(int i=1;i<l;i++)
            if(i%d==0){
                printf("%d\n",i);
                xx=0;
                break;
            }
        if(xx)
            for(int i=r+1;i<1000000000;i++)
                if(i%d==0){
                printf("%d\n",i);
                break;
            }
    }
}


#include<stdio.h>
int main()
{
    int n;
    int l,r,d;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d%d",&l,&r,&d);
        int xx=1;
        for(int i=1;i<l;i++)
            if(i%d==0){
                printf("%d\n",i);
                xx=0;
                break;
            }
        if(xx)
            for(int i=r+1;i<=1000000000;i++)
                if(i%d==0){
                printf("%d\n",i);
                break;
            }
    }
}


#include<stdio.h>
int main()
{
    int q,l,r,d,i,p,j=1;
    scanf("%d",&q);
    for(i=0;i<q;i++)
    {
        scanf("%d%d%d",&l,&r,&d);
        while(1)
        {
            p=d*j;
            if(p<l || p>r)
            {
                j=1;
                break;
            }
            j++;
        }
        printf("%d",p);
    }
}


#include<stdio.h>
int main()
{
    int q,l,r,d,i,p,j=1;
    scanf("%d",&q);
    for(i=0;i<q;i++)
    {
        scanf("%d%d%d",&l,&r,&d);
        while(1)
        {
            p=d*j;
            if(p<l || p>r)
            {
                j=1;
                break;
            }
            j++;
        }
        printf("%d\n",p);
    }
}


#include<stdio.h>
int main()
{
    int q,i;
    long long int l,r,d,p;
    scanf("%d",&q);

    for(i=0;i<q;i++)
    {
        scanf("%I64d %I64d %I64d",&l,&r,&d);

        if(d<l || d>r) printf("%I64d\n",d);
        else
        {
            p=d*((r/d)+1);
            printf("%I64d\n",p);
        }
    }
}


#include<stdio.h>
int main()
{
    long long int q,l,r,d,i,s,j;
    scanf("%I64d",&q);
    for(i=0;i<q;i++)
    {
    scanf("%I64d%I64d%I64d",&l,&r,&d);
    if(d<l || d>r)
        printf("%I64d\n",d);
    else
        {
                  j=r/d+1;
            s=d*j;
                 printf("%I64d\n",s);

    }
    }
}


#include<stdio.h>
int main()
{
    long long int q,l,r,d,i,s,j;
    scanf("%I64d",&q);
    for(i=0;i<q;i++)
    {
    scanf("%I64d%I64d%I64d",&l,&r,&d);
    if(d<l || d>r)
        printf("%I64d",d);
    else
        {
                   for(j=r+1;;j++)
        {
            if(j%d==0)
            {
                 printf("%I64d",j);
    break;
            }
        }
    }
    }
}


#include<stdio.h>
int main()
{
    long long int q,l,r,d,i,s,j;
    scanf("%I64d",&q);
    for(i=0;i<q;i++)
    {
    scanf("%I64d%I64d%I64d",&l,&r,&d);
    if(d<l || d>r)
        printf("%I64d\n",d);
    else
        {
                   for(j=r+1;;j++)
        {
            if(j%d==0)
            {
                 printf("%I64d\n",j);
    break;
            }
        }
    }
    }
}


#include<stdio.h>
int main()
{
    long long q,l,r,i,d;
    scanf("%lld",&q);
    for(i=1;i<=q;i=i+1)
    {
        scanf("%lld %lld %lld",&l,&r,&d);
        if(d<l||d>r)
            {printf("%lld\n",d);}
        else
        {printf("%lld\n",((b/d)+1)*d);}
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int q,l,r,i,d;
    scanf("%d",&q);
    for(i=1;i<=q;i=i+1)
    {
        scanf("%d %d %d",&l,&r,&d);
        if(d<l||d>r)
            {printf("%d\n",d);}
        else
        {printf("%d\n",d*(r+1));}
    }
    return 0;
}


#include<stdio.h>
int main()
{
    long long q,l,r,i,d;
    scanf("%lld",&q);
    for(i=1;i<=q;i=i+1)
    {
        scanf("%lld %lld %lld",&l,&r,&d);
        if(d<l||d>r)
            {printf("%lld\n",d);}
        else
        {printf("%lld\n",((r/d)+1)*d);}
    }
    return 0;
}


#include<stdio.h>
int main()
{
    long long q,l,r,i,d;
    scanf("%lld",&q);
    for(i=1;i<=q;i=i+1)
    {
        scanf("%lld %lld %lld",&l,&r,&d);
        if(d<l||d>r)
            {printf("%lld\n",d);}
        else
        {printf("%lld\n",d*(r+1));}
    }
    return 0;
}


#include <stdio.h>

int main()
{
    int i, t, m;
    long long int L, R, D, X[500];

    scanf("%d", &t);
    for(i=0;i<t;i++)
    {
        scanf("%lld%lld%lld", &L, &R, &D);

        m = 1;

        do{
            if((D * m < L)  ||  (D * m > R))
            {
                X[i] = D * m;
                break;
            }
            else
                m++;
        }while(1);

    }

    for(i=0;i<t;i++)
        printf("\n%lld", X[i]);

    return 0;
}


#include <stdio.h>

int main()
{
    int i, t, m;
    long long int L, R, D, X[500];

    scanf("%d", &t);
    for(i=0;i<t;i++)
    {
        scanf("%lld%lld%lld", &L, &R, &D);

        m = 1;

        do{
            if((D * m < L)  ||  (D * m > R))
            {
                X[i] = D * m;
                break;
            }
            else
                m++;
        }while(1);

    }

    for(i=0;i<t;i++)
        printf("%lld", X[i]);

    return 0;
}


#include <stdio.h>

int main()

{

  int q;

  scanf("%d", &q);

  while (q--)

  {

    int l, r, i, k;

    scanf("%d %d %d", &l, &r, &i);

    if (i < l) printf("%d\n", i);

    else if (i > r) printf("%d\n", i);

    else

    {

        k = i*(r/i);

        while (k <= r) k += i;

        printf("%d\n", k);

    }

  }

  return 0;

}


#include <stdio.h>

int main()

{

  int q;

  scanf("%d", &q);

  while (q--)

  {

    int l, r, i;

    scanf("%d %d %d", &l, &r, &i);

    if (i < l) printf("%d\n", i);

    else if (i > r) printf("%d\n", i);

    else printf("%d\n", i*((r/i)+1));

  }

  return 0;
}


#include<stdio.h>

int main(){
    int q;
    scanf("%d", &q);
    int l, r, d, x;
    while(q--){
        scanf("%d%d%d", &l, &r, &d);
        if(l <= d)
            x = (r / d + 1) * d;
        else
            x = d;
        printf("%d\n", x);
    }
}


#include<stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    for(int i =0; i< n; i++) {
        int l, r, d;
        scanf("%d %d %d", &l, &r, &d);

        r /= d;

        if(l / d > 0 && l != d)
            printf("%d\n", d);
        else
            printf("%d\n", (r+1) *d);
    }

    return 0;
}


#include<stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    for(int i =0; i< n; i++) {
        int l, r, d;
        scanf("%d", &l, &r, &d);

        l /= d;
        r /= d;

        if(l > 0)
            printf("%d\n", d);
        else
            printf("%d\n", (r+1) *d);
    }

    return 0;
}


#include<stdio.h>

int main() {
    int l, r, d;
    scanf("%d", &l, &r, &d);

    l /= d;
    r /= d;

    if(l > 0)
        printf("%d", d);
    else
        printf("%d", (r+1) *d);

    return 0;
}


#include<stdio.h>

int main() {
    int n;
    for(int i =0; i< n; i++) {
        int l, r, d;
        scanf("%d", &l, &r, &d);

        l /= d;
        r /= d;

        if(l > 0)
            printf("%d\n", d);
        else
            printf("%d\n", (r+1) *d);
    }

    return 0;
}


const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });

  let tmp=0, q=0, lrds=[];

  rl.on('line', (input) => {
      if (tmp==0) {q=Number(input); tmp++;}
      else {
          if (tmp<=q){
          let lrd=input.split(' ').map(a=>{return Number(a)});
           lrds.push(lrd);
            tmp++;}
          }
    if (tmp>q){ rl.close(); therest();}
    return 0;}
);

let therest=function() {

    let res=[];
    for (let i=0; i<q; i++) {
        let l=lrds[i][0];
        let r=lrds[i][1];
        let d=lrds[i][2];

        if (d<l || d>r) {res.push(d);
            console.log(d);}
        else {
            if (r%d==0) console.log(r+d);
            else console.log(Math.ceil(r/d)*d)
        }
//console.log(lrds[i]);

    }
//console.log(T, pairs)
}
//console.log(lngth, str);}

  /*
  rl.question('What do you think of Node.js? ', (answer) => {
    // TODO: Log the answer in a database
    console.log(`Thank you for your valuable feedback: ${answer}`);

    rl.close();
  });*/


const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let l = 0;
let n = 0

rl.on('line', (input) => {
    if (l === 0) {
        n = parseInt(input)
    } else {
        if (l === n)
            rl.close();

        arr = input.split(' ').map(function (item) {
            return parseInt(item, 10);
        });

        if(arr[0]>arr[2]){
          console.log(arr[2])
        } else if(arr[1]<arr[2]){
            console.log(arr[2])
        } else {
            console.log(Math.ceil((arr[1]+1)/arr[2])*arr[2])
        }

        if (l === n)
            return

    }
    l++
});


const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let l = 0;
let n = 0

rl.on('line', (input) => {
    if (l === 0) {
        n = parseInt(input)
    } else {
        if (l === n)rl.close();
        arr = input.split(' ').map(function (item) {
            return parseInt(item, 10);
        });

        if(arr[0]>arr[2]){
          console.log(arr[2])
        } else if(arr[1]<arr[2]){
            console.log(arr[2])
        } else {
            console.log(Math.ceil((arr[1]+1)/arr[2])*arr[2])
        }

        //if (l === n)return

    }
    l++
});


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;

namespace Achill
{
    class Program
    {
        static void Main(string[] args)
        {
            int q = int.Parse(Console.ReadLine());
            while (q-- > 0)
            {
                int[] lrd = Console.ReadLine().Split().Select(item => int.Parse(item)).ToArray();
                if(lrd[2] <= lrd[1] && lrd[2] >= lrd[0])
                {
                    Console.WriteLine(lrd[2] - lrd[1] % lrd[2] + lrd[1]);
                }
                else
                {
                    Console.WriteLine(lrd[2]);
                }
            }
            //Console.ReadKey();
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

public sealed class Solution
{
    static void Method(int a, int b, int d)
    {
        Parallel.Invoke((() =>
        {
            for (int i = 1; i < a; i++)
            {
                if (i % d == 0)
                {
                    Console.WriteLine(i);
                    return;
                }
            }

            for (int i = b + 1; i < 10e9; i++)
            {
                if (i % d == 0)
                {
                    Console.WriteLine(i);
                    return;
                }
            }
        }));

    }
    public static void Main(String[] args)
    {
        var n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            var line = Console.ReadLine().Split(' ');
            var a = int.Parse(line[0]);
            var b = int.Parse(line[1]);
            var d = int.Parse(line[2]);
            Method(a,b,d);
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

public sealed class Solution
{
    static int? Method(int a, int b, int d)
    {
        for (int i = 1; i < a; i++)
        {
            if (i % d == 0)
            {
                return i;
            }
        }

        return null;
    }
    static int? Method2(int a, int b, int d)
    {
        for (int i = b+1; i <=10e9; i++)
        {
            if (i % d == 0)
            {
                return i;
            }
        }

        return null;
    }
    public static void Main(String[] args)
    {
        var n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            var line = Console.ReadLine().Split(' ');
            var a = int.Parse(line[0]);
            var b = int.Parse(line[1]);
            var d = int.Parse(line[2]);
            if (d>=a)
            {
                Console.WriteLine(Method2(a, b, d));
            }
            else if(d<a)
            {
                Console.WriteLine(Method(a,b,d));
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

public sealed class Solution
{
    static int? Method(int a, int b, int d)
    {
        for (int i = 1; i < a; i++)
        {
            if (i % d == 0)
            {
                return i;
            }
        }

        return null;
    }
    static int? Method2(int a, int b, int d)
    {
        for (int i = b+1; i <=10e9; i++)
        {
            if (i % d == 0)
            {
                return i;
            }
        }

        return null;
    }
    public static void Main(String[] args)
    {
        var n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            var line = Console.ReadLine().Split(' ');
            var a = int.Parse(line[0]);
            var b = int.Parse(line[1]);
            var d = int.Parse(line[2]);
            var m1 = Method(a, b, d);
            Console.WriteLine(m1 ?? Method2(a,b,d));

        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

public sealed class Solution
{
    static void Method(int a, int b, int d)
    {
        if (d<a)
        {
            Console.WriteLine(d);
            return;
        }

        if (d>=a)
        {
            var ost = d-b % d;
            Console.WriteLine(b+ost);
            return;
        }

    }

    public static void Main(String[] args)
    {
        var n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            var line = Console.ReadLine().Split(' ');
            var a = int.Parse(line[0]);
            var b = int.Parse(line[1]);
            var d = int.Parse(line[2]);
            Method(a,b,d);
        }
    }
}


using System;

namespace CSharp
{
    class _1101A
    {
        public static void Main()
        {
            for (int q = int.Parse(Console.ReadLine()); q > 0; q--)
            {
                var tokens = Console.ReadLine().Split();

                int l = int.Parse(tokens[0]);
                int r = int.Parse(tokens[1]);
                int d = int.Parse(tokens[2]);

                Console.WriteLine(d < l ? d : d * (1 + r / d));
            }
        }
    }
}


using System;
public class Program
{
	public static void Main(string[] args)
	{
		var n = int.Parse(Console.ReadLine());
		for (var i = 0; i < n; i++)
		{
		    var l = Console.ReadLine().Split(' ');
		    var a = int.Parse(l[0]);
		    var b = int.Parse(l[1]);
		    var c = int.Parse(l[2]);
		    if (c < a || c > b)
		    {
		        Console.WriteLine(c);
		    }
		    else
		    {
		        Console.WriteLine((b / c + 1) * c);
		    }
		}
	}
}


using System;
public class Program
{
	public static Main(string[] args)
	{
		Console.WriteLine(args.Length);
	}
}


using System;
public class Program
{
	public static void Main(string[] args)
	{
		Console.WriteLine(args.Length);
	}
}


using System;
using System.IO;
using CompLib.Util;

public class Program
{
    public void Solve()
    {
        var sc = new Scanner();
        int t = sc.NextInt();
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false});
        for (int i = 0; i < t; i++)
        {
            Q(sc);
        }

        Console.Out.Flush();
    }

    void Q(Scanner sc)
    {
        long l = sc.NextInt();
        long r = sc.NextInt();
        long d = sc.NextInt();

        if (d < l)
        {
            Console.WriteLine(d);
            return;
        }

        long e = r / d + 1;
        Console.WriteLine(e * d);
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s = Console.ReadLine();
                while (s.Length == 0)
                {
                    s = Console.ReadLine();
                }

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}


using System;
using System.IO;
using CompLib.Util;

public class Program
{
    public void Solve()
    {
        var sc = new Scanner();
        int t = sc.NextInt();
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false});
        for (int i = 0; i < t; i++)
        {
            Q(sc);
        }

        Console.Out.Flush();
    }

    void Q(Scanner sc)
    {
        long l = sc.NextInt();
        long r = sc.NextInt();
        long d = sc.NextInt();

        if (l < d)
        {
            Console.WriteLine(d);
            return;
        }

        long e = r / d + 1;
        Console.WriteLine(e * d);
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s = Console.ReadLine();
                while (s.Length == 0)
                {
                    s = Console.ReadLine();
                }

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}


using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
	class Program
	{
		static void Main(string[] args)
		{
			int q = int.Parse(Console.ReadLine());

			for (int i = 0; i < q; i++)
			{
				int[] line = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
				int l = line[0], r = line[1], d = line[2];
				Console.WriteLine(Solve(l, r, d));
			}
		}

		static int Solve(int l, int r, int d)
		{
			int before = d;
			if (l > before)
			{
				return before;
			}

			int after = r + d - (r % d);
			return after;
		}
	}
}


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace ReadWriteTemplate
{
    public static class Solver
    {
        private static void SolveCase()
        {
            int n = ReadInt();
            for (int cn = 0; cn < n; cn++)
            {
                long l = ReadLong();
                long r = ReadLong();
                long d = ReadLong();
                if (d < l)
                {
                    Writer.WriteLine(d);
                }
                else
                {
                    Writer.WriteLine((r / d + 1) * d);
                }
            }
        }

        public static void Solve()
        {
#if DEBUG
            var sw = Stopwatch.StartNew();
#endif

            SolveCase();

            /*int T = ReadInt();
            for (int i = 0; i < T; i++)
            {
                Writer.Write("Case #{0}: ", i + 1);
                SolveCase();
            }*/

#if DEBUG
            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds} ms");
#endif
        }

        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if DEBUG
            //Reader = Console.In; Writer = Console.Out;
            Reader = File.OpenText("input.txt"); Writer = File.CreateText("output.txt");
#else
            Reader = Console.In; Writer = Console.Out;
#endif

            // Solve();
            Thread thread = new Thread(Solve, 64 * 1024 * 1024);
            thread.CurrentCulture = CultureInfo.InvariantCulture;
            thread.Start();
            thread.Join();

            Reader.Close();
            Writer.Close();
        }

        public static IOrderedEnumerable<TSource> OrderByWithShuffle<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            return source.Shuffle().OrderBy(keySelector);
        }

        public static T[] Shuffle<T>(this IEnumerable<T> source)
        {
            T[] result = source.ToArray();
            Random rnd = new Random();
            for (int i = result.Length - 1; i >= 1; i--)
            {
                int k = rnd.Next(i + 1);
                T tmp = result[k];
                result[k] = result[i];
                result[i] = tmp;
            }
            return result;
        }

        #region Read/Write

        private static TextReader Reader;

        private static TextWriter Writer;

        private static Queue<string> CurrentLineTokens = new Queue<string>();

        private static string[] ReadAndSplitLine()
        {
            return Reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string ReadToken()
        {
            while (CurrentLineTokens.Count == 0)
                CurrentLineTokens = new Queue<string>(ReadAndSplitLine());
            return CurrentLineTokens.Dequeue();
        }

        public static string ReadLine()
        {
            return Reader.ReadLine();
        }

        public static int ReadInt()
        {
            return int.Parse(ReadToken());
        }

        public static long ReadLong()
        {
            return long.Parse(ReadToken());
        }

        public static double ReadDouble()
        {
            return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
        }

        public static int[] ReadIntArray()
        {
            return ReadAndSplitLine().Select(int.Parse).ToArray();
        }

        public static long[] ReadLongArray()
        {
            return ReadAndSplitLine().Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
        }

        public static int[][] ReadIntMatrix(int numberOfRows)
        {
            int[][] matrix = new int[numberOfRows][];
            for (int i = 0; i < numberOfRows; i++)
                matrix[i] = ReadIntArray();
            return matrix;
        }

        public static string[] ReadLines(int quantity)
        {
            string[] lines = new string[quantity];
            for (int i = 0; i < quantity; i++)
                lines[i] = Reader.ReadLine().Trim();
            return lines;
        }

        public static void WriteArray<T>(IEnumerable<T> array)
        {
            Writer.WriteLine(string.Join(" ", array));
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace sample
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < num; i++)
            {
                int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), Convert.ToInt32);
                int l = input[0];
                int r = input[1];
                int d = input[2];
                if(l > d)
                {
                    System.Console.WriteLine(d);
                }
                else
                {
                    int mul = (int)Math.Floor((double)r/(double)d)+1;
                    System.Console.WriteLine(mul*d);
                }
            }
        }

    }

}


using System;
using System.Globalization;
using System.IO;
using System.Text;
using static System.Console;

namespace Olymp
{
  public class ProblemSolver
  {
    private readonly Tokenizer input;

    public void Solve()
    {
      var q = input.ReadInt();
      for (var i = 0; i < q; i++)
      {
        var l = input.ReadInt();
        var r = input.ReadInt();
        var d = input.ReadInt();
        if (d < l || d > r)
          WriteLine(d);
        else
          WriteLine(d * (r / d + 1));
      }
    }

    public ProblemSolver(TextReader input)
    {
      this.input = new Tokenizer(input);
    }
  }

  #region Service classes

  public class Tokenizer
  {
    private TextReader reader;

    public string ReadToEnd()
    {
      return this.reader.ReadToEnd();
    }

    public int ReadInt()
    {
      var c = SkipWS();
      if (c == -1)
        throw new EndOfStreamException();
      var isNegative = false;
      if (c == '-' || c == '+')
      {
        isNegative = c == '-';
        c = this.reader.Read();
        if (c == -1)
          throw new InvalidOperationException();
      }
      if (!char.IsDigit((char)c))
        throw new InvalidOperationException();
      var result = (char)c - '0';
      c = this.reader.Read();
      while (c > 0 && !char.IsWhiteSpace((char)c))
      {
        if (!char.IsDigit((char)c))
          throw new InvalidOperationException();
        result = result * 10 + (char)c - '0';
        c = this.reader.Read();
      }
      if (isNegative)
        result = -result;
      return result;
    }

    public string ReadLine()
    {
      return reader.ReadLine();
    }

    public long ReadLong()
    {
      return long.Parse(this.ReadToken());
    }

    public double ReadDouble()
    {
      return double.Parse(this.ReadToken(), CultureInfo.InvariantCulture);
    }

    public int[] ReadIntArray(int n)
    {
      var a = new int[n];
      for (var i = 0; i < n; i++)
        a[i] = ReadInt();
      return a;
    }

    public long[] ReadLongArray(int n)
    {
      var a = new long[n];
      for (var i = 0; i < n; i++)
        a[i] = ReadLong();
      return a;
    }

    public double[] ReadDoubleArray(int n)
    {
      var a = new double[n];
      for (var i = 0; i < n; i++)
        a[i] = ReadDouble();
      return a;
    }

    public string ReadToken()
    {
      var c = SkipWS();
      if (c == -1)
        return null;
      var sb = new StringBuilder();
      while (c > 0 && !char.IsWhiteSpace((char)c))
      {
        sb.Append((char)c);
        c = this.reader.Read();
      }
      return sb.ToString();
    }

    private int SkipWS()
    {
      var c = this.reader.Read();
      if (c == -1)
        return c;
      while (c > 0 && char.IsWhiteSpace((char)c))
        c = this.reader.Read();
      return c;
    }

    public Tokenizer(TextReader reader)
    {
      this.reader = reader;
    }
  }

  internal class Program
  {
    public static void Main(string[] args)
    {
      var solver = new ProblemSolver(Console.In);
      solver.Solve();
    }
  }

  #endregion
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class A1101_MinimumInteger
    {
        public static void Main()
        {
            var n = int.Parse(Console.ReadLine());
            while (n > 0)
            {
                n--;
                string[] a = Console.ReadLine().Split(' ');
                int l = int.Parse(a[0]);
                int d = int.Parse(a[1]);
                int r = int.Parse(a[2]);

                if(r<l || r > d)
                {
                    Console.WriteLine(r);
                }
                else
                {
                    int res = (d / r) + 1;
                    Console.WriteLine(res *r);
                }

            }
        }
    }
}


using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		int T = int.Parse(Console.ReadLine());
		for(var i=0;i<T;i++){
			string[] str = Console.ReadLine().Split();
			long L = long.Parse(str[0]);
			long R = long.Parse(str[1]);
			long D = long.Parse(str[2]);
			long ans = 0;
			if(D<L){
				ans = D;
			} else {
				ans = D * (R/D+1);
			}
			Console.WriteLine(ans);
		}
	}
}


using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int q = Convert.ToInt32(Console.ReadLine());
            for(int i = 0; i < q; i++) {
                int l = 0, r = 0, d = 0;
                var inp = Console.ReadLine().Split();
                l = Convert.ToInt32(inp[0]);
                r = Convert.ToInt32(inp[1]);
                d = Convert.ToInt32(inp[2]);
                if(d < l) {
                    Console.WriteLine(d);
                } else {
                    int c = r / d;
                    Console.WriteLine((c + 1) * d);
                }
            }
        }
    }
}


using System;

namespace justsomemycode
{
    class Program
    {
        static void Main(string[] args)
        {
            int q = Convert.ToInt32(Console.ReadLine());
            for(int i = 0; i < q; i++)
            {
                var input = Console.ReadLine().Split();
                int l = Convert.ToInt32(input[0]);
                int r = Convert.ToInt32(input[1]);
                int d = Convert.ToInt32(input[2]);
                for(int j = 1; j >= 1; j++)
                {
                    if(l <= j && j <= r)
                    {
                        j = r + 1;
                    }

                    if(j % d == 0)
                    {
                        Console.WriteLine(j);
                        break;
                    }
                }
            }
        }
    }
}


using System;

namespace justsomemycode
{
    class Program
    {
        static void Main(string[] args)
        {
            int q = Convert.ToInt32(Console.ReadLine());
            for(int i = 0; i < q; i++)
            {
                var input = Console.ReadLine().Split();
                int l = Convert.ToInt32(input[0]);
                int r = Convert.ToInt32(input[1]);
                int d = Convert.ToInt32(input[2]);
                if(d < l)
                {
                    Console.WriteLine(d);
                }
                else
                {
                    var m = d;
                    while (m <= r)
                        m += d;
                    Console.WriteLine(m);
                }
            }
        }
    }
}


using System;

namespace justsomemycode
{
    class Program
    {
        static void Main(string[] args)
        {
            int q = Convert.ToInt32(Console.ReadLine());
            for(int i = 0; i < q; i++)
            {
                var input = Console.ReadLine().Split();
                int l = Convert.ToInt32(input[0]);
                int r = Convert.ToInt32(input[1]);
                int d = Convert.ToInt32(input[2]);
                if(d < l)
                {
                    Console.WriteLine(d);
                }
                else
                {
                    int m = 0;
                    for (int j = r + 1; j > 0; j++)
                        if (j % d == 0)
                        {
                            m = j;
                            break;
                        }
                    Console.WriteLine(m);
                }
            }
        }
    }
}


using System;
using System.Numerics;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Text.RegularExpressions;

namespace ProjectEuler
{
    class Program
    {

        private static void Main()
        {
            int q = int.Parse(Console.ReadLine());
            int[][] nums = new int[q][];

            for (int i = 0; i < q; i++)
            {
                nums[i] = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            }

            for (int j = 0; j < q; j++)
            {
                int l = nums[j][0];
                int r = nums[j][1];
                int d = nums[j][2];

                if (d < l || d > r)
                {
                    Console.WriteLine(d);
                }
                else
                {
                    int tmp = r / d;
                    Console.WriteLine((tmp + 1) * d);
                }
            }
        }
    }
}


using System;
using System.Linq;

namespace ConsoleApp
{
    internal static class Program
    {
        static void Main()
        {
            var n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                var input = Console.ReadLine()
                    .Split()
                    .Select(int.Parse)
                    .ToList();

                var l = input[0];
                var r = input[1];
                var d = input[2];

                var res = d;
                while (res >= l && res <= r)
                {
                    res += d;
                }

                Console.WriteLine(res);
            }
        }
    }
}


using System;
using System.Linq;

namespace ConsoleApp
{
    internal static class Program
    {
        static void Main()
        {
            var n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                var input = Console.ReadLine()
                    .Split()
                    .Select(int.Parse)
                    .ToList();

                var l = input[0];
                var r = input[1];
                var d = input[2];

                var res = 0;

                while (true)
                {
                    res += d;
                    if (res >= l && res <= r)
                        continue;

                    Console.WriteLine(res);
                    break;
                }
            }
        }
    }
}


using System;
using System.Linq;

namespace ConsoleApp
{
    internal static class Program
    {
        static void Main()
        {
            var n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                var input = Console.ReadLine()
                    .Split()
                    .Select(int.Parse)
                    .ToList();

                var l = input[0];
                var r = input[1];
                var d = input[2];

                Console.WriteLine(d < l ? d : d * (r/d + 1));
            }
        }
    }
}


using System;
using System.Linq;

namespace ConsoleApp
{
    internal static class Program
    {
        static void Main()
        {
            var n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                var input = Console.ReadLine()
                    .Split()
                    .Select(int.Parse)
                    .ToList();

                var l = input[0];
                var r = input[1];
                var d = input[2];

                var res = d;
                while (res >= l && res <= r)
                {
                    res += d;
                }
            }
        }
    }
}


using System;
using System.Linq;

namespace ConsoleApp
{
    internal static class Program
    {
        static void Main()
        {
            var n = int.Parse(Console.ReadLine());//

            for (int i = 0; i < n; i++)
            {
                var input = Console.ReadLine()
                    .Split()
                    .Select(int.Parse)
                    .ToList();

                var l = input[0];
                var r = input[1];
                var d = input[2];

                var res = d;
                while (res >= l && res <= r)
                {
                    res += d;
                }

                Console.WriteLine(res);
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             int n=int.Parse(Console.ReadLine());

             for(int j=0;j<n;j++)
             {
                  int g=0;
                  string[]y=Console.ReadLine().Split(' ');
                  int l=int.Parse(y[0]);
                  int r=int.Parse(y[1]);
                  int d=int.Parse(y[2]);
                  bool f=false;
                  int min=0;
                  int h=0;
                  int[]a=new int[9999999];
                  for(int i=1;i<9999999;i++)
                  a[i-1]=i*d;
                  while(true)
                  {
                       if((a[h]<l)||(a[h]>r))
                       {
                            Console.WriteLine(a[h]);
                            break;
                       }
                       h++;
                  }
             }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             int n=int.Parse(Console.ReadLine());

             for(int j=0;j<n;j++)
             {
                  int g=0;
                  string[]y=Console.ReadLine().Split(' ');
                  int l=int.Parse(y[0]);
                  int r=int.Parse(y[1]);
                  int d=int.Parse(y[2]);
                  bool f=false;
                  int min=0;
                  int h=0;
                        while(l>1)
                        {
                             l--;
                             if(l%d==0)
                             {
                                  min=l;
                                  f=true;
                                  //break;
                             }

                        }
                        if(f)
                        Console.WriteLine(min);
                        if(f==false)
                             {
                                  while(true)
                                  {
                                       r++;
                                       if(r%d==0)
                                       {
                                            Console.WriteLine(r);
                                            break;
                                       }
                                  }
                             }

             }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             int n=int.Parse(Console.ReadLine());

             for(int j=0;j<n;j++)
             {
                  int g=0;
                  string[]y=Console.ReadLine().Split(' ');
                  int l=int.Parse(y[0]);
                  int r=int.Parse(y[1]);
                  int d=int.Parse(y[2]);
                  int h=0;
                  while(true)
                  {
                       h++;
                       if((h%d==0)&&((h<l)||(h>r)))
                       {
                            Console.WriteLine(h);
                            break;
                       }

                  }
             }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             int n=int.Parse(Console.ReadLine());

             for(int j=0;j<n;j++)
             {
                  int g=0;
                  string[]y=Console.ReadLine().Split(' ');
                  int l=int.Parse(y[0]);
                  int r=int.Parse(y[1]);
                  int d=int.Parse(y[2]);
                  bool f=false;
                  int min=0;
                  int h=0;
                  int[]a=new int[999999];
                  for(int i=1;i<999999;i++)
                  a[i-1]=i*d;
                  while(true)
                  {
                       if((a[h]<l)||(a[h]>r))
                       {
                            Console.WriteLine(a[h]);
                            break;
                       }
                       h++;
                  }
             }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             int n=int.Parse(Console.ReadLine());

             for(int j=0;j<n;j++)
             {
                  int g=0;
                  string[]y=Console.ReadLine().Split(' ');
                  int l=int.Parse(y[0]);
                  int r=int.Parse(y[1]);
                  int d=int.Parse(y[2]);
                  bool f=false;
                  int h=0;
                  while(true)
                  {

                       h++;
                       if(h>=l)
                       {
                            h=r+1;
                            while(true)
                            {

                                 if(h%d==0)
                                 {
                                      Console.WriteLine(h);
                                      break;
                                 }
                                 h++;
                            }
                       break;
                       }
                       else if(h%d==0)
                       {
                            Console.WriteLine(h);
                            break;
                       }

                  }
             }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             int n=int.Parse(Console.ReadLine());

             for(int j=0;j<n;j++)
             {
                  int g=0;
                  string[]y=Console.ReadLine().Split(' ');
                  int l=int.Parse(y[0]);
                  int r=int.Parse(y[1]);
                  int d=int.Parse(y[2]);
                  if(l>d)
                  Console.WriteLine(d);
                  else
                  {
                       int e=r/d;
                       e++;
                       Console.WriteLine(d*e);
                  }
             }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MinChislo
{
    class Program
    {
        static void Main(string[] args)
        {
            int q = int.Parse(Console.ReadLine());
            for (var i = 1; i <= q; i++)
            {
                string[] s = Console.ReadLine().Split();
                long l = long.Parse(s[0]);
                long r = long.Parse(s[1]);
                long d = long.Parse(s[2]);

                if (l > d)
                {
                    Console.WriteLine(d);
                }
                else
                {
                    long x = r / d + 1;
                    Console.WriteLine(d * x);
                }
            }
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Minimum_Integer
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            int t = Next();
            for (int i = 0; i < t; i++)
            {
                writer.WriteLine(Solve(args));
            }
            writer.Flush();
        }

        private static int Solve(string[] args)
        {
            int l = Next();
            int r = Next();
            int d = Next();

            if (d < l)
                return d;

            return ((r + d)/d)*d;
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}