///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long  double   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
//string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
//LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}

void make(){

}

void solve(){
    //make();
    long long int a,n,b;
    cin>>n>>a;
    set<LL>st;
    long long  f=0;
    map<LL,LL>ar;
   // VI v;
    rep(i,n){
        cin>>b;
        //long long int kk=long long (b)% long long (a);
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<LL>::iterator it;
    LL val=0;
    LL ans=0;
    LL pre=0;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      //F(vv);
      st.erase(vv);
      ans+=vv-pre;
      pre=vv;

      ar[*it]--;
      if(ar[*it]>0){
        LL t=ar[vv];
        LL tt=LL(vv+a);
        ar[tt]+=t;
       // FF(vv,ar[vv]);
        st.insert(tt);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    cout<<ans<<"\n";
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long long int   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}
LL f(LL A, LL B)
{
    int count = 0;
    for (LL i = 0; i < 32; i++)
    {
        if (((A >> i) & 1) != ((B >> i) & 1))
        {
            count++;
        }
    }
    return count;
}

void make(){

}

void solve(){
    //make();
    cin>>n>>a;
    set<long double>st;
    LL f=0;
    map<long double,long double>ar;
    VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<long double>::iterator it;
    long double val=0LL;
    long double ans=0LL;
    long double pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      //F(vv);
      st.erase(vv);
      ans+=vv-pre;
      pre=vv;

      ar[*it]--;
      if(ar[*it]>0){
        LL t=ar[vv];
        ar[vv+a]+=t;
       // FF(vv,ar[vv]);
        st.insert(vv+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    long long ans2=ans;
    cout<<ans2<<endl;
    //cout<<long long(ans)<<"\n";
      // printf("%.0Lf\n",ans);
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long long int   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}
LL f(LL A, LL B)
{
    int count = 0;
    for (LL i = 0; i < 32; i++)
    {
        if (((A >> i) & 1) != ((B >> i) & 1))
        {
            count++;
        }
    }
    return count;
}

void make(){

}

void solve(){
    //make();
    cin>>n>>a;
    set<long double>st;
    LL f=0;
    map<long double,long double>ar;
    VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<long double>::iterator it;
    LL val=0LL;
    LL ans=0LL;
    LL pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      //F(vv);
      st.erase(vv);
      ans+=vv-pre;
      pre=vv;

      ar[*it]--;
      if(ar[*it]>0){
        LL t=ar[vv];
        ar[vv+a]+=t;
       // FF(vv,ar[vv]);
        st.insert(vv+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    cout<<ans<<"\n";
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long long int   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}
LL f(LL A, LL B)
{
    int count = 0;
    for (LL i = 0; i < 32; i++)
    {
        if (((A >> i) & 1) != ((B >> i) & 1))
        {
            count++;
        }
    }
    return count;
}

    map<long double,long double>ar;

void make(){

}

void solve(){
    //make();
    cin>>n>>a;
    set<long double>st;
    st.clear();
    LL f=0;
    ar.clear();
    VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<long double>::iterator it;
    long double val=0LL;
    long double ans=0LL;
    long double pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      ans=vv;
      //F(vv);
      if(st.count(vv))
      st.erase(vv);
      else break;
     // ans+=vv-pre;
      pre=vv;

      ar[vv]--;
      if(ar[vv]>0){
        LL t=ar[vv];
        ar[vv+a]+=t;
       // FF(vv,ar[vv]);
        st.insert(vv+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    //long long ans2=ans;
    cout<<fixed<<setprecision(0)<<ans<<endl;
    //cout<<long long(ans)<<"\n";
      // printf("%Lf\n",ans);
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long long int   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}
LL f(LL A, LL B)
{
    int count = 0;
    for (LL i = 0; i < 32; i++)
    {
        if (((A >> i) & 1) != ((B >> i) & 1))
        {
            count++;
        }
    }
    return count;
}

    map<long double,long double>ar;

void make(){

}

void solve(){
    //make();
    cin>>n>>a;
    set<long double>st;
    st.clear();
    LL f=0;
    ar.clear();
    VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<long double>::iterator it;
    long double val=0LL;
    long double ans=0LL;
    long double pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      ans=vv;
      //F(vv);
      if(st.count(vv))
      st.erase(vv);
      else break;
     // ans+=vv-pre;
      pre=vv;

      ar[vv]--;
      if(ar[vv]>0){
        LL t=ar[vv];
        ar[vv+a]+=t;
       // FF(vv,ar[vv]);
        st.insert(vv+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    //long long ans2=ans;
    cout<<ans<<endl;
    //cout<<long long(ans)<<"\n";
      // printf("%.0Lf\n",ans);
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long long int   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}
LL f(LL A, LL B)
{
    int count = 0;
    for (LL i = 0; i < 32; i++)
    {
        if (((A >> i) & 1) != ((B >> i) & 1))
        {
            count++;
        }
    }
    return count;
}

void make(){

}

void solve(){
    //make();
    cin>>n>>a;
    set<LL>st;
    LL f=0;
    map<LL,LL>ar;
    VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<LL>::iterator it;
    LL val=0LL;
    LL ans=0LL;
    LL pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
     // F(*it);
      st.erase(*it);
      ans+=*it-pre;
      pre=*it;
      ar[*it]--;
      if(ar[*it]>0){
        LL t=ar[*it];
        ar[*it+a]+=t;
        st.insert(*it+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    cout<<ans<<"\n";
    //F(ans);

}
int main()
{
    IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long  double   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
//string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
//LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}

void make(){

}

void solve(){
    //make();
    long long int a,n,b;
    cin>>n>>a;
    set<LL>st;
    long long  f=0;
    map<LL,LL>ar;
   // VI v;
    rep(i,n){
        cin>>b;
        //long long int kk=long long (b)% long long (a);
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<LL>::iterator it;
    LL val=0LL;
    LL ans=0LL;
    LL pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=LL(*it);
      //F(vv);
      st.erase(vv);
      ans+=vv-pre;
      pre=vv;

      ar[*it]--;
      if(ar[*it]>0){
        LL t=ar[vv];
        LL tt=LL(vv+a);
        ar[tt]+=t;
       // FF(vv,ar[vv]);
        st.insert(tt);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
   // printf("%lld\n",ans);
    cout<<ans<<"\n";
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long long int   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}
LL f(LL A, LL B)
{
    int count = 0;
    for (LL i = 0; i < 32; i++)
    {
        if (((A >> i) & 1) != ((B >> i) & 1))
        {
            count++;
        }
    }
    return count;
}

    map<long double,long double>ar;

void make(){

}

void solve(){
    //make();
    cin>>n>>a;
    set<long double>st;
    st.clear();
    LL f=0;
    ar.clear();
    VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<long double>::iterator it;
    long double val=0LL;
    long double ans=0LL;
    long double pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      //F(vv);
      if(st.count(vv))
      st.erase(vv);
      else break;
      ans+=vv-pre;
      pre=vv;

      ar[*it]--;
      if(ar[*it]>0){
        LL t=ar[vv];
        ar[vv+a]+=t;
       // FF(vv,ar[vv]);
        st.insert(vv+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    long long ans2=ans;
    cout<<ans2<<endl;
    //cout<<long long(ans)<<"\n";
      // printf("%.0Lf\n",ans);
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long long int   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}
LL f(LL A, LL B)
{
    int count = 0;
    for (LL i = 0; i < 32; i++)
    {
        if (((A >> i) & 1) != ((B >> i) & 1))
        {
            count++;
        }
    }
    return count;
}

void make(){

}

void solve(){
    //make();
    cin>>n>>a;
    set<long double>st;
    LL f=0;
    map<long double,long double>ar;
    VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<long double>::iterator it;
    long double val=0LL;
    long double ans=0LL;
    long double pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      //F(vv);
      st.erase(vv);
      ans+=vv-pre;
      pre=vv;

      ar[*it]--;
      if(ar[*it]>0){
        LL t=ar[vv];
        ar[vv+a]+=t;
       // FF(vv,ar[vv]);
        st.insert(vv+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    long long ans2=ans;
    //cout<<long long(ans)<<"\n";
      // printf("%.0Lf\n",ans);
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long long int   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}
LL f(LL A, LL B)
{
    int count = 0;
    for (LL i = 0; i < 32; i++)
    {
        if (((A >> i) & 1) != ((B >> i) & 1))
        {
            count++;
        }
    }
    return count;
}

    map<long double,long double>ar;

void make(){

}

void solve(){
    //make();
    cin>>n>>a;
    set<long double>st;
    st.clear();
    LL f=0;
    ar.clear();
    VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<long double>::iterator it;
    long double val=0LL;
    long double ans=0LL;
    long double pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      ans=vv;
      //F(vv);
      if(st.count(vv))
      st.erase(vv);
      else break;
     // ans+=vv-pre;
      pre=vv;

      ar[vv]--;
      if(ar[vv]>0){
        LL t=ar[vv];
        ar[vv+a]+=t;
       // FF(vv,ar[vv]);
        st.insert(vv+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    long long ans2=ans;
    cout<<ans2<<endl;
    //cout<<long long(ans)<<"\n";
      // printf("%.0Lf\n",ans);
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long  double   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
//string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
//LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}

void make(){

}

void solve(){
    //make();
    long long int a,n,b;
    cin>>n>>a;
    set<long double>st;
    long long int f=0;
    map<long double,long double>ar;
   // VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<long double>::iterator it;
    LL val=0;
    LL ans=0;
    LL pre=0;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      //F(vv);
      st.erase(vv);
      ans+=vv-pre;
      pre=vv;

      ar[*it]--;
      if(ar[*it]>0){
        LL t=ar[vv];
        LL tt=LL(vv+a);
        ar[tt]+=t;
       // FF(vv,ar[vv]);
        st.insert(vv+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    cout<<ans<<"\n";
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


///Al-Helal
using namespace std;
#include<bits/stdc++.h>
typedef long long int   LL;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<LL,LL>  MII;

///LOOP

#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
#define reps(i,s) for(int i=0;i<s.size();i++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define EPS (1e-9)
///DEBUG
#define trace(x) cerr<<x<<endl;
#define F(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;
#define FF(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define FFF(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" = "<<z<<endl;
/**Define Bitwise operation**/
#define check(n, pos) (n & (1<<(pos)))
#define biton(n, pos) (n | (1<<(pos)))
#define bitoff(n, pos) (n & ~(1<<(pos)))
#define ledzero(n) (__builtin_clz(n))
#define trailzero(n) (__builtin_ctz(n))
#define onbit(n) (__builtin_popcount(n))
///upper bound and lower bound
#define LB(a,value) (lower_bound(all(a),value)-a.begin())
#define UB(a,value) (upper_bound(all(a),value)-a.begin())
#define PI acos(-1.0)
#define isEq(a,b) (fabs((a)-(b))<eps)
#define spDist(lat1,long1,lat2,long2,r) ((r)*acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos((long1)-(long2))))
#define area(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
#define distSqr(x1,y1,x2,y2) ( sqr(x1-x2) + sqr(y1-y2) )
#define dist4adj(x1,y1,x2,y2) ( abs(x1-x2) + abs(y1-y2) )
#define dist8adj(x1,y1,x2,y2) max( abs(x1-x2) , abs(y1-y2) )
///File
#define READ freopen("INPUT.txt","r",stdin)
#define WRITE freopen("OUTPUT.txt","w",stdout)
#define IOS ios_base::sync_with_stdio(false), cin.tie(false)
///NUMBER THEORY
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
///string to int
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// GEOMETRY
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
//template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
//template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
///Direction Array
///int fx[]={1,-1,0,0}; int fy[]={0,0,1,-1};
/// int fx[]={0,0,1,-1,-1,1,-1,1}; int fy[]={-1,1,0,0,1,1,-1,-1};
///struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
///=======================================CODING=================================================///
const  long long int N=5e5+5,MOD=1e9+7,inf=1e18*1LL;
//LL ar2[N],ar[N],vis[N];

LL a,b,c,r,rr,n,m,t=0,ks;
//string s,ss;

void nl(){
    cout<<"\n";
}
LL f(LL A, LL B)
{
    int count = 0;
    for (LL i = 0; i < 32; i++)
    {
        if (((A >> i) & 1) != ((B >> i) & 1))
        {
            count++;
        }
    }
    return count;
}

    map<long double,long double>ar;

void make(){

}

void solve(){
    //make();
    cin>>n>>a;
    set<long double>st;
    st.clear();
    LL f=0;
    ar.clear();
    VI v;
    rep(i,n){
        cin>>b;
        f=a-(b%a);
       // F(f);
        if(f==0 || f==a)  continue;
        ar[f]++;
       st.insert(f);
       //v.pb(f);
    }
   // sort(all(v));
    //reps(i,v)F(v[i]);
    set<long double>::iterator it;
    long double val=0LL;
    long double ans=0LL;
    long double pre=0LL;
    while(st.size()>0){
     //it=st.fi
      it=st.begin();
      LL vv=*it;
      //F(vv);
      if(st.count(vv))
      st.erase(vv);
      else break;
      ans+=vv-pre;
      pre=vv;

      ar[vv]--;
      if(ar[vv]>0){
        LL t=ar[vv];
        ar[vv+a]+=t;
       // FF(vv,ar[vv]);
        st.insert(vv+a);
      }
     // st.erase(*it);
    }

    if(ans)  ans++;
    long long ans2=ans;
    cout<<ans2<<endl;
    //cout<<long long(ans)<<"\n";
      // printf("%.0Lf\n",ans);
    //F(ans);

}
int main()
{
    //IOS;
    //make();
    t=1;
    if(t==1)
    {
        int t;cin>>t;while(t--)solve();
    }
    else solve();
    return 0;
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TL {

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        try{
            String [] num = br.readLine().split("\\s+");
            String [] corr = br.readLine().split("\\s+");
            String [] wron = br.readLine().split("\\s+");

            int [] a = new int [corr.length];
            for (int i=0;i<corr.length;i++)
                a[i] = Integer.parseInt(corr[i]);

            Arrays.sort(a);
            int max = a[a.length-1];
            boolean flag1 = false;
            boolean flag2 = false;
            if (Integer.parseInt(num[0]) == 1){
                int v = Integer.parseInt(corr[0])*2;

                for (int i=0;i<wron.length;i++)
                    if (Integer.parseInt(wron[i]) > v)
                        flag2 = true;
                    else
                    {
                        flag2 = false;
                        break;
                    }

                if (flag2)
                    System.out.println(v);
                else
                    System.out.println(-1);
            }
            else{
                if (max == 1)
                    max =2;
            for (int i=0;i<a.length-2;i++)
                if (a[i]*2 <= max)
                    flag1=true;

            for (int i=0;i<wron.length;i++)
                if (Integer.parseInt(wron[i]) > max)
                    flag2 = true;
                else
                {
                    flag2 = false;
                    break;
                }

            if (flag1 && flag2)
                System.out.println(max);
            else
                System.out.println(-1);

        }}
        catch (Exception e){

        }

    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TL {

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        try{
            String [] num = br.readLine().split("\\s+");
            String [] corr = br.readLine().split("\\s+");
            String [] wron = br.readLine().split("\\s+");

            int [] a = new int [corr.length];
            int [] b = new int [wron.length];

            for (int i=0;i<corr.length;i++)
                a[i] = Integer.parseInt(corr[i]);

            for (int i=0;i<wron.length;i++)
                b[i] = Integer.parseInt(wron[i]);

            Arrays.sort(a);
            Arrays.sort(b);

            int minA = a[0];
            int maxA = a[a.length-1];
            int minB = b[0];

            int max = (int)(Math.max(minA, maxA));
            if (max < minB)
                System.out.println(max);
            else
                System.out.println(-1);
            }
        catch (Exception e){

        }

    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TL {

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        try{
            String [] num = br.readLine().split("\\s+");
            String [] corr = br.readLine().split("\\s+");
            String [] wron = br.readLine().split("\\s+");

            Arrays.sort(corr);
            int max = Integer.parseInt(corr[corr.length-1]);
            boolean flag1 = false;
            boolean flag2 = false;
            if (Integer.parseInt(num[0]) == 1){
                int v = Integer.parseInt(corr[0])*2;

                for (int i=0;i<wron.length;i++)
                    if (Integer.parseInt(wron[i]) > v)
                        flag2 = true;
                    else
                    {
                        flag2 = false;
                        break;
                    }

                if (flag2)
                    System.out.println(v);
                else
                    System.out.println(-1);
            }
            else{
            for (int i=0;i<corr.length-2;i++)
                if (Integer.parseInt(corr[i])*2 <= max)
                    flag1=true;

            for (int i=0;i<wron.length;i++)
                if (Integer.parseInt(wron[i]) > max)
                    flag2 = true;
                else
                {
                    flag2 = false;
                    break;
                }

            if (flag1 && flag2)
                System.out.println(max);
            else
                System.out.println(-1);

        }}
        catch (Exception e){

        }

    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TL {

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        try{
            String [] num = br.readLine().split("\\s+");
            String [] corr = br.readLine().split("\\s+");
            String [] wron = br.readLine().split("\\s+");

            int [] a = new int [corr.length];
            for (int i=0;i<corr.length;i++)
                a[i] = Integer.parseInt(corr[i]);

            Arrays.sort(a);
            int max = a[a.length-1];
            boolean flag1 = false;
            boolean flag2 = false;
            if (Integer.parseInt(num[0]) == 1){
                int v = Integer.parseInt(corr[0])*2;

                for (int i=0;i<wron.length;i++)
                    if (Integer.parseInt(wron[i]) > v)
                        flag2 = true;
                    else
                    {
                        flag2 = false;
                        break;
                    }

                if (flag2)
                    System.out.println(v);
                else
                    System.out.println(-1);
            }
            else{
            for (int i=0;i<a.length-2;i++)
                if (a[i]*2 <= max)
                    flag1=true;

            for (int i=0;i<wron.length;i++)
                if (Integer.parseInt(wron[i]) > max)
                    flag2 = true;
                else
                {
                    flag2 = false;
                    break;
                }

            if (flag1 && flag2)
                System.out.println(max);
            else
                System.out.println(-1);

        }}
        catch (Exception e){

        }

    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TL {

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        try{
            String [] num = br.readLine().split("\\s+");
            String [] corr = br.readLine().split("\\s+");
            String [] wron = br.readLine().split("\\s+");

            int [] a = new int [corr.length];
            int [] b = new int [wron.length];

            for (int i=0;i<corr.length;i++)
                a[i] = Integer.parseInt(corr[i]);

            for (int i=0;i<wron.length;i++)
                b[i] = Integer.parseInt(wron[i]);

            Arrays.sort(a);
            Arrays.sort(b);

            int minA = a[0];
            int maxA = a[a.length-1];
            int minB = b[0];

            int max = (int)(Math.max(minA*2, maxA));
            if (max < minB)
                System.out.println(max);
            else
                System.out.println(-1);
            }
        catch (Exception e){

        }

    }

}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.TreeSet;

import java.math.BigInteger;

public class Main {

	public static Reader in = new Reader();
	public static Writer out = new Writer();

	public static void main(String[] args) {
		int n = in.nextInt();
		int m = in.nextInt();
		out.print(getMin(n, m, in.readInts(n), in.readInts(m)));
		out.close();
	}

	static int getMin(int n, int m, int[] a, int[] b) {
		Arrays.sort(a);
		int v = a[0]*2;
		for(int i=1; i<n; i++) {
			v = Math.max(a[i], v);
		}
		for(int i=0; i<m; i++) {
			if(v==b[i] || v>=2*b[i])
				return -1;
		}
		return v;
	}

}

class Writer {

	private BufferedWriter output;

	public Writer() {
		output = new BufferedWriter(new OutputStreamWriter(System.out));
	}

	public Writer(String s) {
		try {
			output = new BufferedWriter(new FileWriter(s));
		} catch(Exception ex) { ex.printStackTrace(); System.exit(0);}
	}

	public void println() {
		try {
			output.append("\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void print(Object o) {
		try {
			output.append(o.toString());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void println(Object o) {
		try {
			output.append(o.toString()+"\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void printf(String format, Object... args) {
		try {
			output.append(String.format(format, args));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void printfln(String format, Object... args) {
		try {
			output.append(String.format(format, args)+"\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void flush() {
		try {
			output.flush();
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void close() {
		try {
			output.close();
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
}

class Reader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");

	public Reader() {
		input = new BufferedReader(new InputStreamReader(System.in));
	}

	public Reader(String s) {
		try {
			input = new BufferedReader(new FileReader(s));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public double nextDouble() {
		fill();
		return Double.parseDouble(line.nextToken());
	}

	public String nextWord() {
		fill();
		return line.nextToken();
	}

	public int nextInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}

	public long nextLong() {
		fill();
		return Long.parseLong(line.nextToken());
	}

	public double readDouble() {
		double d = 0;
		try {
			d = Double.parseDouble(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return d;
	}

	public int readInt() {
		int i = 0;
		try {
			i = Integer.parseInt(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return i;
	}

	public int[] readInts(int n) {
		int[] a = new int[n];
		for(int i=0; i<n; i++)
			a[i] = nextInt();
		return a;
	}

	public void fillInts(int[] a) {
		for(int i=0; i<a.length; i++)
			a[i] = nextInt();
	}

	public long readLong() {
		long l = 0;
		try {
			l = Long.parseLong(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return l;
	}

	public String readLine() {
		String s = "";
		try {
			s = input.readLine();
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return s;
	}
}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.TreeSet;

import java.math.BigInteger;

public class Main {

	public static Reader in = new Reader();
	public static Writer out = new Writer();

	public static void main(String[] args) {
		int n = in.nextInt();
		int m = in.nextInt();
		out.print(getMin(n, m, in.readInts(n), in.readInts(m)));
		out.close();
	}

	static int getMin(int n, int m, int[] a, int[] b) {
		Arrays.sort(a);
		int v = a[0]*2;
		for(int i=1; i<n; i++) {
			v = Math.max(a[i], v);
		}
		for(int i=0; i<m; i++) {
			if(v>=b[i])
				return -1;
		}
		return v;
	}

}

class Writer {

	private BufferedWriter output;

	public Writer() {
		output = new BufferedWriter(new OutputStreamWriter(System.out));
	}

	public Writer(String s) {
		try {
			output = new BufferedWriter(new FileWriter(s));
		} catch(Exception ex) { ex.printStackTrace(); System.exit(0);}
	}

	public void println() {
		try {
			output.append("\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void print(Object o) {
		try {
			output.append(o.toString());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void println(Object o) {
		try {
			output.append(o.toString()+"\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void printf(String format, Object... args) {
		try {
			output.append(String.format(format, args));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void printfln(String format, Object... args) {
		try {
			output.append(String.format(format, args)+"\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void flush() {
		try {
			output.flush();
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void close() {
		try {
			output.close();
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
}

class Reader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");

	public Reader() {
		input = new BufferedReader(new InputStreamReader(System.in));
	}

	public Reader(String s) {
		try {
			input = new BufferedReader(new FileReader(s));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public double nextDouble() {
		fill();
		return Double.parseDouble(line.nextToken());
	}

	public String nextWord() {
		fill();
		return line.nextToken();
	}

	public int nextInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}

	public long nextLong() {
		fill();
		return Long.parseLong(line.nextToken());
	}

	public double readDouble() {
		double d = 0;
		try {
			d = Double.parseDouble(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return d;
	}

	public int readInt() {
		int i = 0;
		try {
			i = Integer.parseInt(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return i;
	}

	public int[] readInts(int n) {
		int[] a = new int[n];
		for(int i=0; i<n; i++)
			a[i] = nextInt();
		return a;
	}

	public void fillInts(int[] a) {
		for(int i=0; i<a.length; i++)
			a[i] = nextInt();
	}

	public long readLong() {
		long l = 0;
		try {
			l = Long.parseLong(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return l;
	}

	public String readLine() {
		String s = "";
		try {
			s = input.readLine();
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return s;
	}
}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.TreeSet;

import java.math.BigInteger;

public class Main {

	public static Reader in = new Reader();
	public static Writer out = new Writer();

	public static void main(String[] args) {
		int n = in.nextInt();
		int m = in.nextInt();
		out.print(getMin(n, m, in.readInts(n), in.readInts(m)));
		out.close();
	}

	static int getMin(int n, int m, int[] a, int[] b) {
		Arrays.sort(a);
		int v = a[0]*2;
		for(int i=1; i<n; i++) {
			v = Math.max(a[i], v);
		}
		for(int i=0; i<m; i++) {
			if(v==b[i]) {
				v++;
			}
			if(v>=2*b[i]) {
				return -1;
			}
		}
		return v;
	}

}

class Writer {

	private BufferedWriter output;

	public Writer() {
		output = new BufferedWriter(new OutputStreamWriter(System.out));
	}

	public Writer(String s) {
		try {
			output = new BufferedWriter(new FileWriter(s));
		} catch(Exception ex) { ex.printStackTrace(); System.exit(0);}
	}

	public void println() {
		try {
			output.append("\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void print(Object o) {
		try {
			output.append(o.toString());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void println(Object o) {
		try {
			output.append(o.toString()+"\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void printf(String format, Object... args) {
		try {
			output.append(String.format(format, args));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void printfln(String format, Object... args) {
		try {
			output.append(String.format(format, args)+"\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void flush() {
		try {
			output.flush();
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void close() {
		try {
			output.close();
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
}

class Reader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");

	public Reader() {
		input = new BufferedReader(new InputStreamReader(System.in));
	}

	public Reader(String s) {
		try {
			input = new BufferedReader(new FileReader(s));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public double nextDouble() {
		fill();
		return Double.parseDouble(line.nextToken());
	}

	public String nextWord() {
		fill();
		return line.nextToken();
	}

	public int nextInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}

	public long nextLong() {
		fill();
		return Long.parseLong(line.nextToken());
	}

	public double readDouble() {
		double d = 0;
		try {
			d = Double.parseDouble(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return d;
	}

	public int readInt() {
		int i = 0;
		try {
			i = Integer.parseInt(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return i;
	}

	public int[] readInts(int n) {
		int[] a = new int[n];
		for(int i=0; i<n; i++)
			a[i] = nextInt();
		return a;
	}

	public void fillInts(int[] a) {
		for(int i=0; i<a.length; i++)
			a[i] = nextInt();
	}

	public long readLong() {
		long l = 0;
		try {
			l = Long.parseLong(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return l;
	}

	public String readLine() {
		String s = "";
		try {
			s = input.readLine();
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return s;
	}
}


import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.TreeSet;

import java.math.BigInteger;

public class Main {

	public static Reader in = new Reader();
	public static Writer out = new Writer();

	public static void main(String[] args) {
		int n = in.nextInt();
		int m = in.nextInt();
		out.print(getMin(n, m, in.readInts(n), in.readInts(m)));
		out.close();
	}

	static int getMin(int n, int m, int[] a, int[] b) {
		Arrays.sort(a);
		int v = a[0]*2;
		for(int i=1; i<n; i++) {
			v = Math.max(a[i], v);
		}
		for(int i=0; i<m; i++) {
			if(v==b[i])
				return -1;
		}
		return v;
	}

}

class Writer {

	private BufferedWriter output;

	public Writer() {
		output = new BufferedWriter(new OutputStreamWriter(System.out));
	}

	public Writer(String s) {
		try {
			output = new BufferedWriter(new FileWriter(s));
		} catch(Exception ex) { ex.printStackTrace(); System.exit(0);}
	}

	public void println() {
		try {
			output.append("\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void print(Object o) {
		try {
			output.append(o.toString());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void println(Object o) {
		try {
			output.append(o.toString()+"\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void printf(String format, Object... args) {
		try {
			output.append(String.format(format, args));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void printfln(String format, Object... args) {
		try {
			output.append(String.format(format, args)+"\n");
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void flush() {
		try {
			output.flush();
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void close() {
		try {
			output.close();
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
}

class Reader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");

	public Reader() {
		input = new BufferedReader(new InputStreamReader(System.in));
	}

	public Reader(String s) {
		try {
			input = new BufferedReader(new FileReader(s));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public double nextDouble() {
		fill();
		return Double.parseDouble(line.nextToken());
	}

	public String nextWord() {
		fill();
		return line.nextToken();
	}

	public int nextInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}

	public long nextLong() {
		fill();
		return Long.parseLong(line.nextToken());
	}

	public double readDouble() {
		double d = 0;
		try {
			d = Double.parseDouble(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return d;
	}

	public int readInt() {
		int i = 0;
		try {
			i = Integer.parseInt(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return i;
	}

	public int[] readInts(int n) {
		int[] a = new int[n];
		for(int i=0; i<n; i++)
			a[i] = nextInt();
		return a;
	}

	public void fillInts(int[] a) {
		for(int i=0; i<a.length; i++)
			a[i] = nextInt();
	}

	public long readLong() {
		long l = 0;
		try {
			l = Long.parseLong(input.readLine());
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return l;
	}

	public String readLine() {
		String s = "";
		try {
			s = input.readLine();
		} catch(IOException io) {io.printStackTrace(); System.exit(0);}
		return s;
	}
}


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

public class Main {

    public static void main(String[] args) throws IOException {
        Reader.init(System.in);
        StringBuilder s=new StringBuilder();
        int n=Reader.nextInt(),m=Reader.nextInt(),minc=Integer.MAX_VALUE,maxc=0,minu=Integer.MAX_VALUE,cur;
        while(n-->0) {
            cur=Reader.nextInt();
            minc=Math.min(minc, cur);
            maxc=Math.max(maxc, cur);
        }
        while(m-->0) minu=Math.min(minu, Reader.nextInt());
        if(minu<=maxc||minc*2>maxc) {
            System.out.println(-1);
            return;
        }
        s.append(maxc).append('\n');
        System.out.print(s);
    }

}

class Reader {

    static StringTokenizer tokenizer;
    static BufferedReader reader;

    public static void init(InputStream input) throws UnsupportedEncodingException {
        reader=new BufferedReader(new InputStreamReader(input,"UTF-8"));
        tokenizer=new StringTokenizer("");
    }

    public static String next() throws IOException {
        while(!tokenizer.hasMoreTokens())
            tokenizer=new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    public static String nextLine() throws IOException {
        return reader.readLine();
    }

    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


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

public class Main {

    public static void main(String[] args) throws IOException {
        Reader.init(System.in);
        StringBuilder s=new StringBuilder();
        int n=Reader.nextInt(),m=Reader.nextInt(),minc=Integer.MAX_VALUE,maxc=0,minu=Integer.MAX_VALUE,cur;
        while(n-->0) {
            cur=Reader.nextInt();
            minc=Math.min(minc, cur);
            maxc=Math.max(maxc, cur);
        }
        while(m-->0) minu=Math.min(minu, Reader.nextInt());
        if(minu<=maxc||minc*2>=minu) {
            System.out.println(-1);
            return;
        }
        s.append(maxc==minc?minc*2:maxc<minc*2?minc*2:maxc).append('\n');
        System.out.print(s);
    }

}

class Reader {

    static StringTokenizer tokenizer;
    static BufferedReader reader;

    public static void init(InputStream input) throws UnsupportedEncodingException {
        reader=new BufferedReader(new InputStreamReader(input,"UTF-8"));
        tokenizer=new StringTokenizer("");
    }

    public static String next() throws IOException {
        while(!tokenizer.hasMoreTokens())
            tokenizer=new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    public static String nextLine() throws IOException {
        return reader.readLine();
    }

    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


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

public class Main {

    public static void main(String[] args) throws IOException {
        Reader.init(System.in);
        StringBuilder s=new StringBuilder();
        int n=Reader.nextInt(),m=Reader.nextInt(),minc=Integer.MAX_VALUE,maxc=0,minu=Integer.MAX_VALUE,cur;
        while(n-->0) {
            cur=Reader.nextInt();
            minc=Math.min(minc, cur);
            maxc=Math.max(maxc, cur);
        }
        while(m-->0) minu=Math.min(minu, Reader.nextInt());
        if(minu<=maxc||(minc==maxc&&minc*2>=minu)||(minc!=maxc&&minc*2>maxc)) {
            System.out.println(-1);
            return;
        }
        s.append(maxc==minc?minc*2:maxc).append('\n');
        System.out.print(s);
    }

}

class Reader {

    static StringTokenizer tokenizer;
    static BufferedReader reader;

    public static void init(InputStream input) throws UnsupportedEncodingException {
        reader=new BufferedReader(new InputStreamReader(input,"UTF-8"));
        tokenizer=new StringTokenizer("");
    }

    public static String next() throws IOException {
        while(!tokenizer.hasMoreTokens())
            tokenizer=new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    public static String nextLine() throws IOException {
        return reader.readLine();
    }

    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


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

public class Tl {

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tok;
	int res = 0;

	void init() throws Exception{
		try {
			in = new BufferedReader(new FileReader(new File("versus.in")));
			out = new PrintWriter(new FileWriter(new File("versus.out")),true);
			tok = new StringTokenizer("");
		} catch (Exception e) {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out,true);
			tok = new StringTokenizer("");
		}

		solve();
		out.close();
	}

	String nextString() throws Exception{

		try {
			while(!tok.hasMoreElements()){
				tok = new StringTokenizer(in.readLine());
			}
			return tok.nextToken();
		} catch (Exception e) {
			return "-1";
		}
	}

	int nextInt() throws Exception{
		return Integer.parseInt(nextString());
	}

	void solve() throws Exception{
		int n = nextInt();
		int m = nextInt();
		int g[] = new int[n];
		int b[] = new int[m];

		for (int i = 0; i < n; i++) {
			g[i] = nextInt();
		}

		for (int i = 0; i < m; i++) {
			b[i] = nextInt();
		}

		Arrays.sort(g);
		Arrays.sort(b);
		res = b[0];
		res -= 1;
		if(res >= g[n-1] && g[0]*2 <= res){
			res = g[0]*2;
			if(res < g[n-1]) res = g[n-1];
			System.out.println(res);
		} else {
			System.out.println(-1);
		}

	}

	public static void main(String[] args) throws Exception {
		new Tl().init();

	}

}


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

public class Tl {

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tok;
	int res = 0;

	void init() throws Exception{
		try {
			in = new BufferedReader(new FileReader(new File("versus.in")));
			out = new PrintWriter(new FileWriter(new File("versus.out")),true);
			tok = new StringTokenizer("");
		} catch (Exception e) {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out,true);
			tok = new StringTokenizer("");
		}

		solve();
		out.close();
	}

	String nextString() throws Exception{

		try {
			while(!tok.hasMoreElements()){
				tok = new StringTokenizer(in.readLine());
			}
			return tok.nextToken();
		} catch (Exception e) {
			return "-1";
		}
	}

	int nextInt() throws Exception{
		return Integer.parseInt(nextString());
	}

	void solve() throws Exception{
		int n = nextInt();
		int m = nextInt();
		int g[] = new int[n];
		int b[] = new int[m];

		for (int i = 0; i < n; i++) {
			g[i] = nextInt();
		}

		for (int i = 0; i < m; i++) {
			b[i] = nextInt();
		}

		Arrays.sort(g);
		Arrays.sort(b);
		res = b[0];
		res -= 1;
		if(res >= g[n-1] && g[0]*2 <= res){
			System.out.println(res);
		} else {
			System.out.println(-1);
		}

	}

	public static void main(String[] args) throws Exception {
		new Tl().init();

	}

}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package C_203;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

/**
 *
 * @author Hitarth
 */
public class Q1 {
    public static void main(String[] args){
        FasterScanner in=new FasterScanner();
        PrintWriter out=new PrintWriter(System.out);
        int n=in.nextInt();
        int m=in.nextInt();
        int[] c=new int[n];
        int[] w=new int[m];
        for(int i=0;i<n;i++){
            c[i]=in.nextInt();
        }
        for(int i=0;i<m;i++){
            w[i]=in.nextInt();
        }
        Arrays.sort(c);
        Arrays.sort(w);
        int v=2*c[0];
        for(int i=1;i<n;i++){
            if(c[i]>v){
                v=c[i];
            }
        }
        if(v<w[0]){
            out.println(v);
        }
        else{
            out.println(-1);
        }
        out.close();
    }

    static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;

public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}

public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}

public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}

public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}

public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}

public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}

public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}

private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}

private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}

}


import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.TreeSet;

public class Solution
{
    public static void main(String[] args) {
        new Solution().solve();
    }
    long[]arr;
    public void solve()
    {
        PrintWriter out=new PrintWriter(System.out);
        int n=in.nextInt();
        int m=in.nextInt();
        int max=0;
        int min=Integer.MAX_VALUE;
        for(int i=0;i<n;i++)
        {
            int temp=in.nextInt();
            max=Math.max(temp, max);
            min=Math.min(min, temp);

        }
        if(min*2<=max)
        {

        }
        else
        {
         max*=2;
        }
        for(int j=0;j<m;j++)
        {
             int temp=in.nextInt();
             if(temp<=max)
             {
                 System.out.println(-1);
                 System.exit(0);
             }
        }
        System.out.println(max);
        out.close();
    }

    FasterScanner in=new FasterScanner();

    public class FasterScanner {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = System.in.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }

        public long[] nextLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nextLong();
            }
            return arr;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }
}