#include <bits/stdc++.h>
#define lli long long int
#define ii pair<lli,lli>
#define fi first
#define se second
using namespace std;
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t; cin>>t;
while (t--)
{
int n,k; cin>>n>>k;
lli a[n+1];
for (int i=1;i<=n;i++) cin>>a[i];
if (k!=4)
{
lli mn=1e18;
for (int i=1;i<=n;i++)
if (a[i]%k==0) mn=0; else mn=min(mn,k-a[i]%k);
cout<<mn<<"\n";
}
else
{
bool flag=false; lli kq=1e18;
for (int i=1;i<=n;i++)
if (a[i]%4==0) kq=0;
if (kq)
{
int dem=0;
for (int i=1;i<=n;i++)
if (a[i]%2==0) dem++;
if (dem>=2) cout<<"0\n";
else if (dem==1 && n==1) cout<<"2\n";
else if (dem==1) cout<<"1\n";
else
{
bool ok=false;
for (int i=1;i<=n;i++)
if (a[i]%4==3) ok=true;
if (ok) cout<<"1\n";
else if (!ok && n==1) cout<<"3\n";
else cout<<"2\n";
}
}
else cout<<"0\n";
}
}
}
368. Largest Divisible Subset | 377. Combination Sum IV |
322. Coin Change | 307. Range Sum Query - Mutable |
287. Find the Duplicate Number | 279. Perfect Squares |
275. H-Index II | 274. H-Index |
260. Single Number III | 240. Search a 2D Matrix II |
238. Product of Array Except Self | 229. Majority Element II |
222. Count Complete Tree Nodes | 215. Kth Largest Element in an Array |
198. House Robber | 153. Find Minimum in Rotated Sorted Array |
150. Evaluate Reverse Polish Notation | 144. Binary Tree Preorder Traversal |
137. Single Number II | 130. Surrounded Regions |
129. Sum Root to Leaf Numbers | 120. Triangle |
102. Binary Tree Level Order Traversal | 96. Unique Binary Search Trees |
75. Sort Colors | 74. Search a 2D Matrix |
71. Simplify Path | 62. Unique Paths |
50. Pow(x, n) | 43. Multiply Strings |