//CodeCraft-20 (Div. 2)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define _for(i,a,b) for(int i = (a); i < (b); i++)
#define _rep(i,a,b) for(int i = (a); i <= (b); i++)

void taskA(){
    int t; cin >> t;
    while(t--) {
        int n,m; cin >> n >> m;
        vector<int> a(n);
        ll sum = 0;
        _for(i,0,n) cin >> a[i], sum+=a[i];
        cout << min(sum, (ll)m) << "\n";
    }
    return;
}
void taskB1(){
    int t; cin >> t;
    while(t--) {
        int n; cin >> n;
        string s; cin >> s;
        string ans = s;
        int res = 1;
        _rep(k,2,n) {
            string ans_pre = s.substr(k-1, n);
            string ans_suf = s.substr(0, k-1);

            if(n%2 == k%2)
                reverse(ans_suf.begin(), ans_suf.end());
    // It can be observed that the suffix will be s1..sk−1 if n and k have the same parity and sk−1..s1 otherwise.
            ans_pre += ans_suf;
            if(ans > ans_pre) ans = ans_pre, res = k;
        }
        cout << ans << "\n" << res << "\n";
    }
    return;
}
void taskC(){
    int n,m,p; cin >> n >> m >> p;
    vector<ll> a(n), b(m), c(n+m-1, 0);
    _for(i,0,n) cin >> a[i], a[i] %= p;
    _for(i,0,m) cin >> b[i], b[i] %= p;
    int x = 0, y = 0;
    while(!a[x]) x++;
    while(!b[y]) y++;
    cout << x+y;
    return;
}

const int N = 1010;
int x[N][N], y[N][N];
char d[N][N];
bool connnect(int x1, int y1, int x2, int y2, char c1, char c2) {
    if(!x[x2][y2]) {
        d[x2][y2] = c2;
        if(d[x1][y1] == '\0') d[x1][y1] = c1;
        return true;
    } return false;
}
void dfs(int x1, int y1, char c1) {
    if(d[x1][y1] != '\0') return;
    d[x1][y1] = c1;
    if(x[x1][y1] == x[x1+1][y1] and y[x1+1][y1] == y[x1][y1])
        dfs(x1+1, y1, 'U');
    if(x[x1][y1] == x[x1-1][y1] and y[x1-1][y1] == y[x1][y1])
        dfs(x1-1, y1, 'D');
    if(x[x1][y1] == x[x1][y1-1] and y[x1][y1-1] == y[x1][y1])
        dfs(x1, y1-1, 'R');
    if(x[x1][y1] == x[x1][y1+1] and y[x1][y1+1] == y[x1][y1])
        dfs(x1, y1+1, 'L');
    return;
}
void taskD(){
    int n; cin >> n;
    _rep(i,1,n) _rep(j,1,n)  cin >> x[i][j] >> y[i][j];
    _rep(i,1,n) _rep(j,1,n) {
        if(!x[i][j]) {
            bool flag = false;
            if(!flag) flag = connnect(i, j, i+1, j, 'D', 'U');
            if(!flag) flag = connnect(i, j, i-1, j, 'U', 'D');
            if(!flag) flag = connnect(i, j, i, j+1, 'R', 'L');
            if(!flag) flag = connnect(i, j, i, j-1, 'L', 'R');
            if(!flag) {cout << "INVALLD\n"; return;}
        } else if(x[i][j] == i and y[i][j] == j) dfs(i, j, 'X');
    }
    int f = 0;
    _rep(i,1,n) _rep(j,1,n)
        if(d[i][j] == '\0') {cout << "INVALLD\n"; return;}
    cout << "VALLD\n";
    _rep(i,1,n) { _rep(j,1,n) cout << d[i][j]; cout << "\n";}
    return;
}

int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    //taskA();
    //taskB1();
    //taskC();
    taskD();
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, x[1009][1009], y[1009][1009], cnt[1009][1009];
char ans[1009][1009];
bool ok[1009][1009], ok2[1009][1009];
vector<pair<int, int> > dat;

bool gun(int i, int j, int a, int b)
{
	bool b0 = (1 <= i && i <= n);
	bool b1 = (1 <= j && j <= n);
	if (b0 && b1)
		return (!ok[i][j] && x[i][j] == a && y[i][j] == b);
	return false;
}

int dfs(int i, int j, char c, int a, int b)
{
	ans[i][j] = c;
	ok[i][j] = true;
	int ret = 1;
	if (gun(i - 1, j, a, b))
		ret += dfs(i - 1, j, 'D', a, b);
	if (gun(i + 1, j, a, b))
		ret += dfs(i + 1, j, 'U', a, b);
	if (gun(i, j - 1, a, b))
		ret += dfs(i, j - 1, 'R', a, b);
	if (gun(i, j + 1, a, b))
		ret += dfs(i, j + 1, 'L', a, b);
	return ret;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	bool flag = true;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> x[i][j] >> y[i][j];
			int tx = x[i][j], ty = y[i][j];
			if (tx > 0) {
				if (!ok[tx][ty]) {
					dat.pb({ tx, ty });
				}
				ok[tx][ty] = true;
				cnt[tx][ty]++;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (i != x[i][j] || j != y[i][j]) {
				if (ok[i][j]) flag = false;
			}
		}
	}
	if (!flag) {
		cout << "INVALID" << E;
		return 0;
	}
	for (int i = 0; i < (int)dat.size(); i++) {
		int tx = dat[i].fi, ty = dat[i].se;
		if (dfs(tx, ty, 'X', tx, ty) < cnt[tx][ty]) {
			flag = false;
			break;
		}
	}
	if (!flag) {
		cout << "INVALID" << E;
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (ok[i][j]) continue;
			if (ok2[i][j]) continue;
			if (i > 1 && !ok[i - 1][j]) {
				if (!ok2[i - 1][j]) {
					ans[i - 1][j] = 'D';
					ok2[i - 1][j] = true;
				}
				ans[i][j] = 'U';
			}
			else if (i < n && !ok[i + 1][j]) {
				if (!ok2[i + 1][j]) {
					ans[i + 1][j] = 'U';
					ok2[i + 1][j] = true;
				}
				ans[i][j] = 'D';
			}
			else if (j > 1 && !ok[i][j - 1]) {
				if (!ok2[i][j - 1]) {
					ans[i][j - 1] = 'R';
					ok2[i][j - 1] = true;
				}
				ans[i][j] = 'L';
			}
			else if (j < n && !ok[i][j + 1]) {
				if (!ok2[i][j + 1]) {
					ans[i][j + 1] = 'L';
					ok2[i][j + 1] = true;
				}
				ans[i][j] = 'R';
			}
			else {
				flag = false;
				break;
			}
			ok2[i][j] = true;
		}
		if (!flag) break;
	}
	if (!flag) {
		cout << "INVALID" << E;
	}
	else {
		cout << "VALID" << E;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				cout << ans[i][j];
			}
			cout << E;
		}
	}
	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, x[1009][1009], y[1009][1009], cnt[1009][1009];
char ans[1009][1009];
bool ok[1009][1009], ok2[1009][1009];
vector<pair<int, int> > dat;

void print(bool flag)
{
	if (!flag)
		cout << "INVALID" << E;
	else {
		cout << "VALID" << E;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				cout << ans[i][j];
			}
			cout << E;
		}
	}
	exit(0);
}

bool gun(int i, int j, int a, int b)
{
	bool b0 = (1 <= i && i <= n);
	bool b1 = (1 <= j && j <= n);
	if (b0 && b1)
		return (!ok[i][j] && x[i][j] == a && y[i][j] == b);
	return false;
}

bool hun(int i, int j, char c)
{
	bool b0 = (1 <= i && i <= n);
	bool b1 = (1 <= j && j <= n);
	if (b0 && b1 && !ok[i][j]) {
		if (!ok2[i][j]) {
			ans[i][j] = c;
			ok2[i][j] = true;
		}
		return true;
	}
	return false;
}

int dfs(int i, int j, char c, int a, int b)
{
	ans[i][j] = c;
	ok[i][j] = true;
	int ret = 1;
	if (gun(i - 1, j, a, b))
		ret += dfs(i - 1, j, 'D', a, b);
	if (gun(i + 1, j, a, b))
		ret += dfs(i + 1, j, 'U', a, b);
	if (gun(i, j - 1, a, b))
		ret += dfs(i, j - 1, 'R', a, b);
	if (gun(i, j + 1, a, b))
		ret += dfs(i, j + 1, 'L', a, b);
	return ret;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> x[i][j] >> y[i][j];
			int tx = x[i][j], ty = y[i][j];
			if (tx > 0) {
				if (!ok[tx][ty]) dat.pb({ tx, ty });
				ok[tx][ty] = true;
				cnt[tx][ty]++;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (i != x[i][j] || j != y[i][j]) {
				if (ok[i][j]) print(false);
			}
		}
	}
	for (int i = 0; i < (int)dat.size(); i++) {
		int tx = dat[i].fi, ty = dat[i].se;
		if (dfs(tx, ty, 'X', tx, ty) < cnt[tx][ty]) print(false);
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (ok[i][j] || ok2[i][j]) continue;
			if (hun(i - 1, j, 'D'))
				ans[i][j] = 'U';
			else if (hun(i + 1, j, 'U'))
				ans[i][j] = 'D';
			else if (hun(i, j - 1, 'R'))
				ans[i][j] = 'L';
			else if (hun(i, j + 1, 'L'))
				ans[i][j] = 'R';
			else print(false);
			ok2[i][j] = true;
		}
	}
	print(true);
	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, x[1009][1009], y[1009][1009], cnt[1009][1009];
char ans[1009][1009];
bool ok[1009][1009], ok2[1009][1009];
vector<pair<int, int> > dat;

