B. Fair Numbers

time limit per test

2 seconds

memory limit per test

256 megabytes

We call a positive integer number fair if it is divisible by each of its nonzero digits. For example, 102 is fair (because it is divisible by 1 and 2), but 282 is not, because it isn't divisible by 8. Given a positive integer n. Find the minimum integer x, such that n≤x and x is fair.

Input

The first line contains number of test cases t (1≤t≤103). Each of the next t lines contains an integer n (1≤n≤1018).

Output

For each of t test cases print a single integer --- the least fair number, which is not less than n.

Example

Input

Copy

复制代码
4
1
282
1234567890
1000000000000000000

Output

Copy

复制代码
1
288
1234568040
1000000000000000000

Note

Explanations for some test cases:

  • In the first test case number 1 is fair itself.
  • In the second test case number 288 is fair (it's divisible by both 2 and 8). None of the numbers from 282,287 is fair, because, for example, none of them is divisible by 8.

解题说明:此题是一道数学题,直接从 n开始逐个递增检查,直到找到第一个满足条件的数即可。针对每个数字,判断其中的每一位,如果为0就跳过,否则进行整除判断。

cpp 复制代码
#include<stdio.h>
int main()
{
	int t;
	long long int a, k;
	scanf("%d", &t);
	while (t) 
	{
		scanf("%lld", &a);
		k = a;
		while (k != 0)
		{
			if (k % 10 == 0)
			{
				k = k / 10;
			}
			else
			{
				if (a % (k % 10) == 0)
				{
					k = k / 10;
				}
				else 
				{
					a++; 
					k = a;
				}
			}
		}
		printf("%lld\n", a);
		t--;
	}
	return 0;
}
相关推荐
lingchen19061 分钟前
用MATLAB r2010b和r2016b求解微分方程组
开发语言·算法·matlab
无定义_39 分钟前
Dijkstra——单源最短路径
算法·图论
weixin_307779131 小时前
AI生成代码的隐患与治理:人工审查流程及提示词驱动的修复策略
开发语言·人工智能·算法
zmzb01031 小时前
C++课后习题训练记录Day201
c++·算法·图论
lch2011_yb1 小时前
CSP-J 2023 小苹果
算法
五_谷_丰_登1 小时前
平衡二叉搜索树讲解
数据结构·c++
星星.7221 小时前
【图论】最小生成树|Prim+Kruskal算法
数据结构·c++·算法·图论
_Narcissus_1 小时前
单调栈笔记及例题详解
数据结构·c++·笔记·算法·力扣·单调栈·洛谷
额,不知道写啥。2 小时前
从区间加一次函数到区间加多次函数最值-----区间数据结构与函数的一些东西(《浅谈函数最值的动态维护》的学习笔记)
数据结构·笔记·学习