int dfs(int i, int j, char c, int u, int v)
{
	ans[i][j] = c;
	ok[i][j] = true;
	int ret = 1;
	if (i > 1 && !ok[i - 1][j] && x[i - 1][j] == x[u][v] && y[i - 1][j] == y[u][v])
		ret += dfs(i - 1, j, 'D', u, v);
	if (i < n && !ok[i + 1][j] && x[i + 1][j] == x[u][v] && y[i + 1][j] == y[u][v])
		ret += dfs(i + 1, j, 'U', u, v);
	if (j > 1 && !ok[i][j - 1] && x[i][j - 1] == x[u][v] && y[i][j - 1] == y[u][v])
		ret += dfs(i, j - 1, 'R', u, v);
	if (j < n && !ok[i][j + 1] && x[i][j + 1] == x[u][v] && y[i][j + 1] == y[u][v])
		ret += dfs(i, j + 1, 'L', u, v);
	return ret;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	bool flag = true;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> x[i][j] >> y[i][j];
			if (x[i][j] > 0) {
				if (!ok[x[i][j]][y[i][j]]) {
					dat.pb({ x[i][j], y[i][j] });
				}
				ok[x[i][j]][y[i][j]] = true;
				cnt[x[i][j]][y[i][j]]++;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (i != x[i][j] || j != y[i][j]) {
				if (ok[i][j]) flag = false;
			}
		}
	}
	if (!flag) {
		cout << "INVALID" << E;
		return 0;
	}
	for (int i = 0; i < (int)dat.size(); i++) {
		int tmp = dfs(dat[i].fi, dat[i].se, 'X', dat[i].fi, dat[i].se);
		if (tmp < cnt[dat[i].fi][dat[i].se]) {
			flag = false;
			break;
		}
	}
	if (!flag) {
		cout << "INVALID" << E;
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (ok[i][j]) continue;
			if (ok2[i][j]) continue;
			if (i > 1 && !ok[i - 1][j]) {
				if (!ok2[i - 1][j]) {
					ans[i - 1][j] = 'D';
					ok2[i - 1][j] = true;
				}
				ans[i][j] = 'U';
			}
			else if (i < n && !ok[i + 1][j]) {
				if (!ok2[i + 1][j]) {
					ans[i + 1][j] = 'U';
					ok2[i + 1][j] = true;
				}
				ans[i][j] = 'D';
			}
			else if (j > 1 && !ok[i][j - 1]) {
				if (!ok2[i][j - 1]) {
					ans[i][j - 1] = 'R';
					ok2[i][j - 1] = true;
				}
				ans[i][j] = 'L';
			}
			else if (j < n && !ok[i][j + 1]) {
				if (!ok2[i][j + 1]) {
					ans[i][j + 1] = 'L';
					ok2[i][j + 1] = true;
				}
				ans[i][j] = 'R';
			}
			else {
				flag = false;
				break;
			}
			ok2[i][j] = true;
		}
		if (!flag) break;
	}
	if (!flag) {
		cout << "INVALID" << E;
	}
	else {
		cout << "VALID" << E;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				cout << ans[i][j];
			}
			cout << E;
		}
	}
	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, x[1009][1009], y[1009][1009], cnt[1009][1009];
char ans[1009][1009];
bool ok[1009][1009], ok2[1009][1009];
vector<pair<int, int> > dat;

int dfs(int i, int j, char c, int u, int v)
{
	ans[i][j] = c;
	ok[i][j] = true;
	int ret = 1;
	if (i > 1 && !ok[i - 1][j] && x[i - 1][j] == x[u][v] && y[i - 1][j] == y[u][v])
		ret += dfs(i - 1, j, 'D', u, v);
	if (i < n && !ok[i + 1][j] && x[i + 1][j] == x[u][v] && y[i + 1][j] == y[u][v])
		ret += dfs(i + 1, j, 'U', u, v);
	if (j > 1 && !ok[i][j - 1] && x[i][j - 1] == x[u][v] && y[i][j - 1] == y[u][v])
		ret += dfs(i, j - 1, 'R', u, v);
	if (j < n && !ok[i][j + 1] && x[i][j + 1] == x[u][v] && y[i][j + 1] == y[u][v])
		ret += dfs(i, j + 1, 'L', u, v);
	return ret;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	bool flag = true;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> x[i][j] >> y[i][j];
			if (x[i][j] > 0) {
				if (!ok[x[i][j]][y[i][j]]) {
					dat.pb({ x[i][j], y[i][j] });
				}
				ok[x[i][j]][y[i][j]] = true;
				cnt[x[i][j]][y[i][j]]++;
				if (i != x[i][j] || j != y[i][j]) {
					if (ok[i][j]) flag = false;
				}
			}
		}
	}
	if (!flag) {
		cout << "INVALID" << E;
		return 0;
	}
	for (int i = 0; i < (int)dat.size(); i++) {
		int tmp = dfs(dat[i].fi, dat[i].se, 'X', dat[i].fi, dat[i].se);
		if (tmp < cnt[dat[i].fi][dat[i].se]) {
			flag = false;
			break;
		}
	}
	if (!flag) {
		cout << "INVALID" << E;
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (ok[i][j]) continue;
			if (ok2[i][j]) continue;
			if (i > 1 && !ok[i - 1][j]) {
				if (!ok2[i - 1][j]) {
					ans[i - 1][j] = 'D';
					ok2[i - 1][j] = true;
				}
				ans[i][j] = 'U';
			}
			else if (i < n && !ok[i + 1][j]) {
				if (!ok2[i + 1][j]) {
					ans[i + 1][j] = 'U';
					ok2[i + 1][j] = true;
				}
				ans[i][j] = 'D';
			}
			else if (j > 1 && !ok[i][j - 1]) {
				if (!ok2[i][j - 1]) {
					ans[i][j - 1] = 'R';
					ok2[i][j - 1] = true;
				}
				ans[i][j] = 'L';
			}
			else if (j < n && !ok[i][j + 1]) {
				if (!ok2[i][j + 1]) {
					ans[i][j + 1] = 'L';
					ok2[i][j + 1] = true;
				}
				ans[i][j] = 'R';
			}
			else {
				flag = false;
				break;
			}
			ok2[i][j] = true;
		}
		if (!flag) break;
	}
	if (!flag) {
		cout << "INVALID" << E;
	}
	else {
		cout << "VALID" << E;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				cout << ans[i][j];
			}
			cout << E;
		}
	}
	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, x[1009][1009], y[1009][1009], cnt[1009][1009];
char ans[1009][1009];
bool ok[1009][1009], ok2[1009][1009];
vector<pair<int, int> > dat;

int dfs(int i, int j, char c, int u, int v)
{
	ans[i][j] = c;
	ok[i][j] = true;
	int ret = 1;
	if (i > 1 && !ok[i - 1][j] && x[i - 1][j] == x[u][v] && y[i - 1][j] == y[u][v])
		ret += dfs(i - 1, j, 'D', u, v);
	if (i < n && !ok[i + 1][j] && x[i + 1][j] == x[u][v] && y[i + 1][j] == y[u][v])
		ret += dfs(i + 1, j, 'U', u, v);
	if (j > 1 && !ok[i][j - 1] && x[i][j - 1] == x[u][v] && y[i][j - 1] == y[u][v])
		ret += dfs(i, j - 1, 'R', u, v);
	if (j < n && !ok[i][j + 1] && x[i][j + 1] == x[u][v] && y[i][j + 1] == y[u][v])
		ret += dfs(i, j + 1, 'L', u, v);
	return ret;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> x[i][j] >> y[i][j];
			if (x[i][j] > 0) {
				ans[x[i][j]][y[i][j]] = 'X';
				if (!ok[x[i][j]][y[i][j]]) {
					dat.pb({ x[i][j], y[i][j] });
				}
				ok[x[i][j]][y[i][j]] = true;
				cnt[x[i][j]][y[i][j]]++;
			}
		}
	}
	bool flag = true;
	for (int i = 0; i < (int)dat.size(); i++) {
		int tmp = dfs(dat[i].fi, dat[i].se, 'X', dat[i].fi, dat[i].se);
		if (tmp < cnt[dat[i].fi][dat[i].se]) {
			flag = false;
			break;
		}
		if (!flag) break;
	}
	if (!flag) {
		cout << "INVALID" << E;
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (ok[i][j]) continue;
			if (ok2[i][j]) continue;
			if (i > 1 && !ok[i - 1][j]) {
				if (!ok2[i - 1][j]) {
					ans[i - 1][j] = 'D';
					ok2[i - 1][j] = true;
				}
				ans[i][j] = 'U';
			}
			else if (i < n && !ok[i + 1][j]) {
				if (!ok2[i + 1][j]) {
					ans[i + 1][j] = 'U';
					ok2[i + 1][j] = true;
				}
				ans[i][j] = 'D';
			}
			else if (j > 1 && !ok[i][j - 1]) {
				if (!ok2[i][j - 1]) {
					ans[i][j - 1] = 'R';
					ok2[i][j - 1] = true;
				}
				ans[i][j] = 'L';
			}
			else if (j < n && !ok[i][j + 1]) {
				if (!ok2[i][j + 1]) {
					ans[i][j + 1] = 'L';
					ok2[i][j + 1] = true;
				}
				ans[i][j] = 'R';
			}
			else {
				flag = false;
				break;
			}
			ok2[i][j] = true;
		}
		if (!flag) break;
	}
	if (!flag) {
		cout << "INVALID" << E;
	}
	else {
		cout << "VALID" << E;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				cout << ans[i][j];
			}
			cout << E;
		}
	}
	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, x[1009][1009], y[1009][1009], cnt[1009][1009];
char ans[1009][1009];
bool ok[1009][1009], ok2[1009][1009];
vector<pair<int, int> > dat;

void print(bool flag)
{
	if (!flag) {
		cout << "INVALID" << E;
	}
	else {
		cout << "VALID" << E;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				cout << ans[i][j];
			}
			cout << E;
		}
	}
	exit(0);
}

bool gun(int i, int j, int a, int b)
{
	bool b0 = (1 <= i && i <= n);
	bool b1 = (1 <= j && j <= n);
	if (b0 && b1)
		return (!ok[i][j] && x[i][j] == a && y[i][j] == b);
	return false;
}

int dfs(int i, int j, char c, int a, int b)
{
	ans[i][j] = c;
	ok[i][j] = true;
	int ret = 1;
	if (gun(i - 1, j, a, b))
		ret += dfs(i - 1, j, 'D', a, b);
	if (gun(i + 1, j, a, b))
		ret += dfs(i + 1, j, 'U', a, b);
	if (gun(i, j - 1, a, b))
		ret += dfs(i, j - 1, 'R', a, b);
	if (gun(i, j + 1, a, b))
		ret += dfs(i, j + 1, 'L', a, b);
	return ret;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> x[i][j] >> y[i][j];
			int tx = x[i][j], ty = y[i][j];
			if (tx > 0) {
				if (!ok[tx][ty]) dat.pb({ tx, ty });
				ok[tx][ty] = true;
				cnt[tx][ty]++;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (i != x[i][j] || j != y[i][j]) {
				if (ok[i][j]) print(false);
			}
		}
	}
	for (int i = 0; i < (int)dat.size(); i++) {
		int tx = dat[i].fi, ty = dat[i].se;
		if (dfs(tx, ty, 'X', tx, ty) < cnt[tx][ty]) print(false);
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (ok[i][j] || ok2[i][j]) continue;
			if (i > 1 && !ok[i - 1][j]) {
				if (!ok2[i - 1][j]) {
					ans[i - 1][j] = 'D';
					ok2[i - 1][j] = true;
				}
				ans[i][j] = 'U';
				ok2[i][j] = true;
			}
			else if (j > 1 && !ok[i][j - 1]) {
				if (!ok2[i][j - 1]) {
					ans[i][j - 1] = 'R';
					ok2[i][j - 1] = true;
				}
				ans[i][j] = 'L';
				ok2[i][j] = true;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (!ok[i][j] && !ok2[i][j]) print(false);
		}
	}
	print(true);
	//system("pause");
	return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>1){

       sum+=qe.front() ;
         qe.pop_front();

   if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;
}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}

}

if (qe.size()>1){cout<<l+1;}
else if (qe.size()<1){cout<<l;}

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

   if (qe.size()>1 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n  , l=0   ; cin>>n ;
 vector<int > v ;
 for(int i=0 ;i<n ;++i){
    int y  ; cin>>y ;
    if (y==4){
        l+=1 ;

    }
    else {v.push_back(y);}
 }
 for(int i=0 ; i<v.size() ;++i){
    for(int j=i+1 ;j<v.size() ;++j){
        if (v[i]+v[j]==4){
            l+=1 ;
            v[i]=0 ; v[j]=0 ;

        }
    }

 }
 for(int i=0 ; i<v.size() ;++i){
    if (v[i]!=0){
        l+=1 ;

    }
 }
cout<<l ;

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){

        if (qe.size()==1){
            l+=1 ;

            break ;
        }
       sum+=qe.front() ;
         qe.pop_front();
  if (sum+qe.front()==4){
    l+=1 ;

    qe.pop_front();

    sum=0;

}

 else if (sum+qe.back()<4){
        sum+=qe.back() ;
    qe.pop_back() ;

}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}

else if (sum+qe.front()>4){
    l+=1 ;

    sum=0 ;

}

}

if (sum>0){cout<<l+1;}
else if (sum==0){cout<<l;}

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){
    int o=0 ;
        o++ ;
        if (qe.size()<2){
          if (qe.back()+qe.front()>4){
        cout<<"2" ;
        break ;}
    else if (qe.back()+qe.front()<4){
            cout<<"1" ;
       break ;
    }
      break ;
        }
        sum+=qe.front() ;
 if (sum+qe.back()<4){
    qe.pop_back() ;
    qe.pop_front() ;
sum+=qe.back() ;
}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;
    qe.pop_front() ;
    sum=0 ;

}
else if (sum+arr[o]>4){
    l+=1 ;

    qe.pop_front() ;
    sum=0 ;

}

}
cout<<l;

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int p , l=0 , arr[90]; cin>>p ;
  deque <int > qe ;
  for(int i=0 ; i<p;++i){
        cin>>arr[i] ;
  }
  sort(arr ,arr+p);
  for(int i=0 ; i<p;++i){

    qe.push_back(arr[i]) ;
 }
for(int i=0 ; i<p ;i++){
    if(qe.back()==4){
        l+=1 ;
        qe.pop_back() ;

    }

   else  if (qe.front()+qe.back()==4){
        l+=1 ;
         qe.pop_back() ;
            qe.pop_front() ;
        i+=1 ;
    }
}
cout<<l+qe.size() ;

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){

        if (qe.size()==1){
            l+=1 ;
      sum=0 ;
            break ;
        }
       sum+=qe.front() ;
         qe.pop_front();
  if (sum+qe.front()==4){
    l+=1 ;

    qe.pop_front();

    sum=0;

}

 else if (sum+qe.back()<4){
        sum+=qe.back() ;
    qe.pop_back() ;

}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}

else if (sum+qe.front()>4){
    l+=1 ;

    sum=0 ;

}

}

if (sum>0){cout<<l+1;}
else if (sum==0){cout<<l;}

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }
 if (qe.size()==1){
    cout<<l+1 ;
    return 0 ;
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

if (sum==4&& qe.size()==0){
    l+=1 ;
    sum=0 ;
    break;
}
else if (sum!=4 &&qe.size()==0){
    break;
}

   else if (qe.size()>0 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (qe.size()>0 && sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }
 if (qe.size()==1){
    cout<<l+1 ;
    return 0 ;
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

if (sum==4&& qe.size()==0){
    l+=1 ;
    sum=0 ;
    break;
}
   else if (qe.size()>0 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (qe.size()>0 && sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }
 if (qe.size()==1){
    cout<<l+1 ;
    return 0 ;
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

if (sum==4&& qe.size()==0){
    l+=1 ;
    sum=0 ;
    break;
}
else if (sum<4 &&qe.size()==0){
    break;
}

   else if (qe.size()>0 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (qe.size()>0 && sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

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

    qe.push_back(arr[i]);
 }
 if (qe.size()==1){
    cout<<"1" ;
    return 0 ;
 }

while (qe.size()>0){
    if (qe.front()==4){
            l+=1 ;
        }

       sum+=qe.front() ;
         qe.pop_front();

     if (qe.size()>0 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (qe.size()>0 && sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include<map>
#include<cmath>
using namespace std;

int main()
{

  int n  ;cin>>n ;
  float l =0.0 ;
  map<int , float > m ;
  for(int i=0 ;i<n ;++i){
    int t ;cin>>t ;
    if (t==1){
        m[1]+=0.25 ;
    }
    else if (t==2){
        m[2]+=0.5 ;
    }
    else if (t==3){
        m[3]+=0.76 ;
    }
    else if (t==4){
        m[4]+=1 ;
    }
    }
l+=m[1]+m[2]+m[3]+m[4];
l=ceil(l);
cout<<(int)l;

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }
  sort(arr ,arr+n);
  for(int i=0 ; i<n;++i){

    qe.push_back(arr[i]) ;
 }
for(int i=0 ; i<n ;i++){
    if(qe.back()==4){
        l+=1 ;
        qe.pop_back() ;

    }

   else  if (qe.front()+qe.back()==4){
        l+=1 ;
         qe.pop_back() ;
            qe.pop_front() ;
        i+=1 ;
    }
}
cout<<l+qe.size() ;

    return 0;
}


#include <iostream>

using namespace std;

int main()
{
   int arr[900] , m ,sum=0 , l=0 ;
   cin>>m ;
   for(int i=0 ; i<m ;++i){
    cin>>arr[i] ;
    sum+=arr[i] ;

   }
   for(int i=0 ; i<m ; ++i){
    if (arr[i]==4){
        l+=1 ;
        sum-=4 ;
        arr[i]=0 ;
        continue ;
    }
    for(int j=i+1 ;j<m ;++j) {

        if (arr[i]+arr[j]==4){
            l+=1 ;
            arr[i]=0 ; arr[j]=0 ;
            sum-=4 ;
        }
    }
   }
   while(sum>0){
        int y=0 ;

       sum-=arr[y]  ;
   if(arr[y]!=0){
    l+=1 ;
   }
   y++ ;

   }
   cout<< l ;
    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

   if (qe.size()>1 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (qe.size()>1 && sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (qe.size()>1 && sum+qe.back()>4 && sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
 else if (qe.size()>1 && sum+qe.front()>4 && sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

else if (qe.size()>1 && sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){

        if (qe.size()==1){
            l+=1 ;

            break ;
        }
       sum+=qe.front() ;
         qe.pop_front();
  if (sum+qe.front()==4){
    l+=1 ;

    qe.pop_front();

    sum=0;

}

 else if (sum+qe.back()<4){
        sum+=qe.back() ;
    qe.pop_back() ;

}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;
    qe.pop_front() ;
    sum=0 ;

}

else if (sum+qe.front()>4){
    l+=1 ;

    sum=0 ;

}

}

if (sum>0){cout<<l+1;}
else if (sum==0){cout<<l;}

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){
    int o=0 ;
        o++ ;

        if (qe.size()==1){
            l+=1 ;
            break ;
        }
        sum+=qe.front() ;
else if (sum+qe.back()<4){
    qe.pop_back() ;
    qe.pop_front() ;
sum+=qe.back() ;
}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;
    qe.pop_front() ;
    sum=0 ;

}
else if (sum+arr[o]>4){
    l+=1 ;

    qe.pop_front() ;
    sum=0 ;

}

}
if (sum!=0){cout<<"1" ;}
else if (sum==0){cout<<l;}

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }
 if (qe.size()==1){
    cout<<"1" ;
    return 0 ;
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

   if (qe.size()>0 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (qe.size()>0 && sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){
    int o=0 ;
        o++ ;
        sum+=qe.front() ;
if (sum+qe.back()<4){
    qe.pop_back() ;
    qe.pop_front() ;
sum+=qe.back() ;
}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;
    qe.pop_front() ;
    sum=0 ;

}
else if (sum+arr[o]>4){
    l+=1 ;

    qe.pop_front() ;
    sum=0 ;

}

}
if (sum!=0){cout<<"1" ;}
else if (sum==0){cout<<l;}

    return 0;
}


#include <iostream>
#include<map>
#include<cmath>
using namespace std;

int main()
{

  int n  ;cin>>n ;
  float l =0.0 ;
  map<int , float > m ;
  for(int i=0 ;i<n ;++i){
    int t ;cin>>t ;
    if (t==1){
        m[1]+=0.25 ;
    }
    else if (t==2){
        m[2]+=0.5 ;
    }
    else if (t==3){
        m[3]+=0.75 ;
    }
    else if (t==4){
        m[4]+=1 ;
    }
    }
l+=m[1]+m[2]+m[3]+m[4];
l=ceil(l);
cout<<(int)l;

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }
  sort(arr ,arr+n);
  for(int i=0 ; i<n;++i){

    qe.push_back(arr[i]) ;
 }
for(int i=0 ; i<qe.size() ;++i){
    if(qe.back()==4){
        l+=1 ;
        qe.pop_back() ;
    }

    else if (qe.front()+qe.back()==4){
        l+=1 ;
         qe.pop_back() ;
            qe.pop_front() ;
    }
}
for(int i=0 ;i<qe.size() ;++i){
    l+=1 ;
    qe.pop_front () ;
}
    cout<<l;

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){
    int o=0 ;
        o++ ;
        sum+=qe.front() ;
if (sum+qe.back()<4){
    qe.pop_back() ;
    qe.pop_front() ;
sum+=qe.back() ;
}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;
    qe.pop_front() ;
    sum=0 ;

}
else if (sum+arr[o]>4){
    l+=1 ;

    qe.pop_front() ;
    sum=0 ;

}

}
cout<<l;

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>1){

       sum+=qe.front() ;
         qe.pop_front();

   if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;
}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}

}

if (qe.size()>1){cout<<l+1;}
else if (qe.size()<1 &&sum<4){
    cout<<"1" ;
}
else if (qe.size()<1 &&sum>4){
    cout<<"2" ;
}
else if (qe.size()<1){cout<<l;}

    return 0;
}


#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n  , l=0 , sum=0  ; cin>>n ;
 vector<int > v ;
 for(int i=0 ;i<n ;++i){
    int y  ; cin>>y ;
    if (y==4){
        l+=1 ;

    }
    else {v.push_back(y); sum+=y ;}
 }
 for(int i=0 ; i<v.size() ;++i){
    for(int j=i+1 ;j<v.size() ;++j){
        if (v[i]+v[j]==4){
            l+=1 ;
            v[i]=0 ; v[j]=0 ;
            sum-=v[i];
            sum-=v[j] ;
        }
    }

 }
 for(int i=0 ; i<v.size() ;++i){

    if (v[i]!=0 && sum%4==0 ){
        l+=1 ;
sum-=4  ; i+=3;
    }
    else if (v[i]!=0 && sum<4 ){
        l+=1 ;
          i+=1 ;  }
 }
cout<<l ;

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

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

    qe.push_back(arr[i]) ;
 }
 if (qe.back()==1 &&qe.front()==1){
    cout<<qe.size()/4;

    return 0 ;
  }
for(int i=0 ; i<n ;i++){
    if(qe.back()==4){
        l+=1 ;
        qe.pop_back() ;

    }

   else  if (qe.front()+qe.back()==4){
        l+=1 ;
         qe.pop_back() ;
            qe.pop_front() ;
        i+=1 ;
    }

}
cout<<l+qe.size() ;

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[100000]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }
 if (qe.size()==1){
    cout<<l+1 ;
    return 0 ;
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

if (sum==4&& qe.size()==0){
    l+=1 ;
    sum=0 ;
    break;
}
else if (sum==4 && qe.size()>0){
    l+=1 ;
    sum=0 ;
}
else if (sum!=4 &&qe.size()==0){
    break;
}

   else if (qe.size()>0 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (qe.size()>0 && sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n  , l=0 ,  ; cin>>n ;
 vector<int > v ;
 for(int i=0 ;i<n ;++i){
    int y  ; cin>>y ;
    if (y==4){
        l+=1 ;

    }
    else {v.push_back(y);}
 }
 for(int i=0 ; i<v.size() ;++i){
    for(int j=i+1 ;j<v.size() ;++j){
        if (v[i]+v[j]==4){
            l+=1 ;
            v[i]=0 ; v[j]=0 ;

        }
    }

 }
 for(int i=0 ; i<v.size() ;++i){
    if (v[i]!=0){
        l+=1 ;

    }
 }
cout<<l ;

    return 0;
}


#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n  , l=0 , sum=0  ; cin>>n ;
 vector<int > v ;
 for(int i=0 ;i<n ;++i){
    int y  ; cin>>y ;
    if (y==4){
        l+=1 ;

    }
    else {v.push_back(y);}
 }
 for(int i=0 ; i<v.size() ;++i){
    for(int j=i+1 ;j<v.size() ;++j){
        if (v[i]+v[j]==4){
            l+=1 ;
            v[i]=0 ; v[j]=0 ;

        }
    }

 }
 for(int i=0 ; i<v.size() ;++i){
        sum+=v[i] ;
    if (v[i]!=0 && sum==4 && i>3){
        l+=1 ;
sum=0 ;
    }
    else if (v[i]!=0 && sum!=4 &&i<3){
        l+=1 ;}
 }
cout<<l ;

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 ,sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
        sum+=arr[i];
  }

  sort(arr ,arr+n);

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

    qe.push_back(arr[i]) ;
 }
 if (sum<4){
     cout<<"1";
     return 0;
 }
 if (qe.back()==1 &&qe.front()==1){
    cout<<qe.size()/4;

    return 0 ;
  }
for(int i=0 ; i<n ;i++){
    if(qe.back()==4){
        l+=1 ;
        qe.pop_back() ;

    }

   else  if (qe.front()+qe.back()==4){
        l+=1 ;
         qe.pop_back() ;
            qe.pop_front() ;
        i+=1 ;
    }

}
cout<<l+qe.size() ;

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){
    int o=0 ;
        o++ ;

        if (qe.size()==1){
            l+=1 ;
            break ;
        }
        sum+=qe.front() ;
 if (sum+qe.back()<4){
    qe.pop_back() ;
    qe.pop_front() ;
sum+=qe.back() ;
}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;
    qe.pop_front() ;
    sum=0 ;

}
else if (sum+arr[o]==4){
    l+=1 ;
    qe.pop_front();
    qe.pop_front();

}
else if (sum+arr[o]>4){
    l+=1 ;

    qe.pop_front() ;
    sum=0 ;

}

}
if (sum!=0){cout<<l+1;}
else if (sum==0){cout<<l;}

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){

        if (qe.size()==1){
            l+=1 ;
      sum=0 ;
            break ;
        }
       sum+=qe.front() ;
         qe.pop_front();
  if (sum+qe.front()==4){
    l+=1 ;

    qe.pop_front();

    sum=0;

}

 else if (sum+qe.back()<4){
        sum+=qe.back() ;
    qe.pop_back() ;

}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}

else if (sum+qe.front()>4){
    l+=1 ;

    sum=0 ;

}

}

if (sum>0){cout<<l+1;}
else if (sum==0){cout<<l;}

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

   if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n  , l=0 , sum=0  ; cin>>n ;
 vector<int > v ;
 for(int i=0 ;i<n ;++i){
    int y  ; cin>>y ;
    if (y==4){
        l+=1 ;

    }
    else {v.push_back(y); sum+=y ;}
 }
 for(int i=0 ; i<v.size() ;++i){
    for(int j=i+1 ;j<v.size() ;++j){
        if (v[i]+v[j]==4){
            l+=1 ;
            v[i]=0 ; v[j]=0 ;
            sum-=v[i];
            sum-=v[j] ;
        }
    }

 }
 for(int i=0 ; i<v.size() ;++i){

    if (v[i]!=0 && sum%4==0 ){
        l+=1 ;
sum-=4  ; i+=3;
    }
    else if (v[i]!=0 && sum<4 && sum>0){
        l+=1 ;
          i+=1 ;  }
 }
cout<<l ;

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }
 if (qe.size()==1){
    cout<<l+1 ;
    return 0 ;
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

if (sum==4&& qe.size()==0){
    l+=1 ;
    sum=0 ;
    break;
}
else if (sum==4 && qe.size()>0){
    l+=1 ;
    sum=0 ;
}
else if (sum!=4 &&qe.size()==0){
    break;
}

   else if (qe.size()>0 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (qe.size()>0 && sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){
    int o=0 ;
        o++ ;

        if (qe.size()==1){
            l+=1 ;
            sum=0 ;
            break ;
        }
       sum+=qe.front() ;

 if (sum+qe.back()<4){
        sum+=qe.back() ;
    qe.pop_back() ;
    qe.pop_front() ;

}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;
    qe.pop_front() ;
    sum=0 ;

}
else if (sum+arr[o]==4){
    l+=1 ;
    qe.pop_front();
    qe.pop_front();

}
else if (sum+arr[o]>4){
    l+=1 ;

    qe.pop_front() ;
    sum=0 ;

}

}
if (sum!=0){cout<<l+1;}
else if (sum==0){cout<<l;}

    return 0;
}


#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

 sort(arr,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }
 if (qe.size()==1){
    cout<<l+1 ;
    return 0 ;
 }

while (qe.size()>0){

       sum+=qe.front() ;
         qe.pop_front();

   if (qe.size()>0 && sum+qe.back()==4 ){
    l+=1 ;
    qe.pop_back() ;

    sum=0 ;

}
 else if (sum+qe.front()==4){
    l+=1 ;
    qe.pop_front() ;

    sum=0 ;

}
else if (qe.size()>0 && sum+qe.back()>4 &&sum+qe.front()!=4){
    sum=0 ;
    qe.pop_back() ;
    l+=2;

}
 else if (sum+qe.front()>4 &&sum+qe.back()!=4){
    sum=0 ;
    qe.pop_front() ;
    l+=2;
}

else if (sum+qe.front()<4){
     sum+= qe.front();
    qe.pop_front() ;
}

}

if (sum==0){cout<<l;}
else if (sum<4){
    cout<<l+1 ;
}
else if (sum>4){
    cout<<l+2 ;
}

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , sum=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }

  sort(arr ,arr+n);

  for(int i=0 ; i<n;++i){
        if (arr[i]==4){
            l+=1 ;
        }
      else  { qe.push_back(arr[i]);}
 }

while (qe.size()>0){
    int o=0 ;
        o++ ;

        if (qe.size()==1){
            l+=1 ;
            break ;
        }
        sum+=qe.front() ;
 if (sum+qe.back()<4){
    qe.pop_back() ;
    qe.pop_front() ;
sum+=qe.back() ;
}
else if (sum+qe.back()==4){
    l+=1 ;
    qe.pop_back() ;
    qe.pop_front() ;
    sum=0 ;

}
else if (sum+arr[o]>4){
    l+=1 ;

    qe.pop_front() ;
    sum=0 ;

}

}
if (sum!=0){cout<<l+1;}
else if (sum==0){cout<<l;}

    return 0;
}


#include <iostream>
#include <deque>
#include <bits/stdc++.h>
using namespace std;

int main()
{
 int n , l=0 , arr[90]; cin>>n ;
  deque <int > qe ;
  for(int i=0 ; i<n;++i){
        cin>>arr[i] ;
  }
  sort(arr ,arr+n);
  for(int i=0 ; i<n;++i){

    qe.push_back(arr[i]) ;
 }
for(int i=0 ; i<qe.size() ;++i){
    if(qe.back()==4){
        l+=1 ;
        qe.pop_back() ;
        continue ;
    }

    else if (qe.front()+qe.back()==4){
        l+=1 ;
         qe.pop_back() ;
            qe.pop_front() ;
    }
}

    cout<<l+qe.size();

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int size=10000, ones=0,twos=0, threes=0;
	bool one=false, two=false, three=false;
	cin>>size;
	int sum=0, num, tum=0;
	int mod=0;
	int nums[size];

	for(int i=0; i<size; i++)
	{
		cin>>nums[i];
		 if(nums[i]==3)
	    {
		three=true;
	    threes++;
	   }
	    else if(nums[i]==2)
	    {

	    twos++;
	    two=true;
     	}
	    else if(nums[i]==1)
	    {
			one=true;
	        ones++;
	    }
	    else if(nums[i]==4)
	    {
			mod++;
	    }

	}

     	if(three)
     	{

	    if(ones<threes)
	    {
	    	mod+=threes;

	    	ones=0;
	    	one=false;

		}

		else if(ones>threes)
		{
		   mod+=threes;
		  ones=ones-threes;

		}

		else if (ones==threes)
		{

		  mod+=threes;
		  ones=0;
		  one=false;
	    }
	   }

	if(two && ones>0)
		{

		if(twos>ones)
		{
		if(ones<=4)
		{
		 mod+=(twos+1)/2;
		 mod+=(ones+1)/4;
	    }
		else
			mod+=(twos+ones)/2;

			if(twos%2==0 && ones%2!=0)
			{

				one=true;
			}
			else
			one=false;
		}
    	else if(twos<ones)
		{
			ones-=twos*2;
			mod+=twos;
	   }
	   	else if (ones==twos)
			{
	         if(twos%2==0 && twos>2)
		       mod+=twos-1;
		       else
		       mod+=twos;
		     one=false;
		 }
		}
	else if(two)
	{
		if(twos%2==0)
		mod+=twos/2;
		else
		mod+=((twos-1)/2)+1;
		one=false;
	}
		if(one && ones>0)
		{
			if(ones<=4)
			  mod++;
			  else if (ones%4==0)
			  {
			  	mod+=ones/4;
			  }
			else
			{
				mod+=((ones-(ones%4))/4)+1;
			}
	}
	cout<<mod;

}


import java.util.Scanner;
public class C2_P1
{

	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int q=sc.nextInt();
		int [] l =new int [q];
		int [] r =new int [q];
		int [] d =new int [q];
		for(int i=0;i<q;i++)
		{
			 l[i] =sc.nextInt();
			 r[i] =sc.nextInt();
			 d[i] =sc.nextInt();

			if(l[i]>d[i])
			{
				System.out.println(d[i]);
			}
			else
			{
			    int p=r[i]+1;
			    while(p%d[i]!=0)
			    {
				  p=p+1;
			    }
				System.out.println(p);

			}
		}

	}

}


import java.util.Scanner;
public class C2_P1
{

	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int q=sc.nextInt();
		int [] l =new int [q];
		int [] r =new int [q];
		int [] d =new int [q];
		for(int i=0;i<q;i++)
		{
			 l[i] =sc.nextInt();
			 r[i] =sc.nextInt();
			 d[i] =sc.nextInt();

			if(l[i]>d[i])
			{
				System.out.println(d[i]);
			}
			else
			{
			    int p=(r[i]/d[i])+1;

				System.out.println(p*d[i]);

			}
		}

	}

}


/*
USER: caoash3
LANG: JAVA
TASK:
*/

import java.io.*;
import java.util.*;

public class min {
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

		//BufferedReader br = new BufferedReader(new FileReader("X.in"));
		//PrintWriter pw = new PrintWriter(new FileWriter("X.out"));

		X solver = new X();
		solver.solve(br, pw);
	}

	static class X {
		public void solve(BufferedReader br, PrintWriter pw) throws IOException {

			long n = Long.parseLong(br.readLine());

			for(int i = 0; i < n; i++) {
				StringTokenizer st = new StringTokenizer(br.readLine());
				long l = Long.parseLong(st.nextToken());
				long r = Long.parseLong(st.nextToken());
				long x = Long.parseLong(st.nextToken());

				if(x >= l) {
					if((r+1)==x) {
						pw.println(r+1);
					}
					else {
					pw.println((r+1) + x - ((r+1)%x));
					}
				}
				else {
					pw.println(x);
				}

			}

			pw.close();
		}
	}
}


/*
USER: caoash3
LANG: JAVA
TASK:
*/

import java.io.*;
import java.util.*;

public class min {
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

		//BufferedReader br = new BufferedReader(new FileReader("X.in"));
		//PrintWriter pw = new PrintWriter(new FileWriter("X.out"));

		X solver = new X();
		solver.solve(br, pw);
	}

	static class X {
		public void solve(BufferedReader br, PrintWriter pw) throws IOException {

			int n = Integer.parseInt(br.readLine());

			for(int i = 0; i < n; i++) {
				StringTokenizer st = new StringTokenizer(br.readLine());
				int l = Integer.parseInt(st.nextToken());
				int r = Integer.parseInt(st.nextToken());
				int x = Integer.parseInt(st.nextToken());

				int cnt = 2;

				while(x*cnt <= r) {
					cnt++;
				}
				pw.println(x*cnt);

			}

			pw.close();
		}
	}
}


/*
USER: caoash3
LANG: JAVA
TASK:
*/

import java.io.*;
import java.util.*;

public class min {
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

		//BufferedReader br = new BufferedReader(new FileReader("X.in"));
		//PrintWriter pw = new PrintWriter(new FileWriter("X.out"));

		X solver = new X();
		solver.solve(br, pw);
	}

	static class X {
		public void solve(BufferedReader br, PrintWriter pw) throws IOException {

			int n = Integer.parseInt(br.readLine());

			for(int i = 0; i < n; i++) {
				StringTokenizer st = new StringTokenizer(br.readLine());
				int l = Integer.parseInt(st.nextToken());
				int r = Integer.parseInt(st.nextToken());
				int x = Integer.parseInt(st.nextToken());

				int cnt = x;

				while((cnt % x != 0)|| (cnt >= l && cnt <= r)) {
					cnt++;
				}

				pw.println(cnt);

			}

			pw.close();
		}
	}
}


/*
USER: caoash3
LANG: JAVA
TASK:
*/

import java.io.*;
import java.util.*;

public class min {
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

		//BufferedReader br = new BufferedReader(new FileReader("X.in"));
		//PrintWriter pw = new PrintWriter(new FileWriter("X.out"));

		X solver = new X();
		solver.solve(br, pw);
	}

	static class X {
		public void solve(BufferedReader br, PrintWriter pw) throws IOException {

			int n = Integer.parseInt(br.readLine());

			for(int i = 0; i < n; i++) {
				StringTokenizer st = new StringTokenizer(br.readLine());
				int l = Integer.parseInt(st.nextToken());
				int r = Integer.parseInt(st.nextToken());
				int x = Integer.parseInt(st.nextToken());

				int cnt = 1;

				while((x*cnt >= l && x * cnt <= r)) {
					cnt++;
				}
				pw.println(x * cnt);
			}

			pw.close();
		}
	}
}


/*
USER: caoash3
LANG: JAVA
TASK:
*/

import java.io.*;
import java.util.*;

public class min {
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

		//BufferedReader br = new BufferedReader(new FileReader("X.in"));
		//PrintWriter pw = new PrintWriter(new FileWriter("X.out"));

		X solver = new X();
		solver.solve(br, pw);
	}

	static class X {
		public void solve(BufferedReader br, PrintWriter pw) throws IOException {

			long n = Long.parseLong(br.readLine());

			for(int i = 0; i < n; i++) {
				StringTokenizer st = new StringTokenizer(br.readLine());
				long l = Long.parseLong(st.nextToken());
				long r = Long.parseLong(st.nextToken());
				long x = Long.parseLong(st.nextToken());

				if(x >= l) {
					if((r+1)%x==0) {
						pw.println(r+1);
					}
					else {
					pw.println((r+1) + x - ((r+1)%x));
					}
				}
				else {
					pw.println(x);
				}

			}

			pw.close();
		}
	}
}


/*
USER: caoash3
LANG: JAVA
TASK:
*/

import java.io.*;
import java.util.*;

public class min {
	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

		//BufferedReader br = new BufferedReader(new FileReader("X.in"));
		//PrintWriter pw = new PrintWriter(new FileWriter("X.out"));

		X solver = new X();
		solver.solve(br, pw);
	}

	static class X {
		public void solve(BufferedReader br, PrintWriter pw) throws IOException {

			long n = Long.parseLong(br.readLine());

			for(int i = 0; i < n; i++) {
				StringTokenizer st = new StringTokenizer(br.readLine());
				long l = Long.parseLong(st.nextToken());
				long r = Long.parseLong(st.nextToken());
				long x = Long.parseLong(st.nextToken());
				long cnt = 1;

				while((x*cnt >= l && x * cnt <= r)) {
					cnt++;
				}
				pw.println(x * cnt);
			}

			pw.close();
		}
	}
}


import java.util.*;

public class P1101A_Minimum_Integer {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        for(int k = 0; k < loop; k++){
            int l = sc.nextInt();
            int r = sc.nextInt();
            int d = sc.nextInt();
            boolean flag = true;
            long product = 1;
            for(int i = 1; product < l; i++) {
                product  = d * i;
                if(product == 0) {
                    continue;
                }
                if(!(product >= l && product <= r)) {
                    flag = false;
                    break;
                }
            }
            if(flag == false){
                System.out.println(product);
            }
            else {
                product = 1;
                long div =(long) r / (long)d;
                for(long i = div; true; i++) {
                    product  = d * i;
                    if(product == 0) {
                        continue;
                    }
                    if(!(product >= l && product <= r)) {
                        flag = false;
                        break;
                    }
                }
                System.out.println(product);
            }
        }
    }
}


import java.util.*;

public class P1101A_Minimum_Integer {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int loop = sc.nextInt();
        for(int k = 0; k < loop; k++){
            int l = sc.nextInt();
            int r = sc.nextInt();
            int d = sc.nextInt();
            boolean flag = true;
            long product = 1;
            for(int i = 1; flag; i++) {
                product  = d * i;
                if(!(product >= l && product <= r)) {
                    flag = false;
                    break;
                }
            }
            System.out.println(product);
        }
    }
}


import java.util.*;
public class tht
{

    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int i=0;i<t;i++)
        {
            int l=in.nextInt();
            int r=in.nextInt();
            int d=in.nextInt();
            long ans=(r+d-r%d);
              if(d<l)
              ans=d;
            System.out.println(ans);
        }
    }
}


import java.util.*;
public class tht
{

    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int i=0;i<t;i++)
        {
            int l=in.nextInt();
            int r=in.nextInt();
            int d=in.nextInt();
            long ans=(r+d-r%d);
            if(l%d!=0)
            long ans1=l-l%d;
            else
            long ans1=l-d;
            if(ans1>0)
            ans=ans1;
            System.out.println(ans);
        }
    }
}


import java.util.*;
public class tht
{

    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int i=0;i<t;i++)
        {
            int l=in.nextInt();
            int r=in.nextInt();
            int d=in.nextInt();
            long ans=(r+d-r%d);
            long ans1=0;
            if(l%d!=0)
            ans1=l-l%d;
            else
            ans1=l-d;
            if(ans1>0)
            ans=ans1;
            System.out.println(ans);
        }
    }
}


import java.util.*;
public class tht
{

    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int i=0;i<t;i++)
        {
            int l=in.nextInt();
            int r=in.nextInt();
            int d=in.nextInt();
            long ans=(r+d-r%d);
            long ans1=(l+l%d-l);
            if(ans1>0)
            ans=ans1;
            System.out.println(ans);
        }
    }
}


import java.util.*;
public class tht
{

    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int i=0;i<t;i++)
        {
            int l=in.nextInt();
            int r=in.nextInt();
            int d=in.nextInt();
            long ans=(r+d-r%d);
            System.out.println(ans);
        }
    }
}


import java.util.*;
public class tht
{

    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int i=0;i<t;i++)
        {
            int l=in.nextInt();
            int r=in.nextInt();
            int d=in.nextInt();
            long ans=(r+d-r%d);
            long ans1=0;
            for(int j=1;j<l;j++)
            {
                if(j%d==0)
                {
                    ans1=j;
                    break;
                }
            }
            if(ans1!=0)
            ans=ans1;
            System.out.println(ans);
        }
    }
}


package cfEduRound58div2A;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int q = input.nextInt();
		for(int times = 1;times<=q;times++) {
			int l = input.nextInt();
			int r = input.nextInt();
			int d = input.nextInt();
			if(d>=l && d<=r) {
				System.out.println(r-r%d+d);
			}else {
				System.out.println(d);
			}
		}
		input.close();
	}
}


import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int q = input.nextInt();
		for(int times = 1;times<=q;times++) {
			int l = input.nextInt();
			int r = input.nextInt();
			int d = input.nextInt();
			if(l-l%d>0 && l%d!=0) {
				System.out.println(l-l%d);
				continue;
			}else if(l%d==0 && l-d>0) {
				System.out.println(l-d);
				continue;
			}else if(r%d==0) {
				System.out.println(r+d);
				continue;
			}else if(r%d!=0) {
				System.out.println(r-r%d+d);
			}
		}
		input.close();
	}
}


package cfEduRound58div2A;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int q = input.nextInt();
		for(int times = 1;times<=q;times++) {
			int l = input.nextInt();
			int r = input.nextInt();
			int d = input.nextInt();
			if(l-l%d>0 && l%d!=0) {
				System.out.println(l-l%d);
				continue;
			}else if(l%d==0 && l-d>0) {
				System.out.println(l-d);
				continue;
			}else if(r%d==0) {
				System.out.println(r+d);
				continue;
			}else if(r%d!=0) {
				System.out.println(r-r%d+d);
				continue;
			}
		}
		input.close();
	}
}


package cfEduRound58div2A;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int q = input.nextInt();
		for(int times = 1;times<=q;times++) {
			int l = input.nextInt();
			int r = input.nextInt();
			int d = input.nextInt();
			if(l-l%d>0 && l%d!=0) {
				System.out.println(l-l%d);
				continue;
			}else if(l%d==0 && l-d>0) {
				System.out.println(l-d);
				continue;
			}else if(r%d==0) {
				System.out.println(r+d);
				continue;
			}else if(r%d!=0) {
				System.out.println(r-r%d+d);
			}
		}
		input.close();
	}
}


import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int q = input.nextInt();
		for(int times = 1;times<=q;times++) {
			int l = input.nextInt();
			int r = input.nextInt();
			int d = input.nextInt();
			if(d>=l && d<=r) {
				System.out.println(r-r%d+d);
				continue;
			}else {
				System.out.println(d);
			}
		}
		input.close();
	}
}


import java.util.Scanner;
public class A {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int q = input.nextInt();
		for(int times = 1;times<=q;times++) {
			int l = input.nextInt();
			int r = input.nextInt();
			int d = input.nextInt();
			if(l-l%d>0 && l%d!=0) {
				System.out.println(l-l%d);
				continue;
			}else if(l%d==0 && l-d>0) {
				System.out.println(l-d);
				continue;
			}else if(r%d==0) {
				System.out.println(r+d);
				continue;
			}else if(r%d!=0) {
				System.out.println(r-r%d+d);
			}
		}
		input.close();
	}
}


package cfEduRound58div2A;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int q = input.nextInt();
		for(int times = 1;times<=q;times++) {
			int l = input.nextInt();
			int r = input.nextInt();
			int d = input.nextInt();
			if(d>=l && d<=r) {
				System.out.println(r-r%d+d);
				continue;
			}else {
				System.out.println(d);
			}
		}
		input.close();
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class csdCDc {

	public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader  br= new BufferedReader(new InputStreamReader( System.in));
		Scanner sc =new Scanner(System.in);
		int n =Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++) {
			StringTokenizer st =new StringTokenizer(br.readLine());
			long x=Integer.parseInt(st.nextToken());
			long y=Integer.parseInt(st.nextToken());
			long d=Integer.parseInt(st.nextToken());
			if(d<x || d>y) {
				System.out.println(d);
			}
			else {
				long g=((y-x)/d);
				if((y-x)%d==0)
					;
				else
					g++;
				System.out.println((g+1)*d);
			}

		}

	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class csdCDc {

	public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader  br= new BufferedReader(new InputStreamReader( System.in));
		Scanner sc =new Scanner(System.in);
		int n =Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++) {
			StringTokenizer st =new StringTokenizer(br.readLine());
			long x=Integer.parseInt(st.nextToken());
			long y=Integer.parseInt(st.nextToken());
			long d=Integer.parseInt(st.nextToken());
			if(d<x || d>y) {
				System.out.println(d);
			}
			else {
				long g=((y-x)/d+1)*d;
				System.out.println(g+d);
			}

		}

	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class csdCDc {

	public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader  br= new BufferedReader(new InputStreamReader( System.in));
		Scanner sc =new Scanner(System.in);
		int n =Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++) {
			StringTokenizer st =new StringTokenizer(br.readLine());
			long x=Integer.parseInt(st.nextToken());
			long y=Integer.parseInt(st.nextToken());
			long d=Integer.parseInt(st.nextToken());
			if(d<x || d>y) {
				System.out.println(d);
			}
			else {
				long g=((y-x)/d);
				if((y-x)%d==0 && d!=x)
					;
				else
					g++;
				System.out.println((g+1)*d);
			}

		}

	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class csdCDc {

	public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader  br= new BufferedReader(new InputStreamReader( System.in));
		Scanner sc =new Scanner(System.in);

		int n =Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++) {
			StringTokenizer st =new StringTokenizer(br.readLine());
			int x=Integer.parseInt(st.nextToken());
			int y=Integer.parseInt(st.nextToken());
			int d=Integer.parseInt(st.nextToken());
			if(d<x || d>y) {
				System.out.println(d);
			}
			else {
				int g=((y-x)/d+1)*d;
				System.out.println(g+d);
			}

		}

	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class csdCDc {

	public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader  br= new BufferedReader(new InputStreamReader( System.in));
		Scanner sc =new Scanner(System.in);
		int n =Integer.parseInt(br.readLine());
		for(int i=0;i<n;i++) {
			StringTokenizer st =new StringTokenizer(br.readLine());
			long x=Integer.parseInt(st.nextToken());
			long y=Integer.parseInt(st.nextToken());
			long d=Integer.parseInt(st.nextToken());
			if(d<x || d>y) {
				System.out.println(d);
			}
			else {
				long g=((y-x)/d);
				//System.out.println(g);
				if(d*g>y)
					System.out.println(d*g);
				else if(d*(g+1)>y)
					System.out.println(d*(g+1));
				else
					System.out.println(d=d*(g+2));
			}

		}

	}
}


import java.util.Scanner;
import java.io.IOException;
public class Mint{
	public static void main(String[] args)throws IOException{
		try{
			Scanner sc= new Scanner(System.in);
			int q=sc.nextInt();
			int arr[][]=new int[q][3];
			int i;
			for(i=0;i<q;i++){
				for(int j=0;j<3;j++){
					arr[i][j]=sc.nextInt();
				}
			}
			for(i=0;i<q;i++){
			    for(int j=0;j<(3);j+=3){
			    if(arr[i][2]==1)
			    	System.out.println(arr[i][2]);
					else if(arr[i][2]>=arr[i][0]){
						do{
						if((arr[i][1]+1)%arr[i][2]==0){
							System.out.println((arr[i][1]+1));
							break;
						}
						arr[i][1]+=1;
						}while((arr[i][1])%arr[i][2]!=0);
					}
					else if(arr[i][2]<arr[i][0]){
						do{
						if((arr[i][0]-1)%arr[i][2]==0){
							System.out.println((arr[i][0]-1));
							break;
						}
						arr[i][0]-=1;
						}while((arr[i][0])%arr[i][2]!=0);
					}
				}}
		}
		catch(Exception e){
			return;
		}
	}
}


import java.util.Scanner;
import java.io.IOException;
public class Mint{
	public static void main(String[] args)throws IOException{
		try{
			Scanner sc= new Scanner(System.in);
			int q=sc.nextInt();
			for(int i=0,l,r,d;i<q;i++){
				l=sc.nextInt();
				r=sc.nextInt();
				d=sc.nextInt();
				if(d<l)System.out.println(d);
				else System.out.println((r/d+1)*d);
			}

		}
		catch(Exception e){
			return;
		}
	}
}


import java.util.Scanner;
import java.io.IOException;
public class Mint{
	public static void main(String[] args)throws IOException{
		try{
			Scanner sc= new Scanner(System.in);
			int q=sc.nextInt();
			int arr[][]=new int[q][3];
			int i;
			for(i=0;i<q;i++){
				for(int j=0;j<3;j++){
					arr[i][j]=sc.nextInt();
				}
			}
			for(i=0;i<q;i++){
			    for(int j=0;j<(3);j+=3){
					if(arr[i][2]>=arr[i][0]){
						if((arr[i][1]+1)%arr[i][2]==0){
							System.out.println((arr[i][1]+1));
							break;
						}
						arr[i][1]+=1;
						j-=3;
					}
					else if(arr[i][2]<arr[i][0]){
						if((arr[i][0]-1)%arr[i][2]==0){
							System.out.println((arr[i][0]-1));
							break;
						}
						arr[i][0]-=1;
						j-=3;
					}
				}}
		}
		catch(Exception e){
			return;
		}
	}
}


import java.util.Scanner;
import java.io.IOException;
public class Mint{
	public static void main(String[] args)throws IOException{
		try{
			Scanner sc= new Scanner(System.in);
			int q=sc.nextInt();
			int arr[][]=new int[q][3];
			int i;
			for(i=0;i<q;i++){
				for(int j=0;j<3;j++){
					arr[i][j]=sc.nextInt();
				}
			}
			for(i=0;i<q;i++){
			    for(int j=0;j<(3);j+=3){
			    if(arr[i][0]==1)
			    	System.out.println(arr[i][0]);
					else if(arr[i][2]>=arr[i][0]){
						if((arr[i][1]+1)%arr[i][2]==0){
							System.out.println((arr[i][1]+1));
							break;
						}
						arr[i][1]+=1;
						j-=3;
					}
					else if(arr[i][2]<arr[i][0]){
						if((arr[i][0]-1)%arr[i][2]==0){
							System.out.println((arr[i][0]-1));
							break;
						}
						arr[i][0]-=1;
						j-=3;
					}
				}}
		}
		catch(Exception e){
			return;
		}
	}
}


import java.util.Scanner;
import java.io.IOException;
public class Mint{
	public static void main(String[] args)throws IOException{
		try{
			Scanner sc= new Scanner(System.in);
			int q=sc.nextInt();
			int arr[][]=new int[q][3];
			int i;
			for(i=0;i<q;i++){
				for(int j=0;j<3;j++){
					arr[i][j]=sc.nextInt();
				}
			}
			for(i=0;i<q;i++){
			    for(int j=0;j<(3);j+=3){
			    if(arr[i][2]==1)
			    	System.out.println(arr[i][2]);
					else if(arr[i][2]>=arr[i][0]){
						if((arr[i][1]+1)%arr[i][2]==0){
							System.out.println((arr[i][1]+1));
							break;
						}
						arr[i][1]+=1;
						j-=3;
					}
					else if(arr[i][2]<arr[i][0]){
						if((arr[i][0]-1)%arr[i][2]==0){
							System.out.println((arr[i][0]-1));
							break;
						}
						arr[i][0]-=1;
						j-=3;
					}
				}}
		}
		catch(Exception e){
			return;
		}
	}
}


import java.util.Scanner;
public class code
{
    static int q,l,r,d;
    public static void main (String[] args)
    {
       Scanner ob=new Scanner(System.in);
       q=ob.nextInt();
       while(q>0)
       {
           q--;
            l=ob.nextInt();
            r=ob.nextInt();
            d=ob.nextInt();
            for(int x=d;;x=x+d)
            {
                if(x>=l&&x<=r)
                {
                    x=r-(r%d)+d;
                }
                else
                {
                    if(x%d==0)
                    {
                        System.out.println(x);
                        break;
                    }
                }
            }
       }/* code */
    }
}


import java.util.Scanner;
public class code
{
    static int q,l,r,d;
    public static void main (String[] args)
    {
       Scanner ob=new Scanner(System.in);
       q=ob.nextInt();
       while(q>0)
       {
           q--;
            l=ob.nextInt();
            r=ob.nextInt();
            d=ob.nextInt();
            for(int x=d;;x=x+d)
            {
                if(x>=l&&x<=r)
                {}
                else
                {
                    if(x%d==0)
                    {
                        System.out.println(x);
                        break;
                    }
                }
            }
       }/* code */
    }
}


import java.util.Scanner;
public class code
{
    static int q,l,r,d;
    public static void main (String[] args)
    {
       Scanner ob=new Scanner(System.in);
       q=ob.nextInt();
       while(q>0)
       {
           q--;
            l=ob.nextInt();
            r=ob.nextInt();
            d=ob.nextInt();
            for(int x=d;;x=x+d)
            {
                if(x>=l&&x<=r)
                {

                    x=r-(r%d);

                }
                else
                {
                    if(x%d==0)
                    {
                        System.out.println(x);
                        break;
                    }
                }
            }
       }/* code */
    }
}


import java.util.Scanner;
public class code
{
    static int q,l,r,d;
    public static void main (String[] args)
    {
       Scanner ob=new Scanner(System.in);
       q=ob.nextInt();
       while(q>0)
       {
           q--;
            l=ob.nextInt();
            r=ob.nextInt();
            d=ob.nextInt();
            for(int x=1;;x++)
            {
                if(x>=l&&x<=r)
                {}
                else
                {
                    if(x%d==0)
                    {
                        System.out.println(x);
                        break;
                    }
                }
            }
       }/* code */
    }
}


import java.util.Scanner;

public class CodeForces {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for(int i = 0; i < n; i++){
            int r1 = input.nextInt(), r2 = input.nextInt(), d = input.nextInt();
            System.out.println(MinInt(r1, r2, d));
        }
    }

    private static int MinInt(int r1, int r2, int d){
        for(int i = 0; i < r1; i += d)
            if(i != 0 && i % d == 0)
                return i;
        return (r2 > d ? r2 - r2 % d + d : d);
    }
}


import java.util.Scanner;

public class CodeForces {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for(int i = 0; i < n; i++){
            int r1 = input.nextInt(), r2 = input.nextInt(), d = input.nextInt();
            System.out.println(MinInt(r1, r2, d));
        }
    }

    private static int MinInt(int r1, int r2, int d){
        for(int i = 1; i < r1; i++)
            if(i % d == 0)
                return i;
        int i = r2 + 1;
        while(i % d != 0)
            i++;
        return i;
    }
}


import java.util.Scanner;

public class CodeForces {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for(int i = 0; i < n; i++){
            int r1 = input.nextInt(), r2 = input.nextInt(), d = input.nextInt();
            System.out.println(MinInt(r1, r2, d));
        }
    }

    private static int MinInt(int r1, int r2, int d){
        for(int i = 0; ; i += d)
            if(i != 0 && i % d == 0 && (i < r1 || i > r2))
                return i;
    }
}


import java.util.Scanner;

public class CodeForces {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for(int i = 0; i < n; i++){
            int r1 = input.nextInt(), r2 = input.nextInt(), d = input.nextInt();
            System.out.println(MinInt(r1, r2, d));
        }
    }

    private static int MinInt(int r1, int r2, int d){
        for(int i = 0; i < r1; i += d)
            if(i != 0 && i % d == 0)
                return i;
        return (r2 >= d ? r2 - r2 % d + d : d);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();

        int t = in.nextInt();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < t; i++) {
            long a = in.nextLong(), b = in.nextLong(), n = in.nextLong();

            if (n < a || n > b) {
                sb.append(n).append("\n");
            } else {
                sb.append(a + b).append("\n");
            }
        }

        System.out.print(sb);
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();

        int t = in.nextInt();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < t; i++) {
            long a = in.nextLong(), b = in.nextLong(), n = in.nextLong();

            if (n < a || n > b) {
                sb.append(n).append("\n");
            } else {
                if (((a + b) % n) == 0) {
                    sb.append(a + b).append("\n");
                } else {
                    long cp = n;
                    while (n <= b || (n % cp) != 0) {
                        n *= 2;
                    }
                    sb.append(n).append("\n");
                }
            }
        }

        System.out.print(sb);
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();

        int t = in.nextInt();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < t; i++) {
            long a = in.nextLong(), b = in.nextLong(), n = in.nextLong();

            if (n < a || n > b) {
                sb.append(n).append("\n");
            } else {
                if (((a + b) % n) == 0) {
                    sb.append(a + b).append("\n");
                } else {
                    b += n;
                    while ((b % n) != 0) {
                        b--;
                    }
                    sb.append(b).append("\n");
                }
            }
        }

        System.out.print(sb);
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();

        int t = in.nextInt();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < t; i++) {
            long a = in.nextLong(), b = in.nextLong(), n = in.nextLong();

            if (n < a || n > b) {
                sb.append(n).append("\n");
            } else {
                long mod = (b + n + 1) % n;
                sb.append(b+1+n-mod).append("\n");
            }
        }

        System.out.print(sb);
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();

        int t = in.nextInt();

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < t; i++) {
            long a = in.nextLong(), b = in.nextLong(), n = in.nextLong();

            if (n < a || n > b) {
                sb.append(n).append("\n");
            } else {
                if (((a + b) % n) == 0) {
                    sb.append(a + b).append("\n");
                } else {
                    while (n <= b) {
                        n *= 2;
                    }
                    sb.append(n).append("\n");
                }
            }
        }

        System.out.print(sb);
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();

        int t = in.nextInt();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < t; i++) {
            long a = in.nextLong(), b = in.nextLong(), n = in.nextLong();

            if (n < a || n > b) {
                sb.append(n).append("\n");
            } else {
                long mod = (b + n) % n;
                sb.append(b+n-mod).append("\n");
            }
        }

        System.out.print(sb);
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}


import java.util.Scanner;

public class ProblemA {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		int q = reader.nextInt();
		int [][] tab = new int[q][3];
		int [] result = new int[q];
		for(int i = 0; i< q; i++)
			{for(int j = 0 ; j < 3 ; j++)
				tab[i][j] = reader.nextInt();
			}

		for(int i =0; i< q; i++)
			{if(tab[i][2] > tab[i][1] || tab[i][2] < tab[i][0]) {
				result[i] = tab[i][2];
			}
			else
				{int j = tab[i][1]+1 ;
				while( j% tab[i][2]!=0)
					j++;
					result[i] = j;
				}
			System.out.println(result[i]);
			}
reader.close();
	}

}


import java.util.Scanner;

public class ProblemA {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int q = reader.nextInt();
		int [][] tab = new int[q][3];
		int [] result = new int[q];
		String s = "";
		for(int i = 0; i< q; i++)
			{for(int j = 0 ; j < 3 ; j++)
				tab[i][j] = reader.nextInt();
			if(tab[i][2] > tab[i][1] || tab[i][2] < tab[i][0]) {
				result[i] = tab[i][2];
			}
			else
				{
					result[i] = ((tab[i][1]/tab[i][2])+1)*tab[i][2];
				}
			s += result[i]+ "\n";
			}
		System.out.println(s);
reader.close();
	}

}


import java.util.Scanner;

public class ProblemA {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		int q = reader.nextInt();
		int [][] tab = new int[q][3];
		int [] result = new int[q];
		String s = "";
		for(int i = 0; i< q; i++)
			{for(int j = 0 ; j < 3 ; j++)
				tab[i][j] = reader.nextInt();
			if(tab[i][2] > tab[i][1] || tab[i][2] < tab[i][0]) {
				result[i] = tab[i][2];
			}
			else
				{
				int j = tab[i][2];
				int k = 2;
				while(j*k <= tab[i][1])
					k++;
					result[i] = j*k;
				}
			s += result[i]+ "\n";
			}
		System.out.println(s);
reader.close();
	}

}


import java.util.Scanner;

public class ProblemA {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		int q = reader.nextInt();
		int [][] tab = new int[q][3];
		int [] result = new int[q];
		String s = "";
		for(int i = 0; i< q; i++)
			{for(int j = 0 ; j < 3 ; j++)
				tab[i][j] = reader.nextInt();
			if(tab[i][2] > tab[i][1] || tab[i][2] < tab[i][0]) {
				result[i] = tab[i][2];
			}
			else
				{
				int j = tab[i][1]+1 ;
				while( j% tab[i][2]!=0)
					j++;
					result[i] = j;
				}
			s += result[i]+ "\n";
			}
		System.out.println(s);
reader.close();
	}

}


import java.util.Scanner;

public class Main{
    public static void main(String[] args) {

        Scanner ss = new Scanner(System.in);
        int q,l,r,d,s;
        q=ss.nextInt();
        for (int i = 0; i < q; i++) {
              l=ss.nextInt();
              r=ss.nextInt();
              d=ss.nextInt();

              if(d>r || d<l)
              {
                  System.out.println(d);
              }
              else if(r%d==0)
              {
                  System.out.println(r+d);
              }

              else{
                 int g=r/d;
                  System.out.println(d*g+d);

              }
        }
        System.exit(0);
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner ss = new Scanner(System.in);
        int q,l,r,d,s;
        q=ss.nextInt();
        for (int i = 0; i < q; i++) {
              l=ss.nextInt();
              r=ss.nextInt();
              d=ss.nextInt();

              if(d>r || d<l)
              {
                  System.out.println(d);
              }
              else{
                  s=2;
                  while(true){
                     int m= d*s;
                      if(m>r)
                      {
                          System.out.println(m);
                          break;
                      }
                      else{
                          s++;
                      }
                  }
              }

        }
    }
}


package Codeforce;

import java.util.Scanner;

public class Edu {
    public static void main(String[] args) {

        Scanner ss = new Scanner(System.in);
        int q,l,r,d,s;
        q=ss.nextInt();
        for (int i = 0; i < q; i++) {
              l=ss.nextInt();
              r=ss.nextInt();
              d=ss.nextInt();

              if(d>r || d<l)
              {
                  System.out.println(d);
              }
              else{
                  s=2;
                  while(true){
                     int m= d*s;
                      if(m>r || m<l)
                      {
                          System.out.println(m);
                          break;
                      }
                      else{
                          s++;
                      }
                  }
              }
        }
    }
}


import java.util.Scanner;

public class Main{
    public static void main(String[] args) {

        Scanner ss = new Scanner(System.in);
        int q,l,r,d,s;
        q=ss.nextInt();
        for (int i = 0; i < q; i++) {
              l=ss.nextInt();
              r=ss.nextInt();
              d=ss.nextInt();

              if(d>r || d<l)
              {
                  System.out.println(d);
              }

              else{
                  s=2;
                  while(true){
                     int m= d*s;
                      if(m>r || m<l)
                      {
                          System.out.println(m);

                          break;
                      }
                      else{
                          s++;
                      }
                  }
              }
        }
        System.exit(0);
    }
}


import java.util.Scanner;

public class Codeforces1101A {
    public static final Scanner SCANNER = new Scanner(System.in);
    public static void main(String[]args){
        int number = SCANNER.nextInt();
        int [] array = new int[number];
        int [] array2 = new int[3];
        for(int j = 0;j<array.length;j++){
            for(int i = 0;i<array2.length;i++){
                array2[i]= SCANNER.nextInt();
                if(array2[2]<=array2[0]){
                    array[j] = array2[2];
                }
                else{
                    array[j] = array2[0]*2+2;
                }
            }
        }
        for(int i = 0;i<array.length;i++){
            System.out.println(array[i]);
        }
    }
}


import java.util.Scanner;

public class Codeforces1101A {
    public static final Scanner SCANNER = new Scanner(System.in);
    public static void main(String[]args){
        int number = SCANNER.nextInt();
        int [] array = new int[number];
        int [] array2 = new int[3];
        for(int j = 0;j<array.length;j++){
            for(int i = 0;i<array.length;i++){
                array2[i]= SCANNER.nextInt();
                if(array2[2]<=array2[0]){
                    array[j] = array2[2];
                }
                else{
                    array[j] = array2[0]*2+2;
                }
            }
        }
        for(int i = 0;i<array.length;i++){
            System.out.println(array[i]);
        }
    }
}


import java.util.Scanner;

public class Codeforces1101A {
    public static final Scanner SCANNER = new Scanner(System.in);
    public static void main(String[]args){
        int number = SCANNER.nextInt();
        int [] array = new int[number];
        int [] array2 = new int[3];
        for(int j = 0;j<array.length;j++){
            for(int i = 0;i<array2.length;i++){
                array2[i]= SCANNER.nextInt();
                if(array2[2]<array2[0]){
                    array[j] = array2[2];
                }
                else{
                    array[j] = array2[0]*2+2;
                }
            }
        }
        for(int i = 0;i<array.length;i++){
            System.out.println(array[i]);
        }
    }
}


import java.util.Scanner;

public class Codeforces1101A {
    public static final Scanner SCANNER = new Scanner(System.in);
    public static void main(String[]args){
        int number = SCANNER.nextInt();
        int [] array = new int[number];
        int [] array2 = new int[3];
        for(int j = 0;j<array.length;j++){
            for(int i = 0;i<array2.length;i++){
                array2[i]= SCANNER.nextInt();
                if(array2[2]<array2[0]){
                    array[j] = array2[2];
                }
                else{
                    array[j] = (array2[1]/array2[2]+1)*array2[2];
                }
            }
        }
        for(int i = 0;i<array.length;i++){
            System.out.println(array[i]);
        }
    }
}


import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    while(n-->0)
    {
      int l = s.nextInt();
      int r = s.nextInt();
      int d = s.nextInt();
      for(int i=1;i>0;i++)
      {
        if(i*d<l||i*d>r)
        {
          System.out.println(i*d);
          break;
        }
      }
    }
  }
}


import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    while(n-->0)
    {
      int l = s.nextInt();
      int r = s.nextInt();
      int d = s.nextInt();
      if(c<l){
          System.out.println(d);
      }
      else{
          System.out.println(d*((r/d)+1));
      }
    }
  }
}


import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    while(n-->0)
    {
      int l = s.nextInt();
      int r = s.nextInt();
      int d = s.nextInt();
      if(c<l){
          System.out.println(c);
      }
      else{
          System.out.println(c*((r/c)+1))
      }
    }
  }
}