#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 2000001;
const lint maxm = 4000001;
lint tot,ver[maxm],he[maxn],ne[maxm],id[maxm];
pii edge[maxm];
void add( lint x,lint y,lint n ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    id[tot] = n;
}
vector<lint> ans;
lint vis[maxm],vis2[maxm];
void euler( lint x ){
    for( lint cure = he[x];cure;cure = ne[cure] ){
        if( vis[cure] ) continue;
        vis[cure] = vis[cure^1] = 1;
        lint y = ver[cure];
        euler( y );
        ans.push_back( id[cure] );
    }
}
vector<lint> res;
lint du[maxm];
int main(){
    lint n,m;
    scanf("%d%d",&n,&m);
    tot = 1;
    for( lint x,y,i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y,i );
        add( y,x,i );
        du[x]++;du[y]++;
        edge[i].first = x;
        edge[i].second = y;
    }
    lint totm = m;
    for( lint i = 1;i <= n;i++ ){
        if( du[i]&1 ){
            add( 0,i,++totm );add( i,0,totm );
        }
    }
    int res = m;
    for (int i = 0; i <= n; i++) {
        ans.clear();
        euler(i);
        for (int j = 1; j < ans.size(); j += 2) {
            int now = ans[j];
            if(now > m) vis2[now] = 1;
            else {
                int tmp = ans[j - 1];
                if(tmp> m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                int Next = j + 1;
                if(Next == ans.size()) Next = 0;
                tmp = ans[Next];
                if(tmp > m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                vis2[now] = 1;
                res--;
            }
        }
    }
    printf("%d\n", res );
    for( lint i = 1;i <= m;i++ ){
        if( vis2[i] ) continue;
        printf("%d %d\n", edge[i].first,edge[i].second );
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 2000001;
const lint maxm = 4000001;
lint tot,ver[maxm],he[maxn],ne[maxm],id[maxm];
pii edge[maxm];
void add( lint x,lint y,lint n ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    id[tot] = n;
}
vector<lint> ans;
lint vis[maxm],vis2[maxm];
void euler( lint x ){
    for( lint cure = he[x];cure;cure = ne[cure] ){
        if( vis[cure] ) continue;
        vis[cure] = vis[cure^1] = 1;
        lint y = ver[cure];
        euler( y );
        ans.push_back( id[cure] );
    }
}
vector<lint> res;
lint du[maxm];
int main(){
    lint n,m;
    scanf("%d%d",&n,&m);
    tot = 1;
    for( lint x,y,i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y,i );
        add( y,x,i );
        du[x]++;du[y]++;
        edge[i].first = x;
        edge[i].second = y;
    }
    lint totm = m;
    for( lint i = 1;i <= n;i++ ){
        if( du[i]&1 ){
            add( 0,i,++totm );add( i,0,totm );
        }
    }
    lint cnt = 0;
    //memset(vis, 0, sizeof(vis));
    for( lint i = 0;i <= n;i++ ) {
        ans.clear();
        euler(i);
        for (lint i = 0; i < ans.size(); i++) {
            if ((i & 1) && ans[i] <= m ) {
                if (ans[i - 1] > m && !vis2[ans[i - 1] ]) {
                    vis2[ans[i - 1] ] = 1;
                } else if (ans[(i + 1) % ans.size()] > m && !vis2[ans[(i + 1) % ans.size()]]) {
                    vis2[ans[(i + 1) % ans.size()]] = 1;
                } else {
                    vis2[ ans[i] ] = 1;
                    cnt++;
                }
            }else if( i&1 ){
                vis2[ ans[i] ] = 1;
            }
        }
    }
    printf("%d\n", m-cnt );
    for( lint i = 1;i <= m;i++ ){
        if( vis2[i] ) continue;
        printf("%d %d\n", edge[i].first,edge[i].second );
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 2000001;
const lint maxm = 4000001;
lint tot,ver[maxm],he[maxn],ne[maxm],id[maxm];
pii edge[maxm];
void add( lint x,lint y,lint n ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    id[tot] = n;
}
vector<lint> ans;
lint vis[maxm],v[maxn],vis2[maxm],st[maxm],re[maxm],top;
void euler (int s) {
    st[++top] = s;
    while(top > 0) {
        int x = st[top], i = he[x];
        while(i && v[i]) i = ne[i];
        if(i) {
            st[++top] = ver[i];
            re[top] = id[i];
            v[i] = v[i ^ 1] = 1;
            he[x] = ne[i];
        } else {
            if( top != 1 )
            ans.push_back(re[top]);
            top--;
        }
    }
}
vector<lint> res;
lint du[maxm];
int main(){
    lint n,m;
    scanf("%d%d",&n,&m);
    tot = 1;
    for( lint x,y,i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y,i );
        add( y,x,i );
        du[x]++;du[y]++;
        edge[i].first = x;
        edge[i].second = y;
    }
    lint totm = m;
    for( lint i = 1;i <= n;i++ ){
        if( du[i]&1 ){
            add( 0,i,++totm );add( i,0,totm );
        }
    }
    int res = m;
    for (int i = 0; i <= n; i++) {
        ans.clear();
        euler(i);
        for (int j = 1; j < ans.size(); j += 2) {
            int now = ans[j];
            if(now > m) vis2[now] = 1;
            else {
                int tmp = ans[j - 1];
                if(tmp> m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                int Next = j + 1;
                if(j == ans.size()) Next = 0;
                tmp = ans[Next];
                if(tmp > m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                vis2[now] = 1;
                res--;
            }
        }
    }
    printf("%d\n", res );
    for( lint i = 1;i <= m;i++ ){
        if( vis2[i] ) continue;
        printf("%d %d\n", edge[i].first,edge[i].second );
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 2000001;
const lint maxm = 4000001;
lint tot,ver[maxm],he[maxn],ne[maxm],id[maxm];
pii edge[maxm];
void add( lint x,lint y,lint n ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    id[tot] = n;
}
vector<lint> ans;
lint vis[maxm],vis2[maxn];
void euler( lint x){
    vis2[x] = 1;
    for( lint cure = he[x];cure;cure = ne[cure] ){
        if( vis[cure] ) continue;
        vis[cure] = vis[cure^1] = 1;
        lint y = ver[cure];
        euler( y );
        ans.push_back( id[cure] );
    }
}
vector<pii> res;
lint du[maxm];
int main(){
    lint n,m;
    scanf("%d%d",&n,&m);
    tot = 1;
    for( lint x,y,i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y,i );
        add( y,x,i );
        du[x]++;du[y]++;
        edge[i].first = x;
        edge[i].second = y;
    }
    for( lint i = 1;i <= n;i++ ){
        if( du[i]&1 ){
            add( 0,i,0 );add( i,0,0 );
        }
    }
    lint used ,vis;
    for( lint i = 0;i <= n;i++ ){
        if( vis2[i] ) continue;
        used = 0; ans.clear();vis = 0;
        euler(i);
        for( lint i = 0;i < ans.size();i++ ){
            if( i&1 && ans[i]  ) {
                if (!used && !ans[i - 1]) {
                    if (i - 1 == 0) vis = 1;
                    res.push_back( pii(edge[ans[i]].first, edge[ans[i]].second));
                } else if (i + 1 < ans.size() && ans[i + 1] == 0) {
                    used = 1;
                    res.push_back( pii(edge[ans[i]].first, edge[ans[i]].second));
                } else if (i + 1 == ans.size() && !vis && ans[0] == 0) {
                    res.push_back( pii(edge[ans[i]].first, edge[ans[i]].second));
                } else {
                    used = 0;
                }
            }else if( i%2 == 0 && ans[i] ){
                res.push_back( pii( edge[ans[i]].first, edge[ans[i]].second));
            }
        }
    }
    printf("%d\n",res.size());
    for( lint i =0;i < res.size();i++ ){
        printf("%d %d\n",res[i].first,res[i].second);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 2000001;
const lint maxm = 4000001;
lint tot,ver[maxm],he[maxn],ne[maxm],id[maxm];
pii edge[maxm];
void add( lint x,lint y,lint n ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    id[tot] = n;
}
vector<lint> ans;
lint vis[maxm],vis2[maxm];
void euler( lint x ){
    for( lint cure = he[x];cure;cure = ne[cure] ){
        if( vis[cure] ) continue;
        vis[cure] = vis[cure^1] = 1;
        lint y = ver[cure];
        euler( y );
        ans.push_back( id[cure] );
    }
}
vector<lint> res;
lint du[maxm];
int main(){
    lint n,m;
    scanf("%d%d",&n,&m);
    tot = 1;
    for( lint x,y,i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y,i );
        add( y,x,i );
        du[x]++;du[y]++;
        edge[i].first = x;
        edge[i].second = y;
    }
    lint totm = m;
    for( lint i = 1;i <= n;i++ ){
        if( du[i]&1 ){
            add( 0,i,++totm );add( i,0,totm );
        }
    }
    int res = m;
    for (int i = 0; i <= n; i++) {
        ans.clear();
        euler(i);
        for (int j = 1; j < ans.size(); j += 2) {
            int now = ans[j];
            if(now > m) vis2[now] = 1;
            else {
                int tmp = ans[j - 1];
                if(tmp> m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                int Next = j + 1;
                if(j == ans.size()) Next = 0;
                tmp = ans[Next];
                if(tmp > m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                vis2[now] = 1;
                res--;
            }
        }
    }
    printf("%d\n", res );
    for( lint i = 1;i <= m;i++ ){
        if( vis2[i] ) continue;
        printf("%d %d\n", edge[i].first,edge[i].second );
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 2000001;
const lint maxm = 4000001;
lint tot,ver[maxm],he[maxn],ne[maxm],id[maxm];
pii edge[maxm];
void add( lint x,lint y,lint n ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    id[tot] = n;
}
vector<lint> ans;
lint vis[maxm],v[maxn],vis2[maxm],st[maxm],re[maxm];
void euler( lint x ){
    lint top = 0;
    st[top++] = x;
    re[0] = 0;
    //v[x] = 1;
    while( top ){
        x = st[top-1];
        lint cure  = he[x];
        while( cure && vis[cure] ) cure = ne[cure];
        if( cure ){
            lint y = ver[cure];
            vis[cure] = vis[cure^1] = 1;
            st[top++] = y;
            re[top-1] = id[cure];
        }else{
            top--;
            if( top  )ans.push_back( re[top] );
        }
    }
}
vector<lint> res;
lint du[maxm];
int main(){
    lint n,m;
    scanf("%d%d",&n,&m);
    tot = 1;
    for( lint x,y,i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y,i );
        add( y,x,i );
        du[x]++;du[y]++;
        edge[i].first = x;
        edge[i].second = y;
    }
    lint totm = m;
    for( lint i = 1;i <= n;i++ ){
        if( du[i]&1 ){
            add( 0,i,++totm );add( i,0,totm );
        }
    }
    int res = m;
    for (int i = 0; i <= n; i++) {
        ans.clear();
        euler(i);
        for (int j = 1; j < ans.size(); j += 2) {
            int now = ans[j];
            if(now > m) vis2[now] = 1;
            else {
                int tmp = ans[j - 1];
                if(tmp> m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                int Next = j + 1;
                if(j == ans.size()) Next = 0;
                tmp = ans[Next];
                if(tmp > m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                vis2[now] = 1;
                res--;
            }
        }
    }
    printf("%d\n", res );
    for( lint i = 1;i <= m;i++ ){
        if( vis2[i] ) continue;
        printf("%d %d\n", edge[i].first,edge[i].second );
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 2000001;
const lint maxm = 4000001;
lint tot,ver[maxm],he[maxn],ne[maxm],id[maxm];
pii edge[maxm];
void add( lint x,lint y,lint n ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    id[tot] = n;
}
vector<lint> ans;
lint vis[maxm],v[maxn],vis2[maxm],st[maxm],re[maxm],top;
void euler( lint x ){
    st[++top] = x;
    while( top>0 ){
        lint x = st[top];
        lint cure  = he[x];
        while( cure && vis[cure] ) cure = ne[cure];
        if( cure ){
            lint y = ver[cure];
            vis[cure] = vis[cure^1] = 1;
            st[++top] = y;
            re[top] = id[cure];
            he[x] = ne[cure];
        }else{
            if( top !=  1  )ans.push_back( re[top] );
            top--;
        }
    }
}
vector<lint> res;
lint du[maxm];
int main(){
    lint n,m;
    scanf("%d%d",&n,&m);
    tot = 1;
    for( lint x,y,i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y,i );
        add( y,x,i );
        du[x]++;du[y]++;
        edge[i].first = x;
        edge[i].second = y;
    }
    lint totm = m;
    for( lint i = 1;i <= n;i++ ){
        if( du[i]&1 ){
            add( 0,i,++totm );add( i,0,totm );
        }
    }
    int res = m;
    for (int i = 0; i <= n; i++) {
        ans.clear();
        euler(i);
        for (int j = 1; j < ans.size(); j += 2) {
            int now = ans[j];
            if(now > m) vis2[now] = 1;
            else {
                int tmp = ans[j - 1];
                if(tmp> m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                int Next = j + 1;
                if(j == ans.size()) Next = 0;
                tmp = ans[Next];
                if(tmp > m && vis2[tmp] == 0) {
                    vis2[tmp] = 1;
                    continue;
                }
                vis2[now] = 1;
                res--;
            }
        }
    }
    printf("%d\n", res );
    for( lint i = 1;i <= m;i++ ){
        if( vis2[i] ) continue;
        printf("%d %d\n", edge[i].first,edge[i].second );
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 1000001;
const lint maxm = 2000001;
lint tot,ver[maxm],he[maxn],ne[maxm],id[maxm];
pii edge[maxm];
void add( lint x,lint y,lint n ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    id[tot] = n;
}
vector<lint> ans;
lint vis[maxm],vis2[maxm];
void euler( lint x ){
    for( lint cure = he[x];cure;cure = ne[cure] ){
        if( vis[cure] ) continue;
        vis[cure] = vis[cure^1] = 1;
        lint y = ver[cure];
        euler( y );
        ans.push_back( id[cure] );
    }
}
vector<lint> res;
lint du[maxm];
int main(){
    lint n,m;
    scanf("%d%d",&n,&m);
    tot = 1;
    for( lint x,y,i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y,i );
        add( y,x,i );
        du[x]++;du[y]++;
        edge[i].first = x;
        edge[i].second = y;
    }
    lint totm = m;
    for( lint i = 1;i <= n;i++ ){
        if( du[i]&1 ){
            add( 0,i,++totm );add( i,0,totm );
        }
    }
    lint cnt = 0;
    //memset(vis, 0, sizeof(vis));
    for( lint i = 0;i <= n;i++ ) {
        ans.clear();
        euler(i);
        for (lint i = 0; i < ans.size(); i++) {
            if ((i & 1) && ans[i] <= m ) {
                if (ans[i - 1] > m && !vis2[ans[i - 1] ]) {
                    vis2[ans[i - 1] ] = 1;
                } else if (ans[(i + 1) % ans.size()] > m && !vis2[ans[(i + 1) % ans.size()]]) {
                    vis2[ans[(i + 1) % ans.size()]] = 1;
                } else {
                    vis2[ ans[i] ] = 1;
                    cnt++;
                }
            }
        }
    }
    printf("%d\n", m-cnt );
    for( lint i = 1;i <= m;i++ ){
        if( vis2[i] ) continue;
        printf("%d %d\n", edge[i].first,edge[i].second );
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 2000001;
const lint maxm = 4000001;
lint tot,ver[maxm],he[maxn],ne[maxm],id[maxm];
pii edge[maxm];
void add( lint x,lint y,lint n ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
    id[tot] = n;
}
vector<lint> ans;
lint vis[maxm],vis2[maxm];
void euler( lint x ){
    for( lint cure = he[x];cure;cure = ne[cure] ){
        if( vis[cure] ) continue;
        vis[cure] = vis[cure^1] = 1;
        lint y = ver[cure];
        euler( y );
        ans.push_back( id[cure] );
    }
}
vector<lint> res;
lint du[maxm];
int main(){
    lint n,m;
    scanf("%d%d",&n,&m);
    tot = 1;
    for( lint x,y,i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y,i );
        add( y,x,i );
        du[x]++;du[y]++;
        edge[i].first = x;
        edge[i].second = y;
    }
    lint totm = m;
    for( lint i = 1;i <= n;i++ ){
        if( du[i]&1 ){
            add( 0,i,++totm );add( i,0,totm );
        }
    }
    lint cnt = 0;
    //memset(vis, 0, sizeof(vis));
    for( lint i = 0;i <= n;i++ ) {
        ans.clear();
        euler(i);
        for (lint i = 0; i < ans.size(); i++) {
            if ((i & 1) && ans[i] <= m ) {
                if (ans[i - 1] > m && !vis2[ans[i - 1] ]) {
                    vis2[ans[i - 1] ] = 1;
                } else if (ans[(i + 1) % ans.size()] > m && !vis2[ans[(i + 1) % ans.size()]]) {
                    vis2[ans[(i + 1) % ans.size()]] = 1;
                } else {
                    vis2[ ans[i] ] = 1;
                    cnt++;
                }
            }
        }
    }
    printf("%d\n", m-cnt );
    for( lint i = 1;i <= m;i++ ){
        if( vis2[i] ) continue;
        printf("%d %d\n", edge[i].first,edge[i].second );
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef int lint;
typedef pair<lint,lint> pii;
const lint maxn = 1e6+1;
lint du[maxn];
struct node{
    lint x,y,id;
    lint dx,dy;
    node( lint xx,lint yy,lint ddx,lint ddy ){
        x = xx;
        y = yy;
        dx = ddx;
        dy = ddy;
    }
    bool operator <( const node& b )const{
        lint z1 = min( dx,dy );
        lint z2 = min( b.dx,b.dy );
        if( z1 == z2 ){
            return max( dx,dy ) < max( b.dx,b.dy );
        }
        return z1 < z2;
    }
};
priority_queue<node> que;
vector<pii> edge;
lint ans;
int main(){
    lint n,m,x,y;
    scanf("%d%d",&n,&m);
    for( lint i = 1;i <= m;i++ ){
        scanf("%d%d",&x,&y);
        du[x]++;du[y]++;
        edge.push_back( pii(x,y) );
    }
    for( lint i = 1;i <= n;i++ ) du[i] /= 2;
    for( lint i = 0;i < edge.size();i++ ){
        lint x = edge[i].first;
        lint y = edge[i].second;
        que.push( node( x,y,du[x],du[y] ) );
    }
    lint cnt = m - (n+m+1)/2;
    while( que.size() ){
        node cur = que.top();
        que.pop();
        lint x = cur.x;lint y = cur.y;
        if( cur.dx != du[x] || cur.dy != du[y] ){
            que.push( node( x,y,du[x],du[y] ) );
            continue;
        }
        du[x]--;du[y]--;
        ans++;
        if( ans >= cnt+1 ) break;
    }
    printf("%d\n", m - ans );
    while( que.size() ){
        node cur = que.top();que.pop();
        printf("%d %d\n",cur.x,cur.y);
    }
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define PB push_back
#define MP make_pair
#define PII pair<int, int>
#define FF first
#define SS second
using namespace std;
template<typename T> inline void gmax(T &a, T b){a = a > b ? a : b;}
template<typename T> inline void gmin(T &a, T b){a = a < b ? a : b;}
const int MAXN = 1000010;
const int MAXM = 4000010;
struct Edge{
	int to, next, visited;
	Edge(){}
	Edge(int to, int next):to(to),next(next){}
}edge[MAXM];
int head[MAXN], cnt;
void initedge(){
	cnt = 0;
	memset(head, -1, sizeof(head));
}
void addedge(int from, int to){
	edge[cnt] = Edge(to, head[from]);
	head[from] = cnt++;
}
void addedge2(int x, int y){
	addedge(x, y);
	addedge(y, x);
}
int n, m;
int d[MAXN];
int v[MAXM], len;
int iter[MAXN];
void dfs(int x){
	for(register int &p = iter[x]; ~p; p = edge[p].next) if(~edge[p].to){
		int y = edge[p].to;
		edge[p].to = edge[p^1].to = -1;
		dfs(y);
	}
	v[len++] = x;
}
vector<PII> ans;
int main(){
	scanf("%d%d", &n, &m);
	initedge();
	for(int i = 0; i < m; i++){
		int u, v;
		scanf("%d%d", &u, &v);
		d[u]++; d[v]++;
		addedge2(u, v);
	}
	for(int i = 1; i <= n; i++) if(d[i] & 1){
		d[i]++; d[0]++;
		addedge2(i, 0);
	}
	for(int i = 0; i <= n; i++) iter[i] = head[i];
	for(int x = 0; x <= n; x++) if(~iter[x]){
		len = 0;
		dfs(x);
		v[len + 1] = v[1];
		for(int i = 1; i < len; i++)//v[i] & v[i - 1]
			if(v[i - 1] && v[i]){
				if((i & 1) || !v[i - 2] || !v[i + 1])
					ans.PB(MP(v[i - 1], v[i]));
			}
	}
	printf("%d\n", ans.size());
	for(unsigned i = 0; i < ans.size(); i++)
		printf("%d %d\n", ans[i].FF, ans[i].SS);
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define PB push_back
#define MP make_pair
#define PII pair<int, int>
#define FF first
#define SS second
using namespace std;
template<typename T> inline void gmax(T &a, T b){a = a > b ? a : b;}
template<typename T> inline void gmin(T &a, T b){a = a < b ? a : b;}
const int MAXN = 1000010;
const int MAXM = 4000010;
struct Edge{
	int to, next;
	Edge(){}
	Edge(int to, int next):to(to),next(next){}
}edge[MAXM];
int head[MAXN], cnt;
void initedge(){
	cnt = 0;
	memset(head, -1, sizeof(head));
}
void addedge(int from, int to){
	edge[cnt] = Edge(to, head[from]);
	head[from] = cnt++;
}
void addedge2(int x, int y){
	addedge(x, y);
	addedge(y, x);
}
int n, m;
int d[MAXN];
int v[MAXM], len;
int iter[MAXN];
void dfs(int x){
	for(register int &p = iter[x]; ~p;) if(~edge[p].to){
		int y = edge[p].to;
		edge[p].to = edge[p^1].to = -1;
		p = edge[p].next;
		dfs(y);
	}
	else p = edge[p].next;
	v[len++] = x;
}
vector<PII> ans;
int main(){
	scanf("%d%d", &n, &m);
	initedge();
	for(int i = 0; i < m; i++){
		int u, v;
		scanf("%d%d", &u, &v);
		d[u]++; d[v]++;
		addedge2(u, v);
	}
	for(int i = 1; i <= n; i++) if(d[i] & 1){
		d[i]++; d[0]++;
		addedge2(i, 0);
	}
	for(int i = 0; i <= n; i++) iter[i] = head[i];
	for(int x = 0; x <= n; x++) if(~iter[x]){
		len = 0;
		dfs(x);
		v[len + 1] = v[1];
		for(int i = 1; i < len; i++)//v[i] & v[i - 1]
			if(v[i - 1] && v[i]){
				if((i & 1) || !v[i - 2] || !v[i + 1])
					ans.PB(MP(v[i - 1], v[i]));
			}
	}
	printf("%d\n", ans.size());
	for(unsigned i = 0; i < ans.size(); i++)
		printf("%d %d\n", ans[i].FF, ans[i].SS);
	return 0;
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

vector<int>	a;

int ksm(int A,int T)
{
	int now=1;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		if(pppp==1000 && ttt%500==0)	return 0;
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
				if(now>=ksm(p,a2-a1))	(vis[a1]+=ksm(p,a2-a1)*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a1),vis[a2]=0;
			}
			vis[a1]%=2;
		}
//		cout<<"gg"<<endl;
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a[i]]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
//			cout<<s[a[i]]<<endl;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];

			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int ss=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') ss=ss*10+ch-'0',ch=getchar();
   return ss*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
//	if(kk[T])	return kk[T];
	int now=1,ty=T;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(ty);
	kk[ty]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		b.clear();
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
//				cout<<a[j]-a[j-1]<<endl;
				if(pow(p,a2-a[j-1])>1e6)	break;
				if(now>=ksm(p,a[j]-a[j-1]))
				{
					(vis[a1]+=p*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a[j-1]),vis[a2]=0;
					continue;
				}
				break;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a1]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];
			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int ss=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') ss=ss*10+ch-'0',ch=getchar();
   return ss*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
//	if(kk[T])	return kk[T];
	int now=1,ty=T;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(ty);
	kk[ty]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		if(pppp==1000 && ttt>=10)
		{
			cout<<0<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		b.clear();
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
				if(now>=ksm(p,a2-a1))
				(vis[a1]+=ksm(p,a2-a1)*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a1),vis[a2]=0;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a[i]]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];
			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],tt[maxn],t;

signed main()
{
	ios::sync_with_stdio(0);
	int pppp=100000;
	cin>>t;
	for(int ttt=1;ttt<=t;ttt++)
	{

		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=tt[i]=0;
		mx=0;
		cin>>n>>p;
		if(ttt==189)	cout<<n<<' '<<p<<endl;
		for(int i=1;i<=n;i++)
		{

			int x;
			cin>>x;
			if(ttt==189)	cout<<x<<' ';
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+(now-1)/p+1,vis[j]=0;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			tt[i]=tmp;
			(s[i]=s[max(0ll,i-1)]+tmp*v[i])%=mod;
//			cout<<t<<' '<<i<<' '<<s[i]<<endl;
			(tmp*=p)%=mod;
		}
		if(pppp=100000)	continue;
		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(s[0]%mod+mod)%mod<<endl;
					break;
				}
				cout<<((tt[i]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000006
#define int long long
#define mod 1000000007
using namespace std;
int n,p,vis[maxn],s[maxn],v[maxn],t;
inline int read(){
int ss=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') ss=ss*10+ch-'0',ch=getchar();
return ss*w;
}
vector<int>	a;
vector<int> b;
int kk[maxn];
int ksm(int A,int T)
{
if(kk[T])	return kk[T];
int now=1,ty=T;
while(T)
{
if(T&1)	now=now*A%mod;
A=A*A%mod;
T>>=1;
}
b.push_back(ty);
kk[ty]=now;
return now;
}
signed main()
{
t=read();
int pppp=t;
for(int ttt=1;ttt<=t;ttt++)
{
for(int i=0;i<a.size();i++)
{
int j=a[i];
s[j]=vis[j]=v[j]=0;
}
for(int i=0;i<b.size();i++)
{
kk[b[i]]=0;
}
b.clear();
a.clear();
n=read(),p=read();
for(int i=1;i<=n;i++)
{
int x;
x=read();
vis[x]++;
if(vis[x]==1)	a.push_back(x);
v[x]++;
}
sort(a.begin(),a.end());
if(p==1)
{
cout<<n%2<<endl;
continue;
}
for(int i=0;i<a.size();i++)
{
int a1=a[i],now=vis[a1];
for(int j=i+1;j<a.size();j++)
{
int a2=a[j];
// cout<<a[j]-a[j-1]<<endl;
if(now>=ksm(p,a[j]-a[j-1]))
{
(vis[a1]+=p*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a[j-1]),vis[a2]=0;
}
}
vis[a1]%=2;
}
for(int i=0;i<a.size();i++)
{
int a1=a[i];
(s[a1]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
}
int flag=1;
for(int i=a.size()-1;i>=0;i--)
{
int a1=a[i];
if(vis[a1]==1) //ÆæÊý
{
flag=0;
if(i==0)
{
cout<<(ksm(p,a1))%mod<<'\n';
break;
}
cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
break;
}
}
if(flag==1)	cout<<0<<'\n';
}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],tt[maxn],t;

signed main()
{
	ios::sync_with_stdio(0);

	cin>>t;
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{

		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=tt[i]=0;
		mx=0;
		cin>>n>>p;
		if(ttt==27 && p==6)	cout<<n<<' '<<p<<endl;
		for(int i=1;i<=n;i++)
		{

			int x;
			cin>>x;
			if(ttt==27 && p==6)	cout<<x<<' ';
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+now/p,vis[j]=0;
//					cout<<now<<endl;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			tt[i]=tmp;
			(s[i]=s[max(0ll,i-1)]+tmp*v[i])%=mod;
			(tmp*=p)%=mod;
		}
		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(s[0]%mod+mod)%mod<<endl;
					break;
				}
				cout<<((tt[i]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

vector<int>	a;

int ksm(int A,int T)
{
	int now=1;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	return now;
}

signed main()
{
	ios::sync_with_stdio(0);
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
				if(now>=ksm(p,a2-a1))	(vis[a1]+=ksm(p,a2-a1)*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a1),vis[a2]=0;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a[i]]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
//			cout<<s[a[i]]<<endl;
		}
//		cout<<endl;
		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];

			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<endl;
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],tt[maxn],t;

signed main()
{
	ios::sync_with_stdio(0);

	cin>>t;
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{

		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=tt[i]=0;
		mx=0;
		cin>>n>>p;
//		if(ttt==27 && p==6)	cout<<n<<' '<<p<<endl;
		for(int i=1;i<=n;i++)
		{

			int x;
			cin>>x;
//			if(ttt==27 && p==6)	cout<<x<<' ';
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+now/p,vis[j]=0;
//					cout<<now<<endl;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			tt[i]=tmp;
			(s[i]=s[max(0ll,i-1)]+tmp*v[i])%=mod;
//			cout<<s[i]<<' ';
			(tmp*=p)%=mod;
		}
//		cout<<endl;
		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
//				cout<<i<<endl;
				flag=0;
				if(i==0)
				{
					cout<<(tt[0])%mod<<endl;
					break;
				}
				cout<<((tt[i]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],tt[maxn],t;

signed main()
{
	ios::sync_with_stdio(0);

	cin>>t;
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{

		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=tt[i]=0;
		mx=0;
		cin>>n>>p;
//		if(ttt==189)	cout<<n<<' '<<p<<endl;
		for(int i=1;i<=n;i++)
		{

			int x;
			cin>>x;
			if(ttt==189)	cout<<x<<' ';
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+now/p,vis[j]=0;
//					cout<<now<<endl;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			tt[i]=tmp;
			(s[i]=s[max(0ll,i-1)]+tmp*v[i])%=mod;
//			cout<<t<<' '<<i<<' '<<s[i]<<endl;
			(tmp*=p)%=mod;
		}
//		if(pppp==100000)	continue;
		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(s[0]%mod+mod)%mod<<endl;
					break;
				}
				cout<<((tt[i]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

vector<int>	a;

int ksm(int A,int T)
{
	int now=1;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
				if(now>=ksm(p,a2-a1))	(vis[a1]+=ksm(p,a2-a1)*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a1),vis[a2]=0;
			}
			vis[a1]%=2;
		}
//		cout<<"gg"<<endl;
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a[i]]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
//			cout<<s[a[i]]<<endl;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];

			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int ss=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') ss=ss*10+ch-'0',ch=getchar();
   return ss*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
	if(kk[T])	return kk[T];
	int now=1,ty=T;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(ty);
	kk[ty]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		b.clear();
		a.clear();
		n=read(),p=read();
		int tmp=p,cnt=-1;
		while(tmp)
			cnt++,tmp/=10;
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
//				cout<<a[j]-a[j-1]<<endl;
				if((a[j]-a[i-1])*cnt>=8)	break;
				if(now>=ksm(p,a[j]-a[j-1]))
				{
					(vis[a1]+=p*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a[j-1]),vis[a2]=0;
					continue;
				}
				break;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a1]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];
			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int ss=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') ss=ss*10+ch-'0',ch=getchar();
   return ss*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
	if(kk[T])	return kk[T];
	int now=1,ty=T;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(ty);
	kk[ty]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		b.clear();
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
//				cout<<a[j]-a[j-1]<<endl;
				if(now>=ksm(p,a[j]-a[j-1]))
				{
					(vis[a1]+=p*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a[j-1]),vis[a2]=0;
				}

			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a1]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];
			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],t;

signed main()
{
	ios::sync_with_stdio(0);
	cin>>t;
	while(t--)
	{

		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=0;
		mx=0;
		cin>>n>>p;
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+(now-1)/p+1,vis[j]=0;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			(s[i]=s[i-1]+tmp*v[i])%=mod;
//			cout<<t<<' '<<i<<' '<<s[i]<<endl;
			(tmp*=p)%=mod;
		}

		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(s[0]%mod+mod)%mod<<endl;
					break;
				}
				cout<<((s[i]-s[i-1]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],tt[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

signed main()
{
	ios::sync_with_stdio(0);
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{

		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=tt[i]=0;
		mx=0;
		n=read(),p=read();
//		if(ttt==27 && p==6)	cout<<n<<' '<<p<<endl;
		for(int i=1;i<=n;i++)
		{

			int x;
			x=read();
//			if(ttt==27 && p==6)	cout<<x<<' ';
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+now/p,vis[j]=0;
//					cout<<now<<endl;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			tt[i]=tmp;
			(s[i]=s[max(0ll,i-1)]+tmp*v[i])%=mod;
//			cout<<s[i]<<' ';
			(tmp*=p)%=mod;
		}
//		cout<<endl;
		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
//				cout<<i<<endl;
				flag=0;
				if(i==0)
				{
					cout<<(tt[0])%mod<<endl;
					break;
				}
				cout<<((tt[i]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],tt[maxn],t;

signed main()
{
	ios::sync_with_stdio(0);
	cin>>t;
	while(t--)
	{

		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=tt[i]=0;
		mx=0;
		cin>>n>>p;
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+(now-1)/p+1,vis[j]=0;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			tt[i]=tmp;
			(s[i]=s[max(0ll,i-1)]+tmp*v[i])%=mod;
//			cout<<t<<' '<<i<<' '<<s[i]<<endl;
			(tmp*=p)%=mod;
		}

		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(s[0]%mod+mod)%mod<<endl;
					break;
				}
				cout<<((tt[i]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],tt[maxn],t;

signed main()
{
	ios::sync_with_stdio(0);

	cin>>t;
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{

		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=tt[i]=0;
		mx=0;
		cin>>n>>p;
		if(ttt==189)	cout<<n<<' '<<p<<endl;
		for(int i=1;i<=n;i++)
		{

			int x;
			cin>>x;
			if(ttt==189)	cout<<x<<' ';
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+(now-1)/p+1,vis[j]=0;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			tt[i]=tmp;
			(s[i]=s[max(0ll,i-1)]+tmp*v[i])%=mod;
//			cout<<t<<' '<<i<<' '<<s[i]<<endl;
			(tmp*=p)%=mod;
		}
		if(pppp==100000)	continue;
		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(s[0]%mod+mod)%mod<<endl;
					break;
				}
				cout<<((tt[i]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

vector<int>	a;

int ksm(int A,int T)
{
	int now=1;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		if(pppp==1000 && ttt%100==0)	return 0;
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
				if(now>=ksm(p,a2-a1))	(vis[a1]+=ksm(p,a2-a1)*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a1),vis[a2]=0;
			}
			vis[a1]%=2;
		}
//		cout<<"gg"<<endl;
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a[i]]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
//			cout<<s[a[i]]<<endl;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];

			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],t;

signed main()
{
	ios::sync_with_stdio(0);
	cin>>t;
	while(t--)
	{
		memset(s,0,sizeof(s));
		memset(vis,0,sizeof(vis));
		memset(v,0,sizeof(v));
		mx=0;
		cin>>n>>p;
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+(now-1)/p+1,vis[j]=0;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			(s[i]=s[i-1]+tmp*v[i])%=mod;
//			cout<<t<<' '<<i<<' '<<s[i]<<endl;
			(tmp*=p)%=mod;
		}

		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(s[0]%mod+mod)%mod<<endl;
					break;
				}
				cout<<((s[i]-s[i-1]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
//	cout<<A<<' '<<T<<' '<<kk[T]<<endl;
	if(kk[T])	return kk[T];
	int now=1,ty=T;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(ty);
	kk[ty]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		b.clear();
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
				if(now>=ksm(p,a2-a1))
				(vis[a1]+=ksm(p,a2-a1)*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a1),vis[a2]=0;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a[i]]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];

			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],tt[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

signed main()
{
	ios::sync_with_stdio(0);
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		if(pppp==9999)
		{
			if(ttt%1000==0)	cout<<ttt<<endl;
			exit(0);
			}
		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=tt[i]=0;
		mx=0;
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+now/p,vis[j]=0;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			tt[i]=tmp;
			(s[i]=s[max(0ll,i-1)]+tmp*v[i])%=mod;
			(tmp*=p)%=mod;
		}
//		cout<<endl;
		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(tt[0])%mod<<endl;
					break;
				}
				cout<<((tt[i]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int ss=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') ss=ss*10+ch-'0',ch=getchar();
   return ss*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
	if(kk[T])	return kk[T];
	int now=1,ty=T;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(ty);
	kk[ty]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		b.clear();
		a.clear();
		n=read(),p=read();
		int tmp=p,cnt=-1;
		while(tmp)
			cnt++,tmp/=10;
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
//				cout<<a[j]-a[j-1]<<endl;
				if((a[j]-a[i-1])*cnt>=7)	break;
				if(now>=ksm(p,a[j]-a[j-1]))
				{
					(vis[a1]+=p*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a[j-1]),vis[a2]=0;
					continue;
				}
				break;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a1]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];
			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

vector<int>	a;

int ksm(int A,int T)
{
	int now=1;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	return now;
}

signed main()
{
	ios::sync_with_stdio(0);
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
				if(now>=ksm(p,a2-a1))	(vis[a1]+=ksm(p,a2-a1)*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a1),vis[a2]=0;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<=a.size();i++)
		{
			int a1=a[i];
			(s[i]=s[max(0ll,i-1)]+ksm(p,a1)*v[a1])%=mod;
		}
//		cout<<endl;
		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];

			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<endl;
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int ss=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') ss=ss*10+ch-'0',ch=getchar();
   return ss*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
	if(kk[T])	return kk[T];
	int now=1,ty=T;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(ty);
	kk[ty]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		b.clear();
		a.clear();
		n=read(),p=read();
		int tmp=p,cnt=-1;
		while(tmp)
			cnt++,tmp/=10;
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
//				cout<<a[j]-a[j-1]<<endl;
				if((a[j]-a[i-1])*cnt>=10)	break;
				if(now>=ksm(p,a[j]-a[j-1]))
				{
					(vis[a1]+=p*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a[j-1]),vis[a2]=0;
					continue;
				}
				break;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a1]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];
			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
	if(kk[T])	return kk[T];
	int now=1,ty=T;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(ty);
	kk[ty]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		b.clear();
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
				if(now>=ksm(p,a2-a1))
				(vis[a1]+=ksm(p,a2-a1)*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a1),vis[a2]=0;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a[i]]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];

			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],mx,s[maxn],v[maxn],tt[maxn],t;

signed main()
{
	ios::sync_with_stdio(0);

	cin>>t;
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{

		for(int i=0;i<=mx;i++)
			s[i]=vis[i]=v[i]=tt[i]=0;
		mx=0;
		cin>>n>>p;
//		if(ttt==189)	cout<<n<<' '<<p<<endl;
		for(int i=1;i<=n;i++)
		{

			int x;
			cin>>x;
			vis[x]++;
			v[x]++;
			mx=max(mx,x);
		}
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<=mx;i++)
		{
			if(vis[i]>=p)
			{
				int now=vis[i];
				for(int j=i+1;j<=mx;j++)
				{
					if(now>=p)	(vis[i]+=p*vis[j])%=2,now=vis[j]+now/p,vis[j]=0;
//					cout<<now<<endl;
				}
			}
			vis[i]%=2;
		}
		int tmp=1;
		for(int i=0;i<=mx;i++)
		{
			tt[i]=tmp;
			(s[i]=s[max(0ll,i-1)]+tmp*v[i])%=mod;
			(tmp*=p)%=mod;
		}
		int flag=1;
		for(int i=mx;i>=0;i--)
		{
			if(vis[i]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(s[0]%mod+mod)%mod<<endl;
					break;
				}
				cout<<((tt[i]-s[i-1])%mod+mod)%mod<<endl;
				break;
			}
		}
		if(flag==1)	cout<<0<<endl;
	}

}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int ss=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') ss=ss*10+ch-'0',ch=getchar();
   return ss*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
//	if(kk[T])	return kk[T];
	int now=1,ty=T;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(ty);
	kk[ty]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		b.clear();
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
//				cout<<a[j]-a[j-1]<<endl;
				if(pow(p,a2-a[j-1])>1e6)	break;
				if(now>=ksm(p,a[j]-a[j-1]))
				{
					(vis[a1]+=p*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a[j-1]),vis[a2]=0;
					continue;
				}
				break;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a1]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];
			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}


#include<bits/stdc++.h>
#define maxn 1000005
#define int long long
#define mod 1000000007
using namespace std;

int n,p,vis[maxn],s[maxn],v[maxn],t;

inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

vector<int>	a;
vector<int> b;
int kk[maxn];

int ksm(int A,int T)
{
	if(kk[T])	return kk[T];
	int now=1;
	while(T)
	{
		if(T&1)	now=now*A%mod;
		A=A*A%mod;
		T>>=1;
	}
	b.push_back(T);
	kk[T]=now;
	return now;
}

signed main()
{
	t=read();
	int pppp=t;
	for(int ttt=1;ttt<=t;ttt++)
	{
		for(int i=0;i<a.size();i++)
		{
			int j=a[i];
			s[j]=vis[j]=v[j]=0;
		}
		for(int i=0;i<b.size();i++)
		{
			kk[b[i]]=0;
		}
		a.clear();
		n=read(),p=read();
		for(int i=1;i<=n;i++)
		{
			int x;
			x=read();
			vis[x]++;
			if(vis[x]==1)	a.push_back(x);
			v[x]++;
		}
		sort(a.begin(),a.end());
		if(p==1)
		{
			cout<<n%2<<endl;
			continue;
		}
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i],now=vis[a1];
			for(int j=i+1;j<a.size();j++)
			{
				int a2=a[j];
				if(now>=ksm(p,a2-a1))
				(vis[a1]+=ksm(p,a2-a1)*vis[a2])%=2,now=vis[a2]+now/ksm(p,a2-a1),vis[a2]=0;
			}
			vis[a1]%=2;
		}
		for(int i=0;i<a.size();i++)
		{
			int a1=a[i];

			(s[a[i]]=s[a[max(0ll,i-1)]]+ksm(p,a1)*v[a1])%=mod;
		}

		int flag=1;
		for(int i=a.size()-1;i>=0;i--)
		{
			int a1=a[i];

			if(vis[a1]==1)		//ÆæÊý
			{
				flag=0;
				if(i==0)
				{
					cout<<(ksm(p,a1))%mod<<'\n';
					break;
				}
				cout<<((ksm(p,a1)-s[a[i-1]])%mod+mod)%mod<<'\n';
				break;
			}
		}
		if(flag==1)	cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define P 1000000007
#define inf 999999999
using namespace std;
int T,n,p,a[1000001],np,nfl,ans,nx,pi;
int qread(){
	int nans=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		nans=nans*10+c-'0';
		c=getchar();
	}
	return nans;
}
int ksm(int x,int y){
	pi=0;
	int nans=1;
	for(;y;y>>=1){
		if(y&1){
			if(nans*x>=inf) pi=1;
			nans=nans*x%P;
		}
		x=x*x%P;
	}
	return nans;
}
signed main(){
	T=qread();
	while(T--){
		n=qread();p=qread();ans=nx=0;
		for(int i=1;i<=n;i++){
			a[i]=qread();
		}
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) printf("1\n");
			else printf("0\n");
			continue;
		}
		for(int i=n;i>=1;i--){
			if(nx>0){
				if(nx<inf) nx--;
				ans=(ans-ksm(p,a[i])+P)%P;
			}else{
				nx=1;
				ans=(ans+ksm(p,a[i]))%P;
			}
			if(a[i]!=a[i-1]&&nx<inf){
				nx=nx*ksm(p,a[i]-a[i-1])%P;
				if(pi) nx=inf;
			}
			if(nx>inf) nx=inf;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*

*/


#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define P 1000000007
using namespace std;
int T,n,p,a[1000001],np,nfl,ans,nx;
int qread(){
	int nans=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		nans=nans*10+c-'0';
		c=getchar();
	}
	return nans;
}
int ksm(int x,int y){
	int nans=1;
	for(;y;y>>=1){
		if(y&1) nans=nans*x%P;
		x=x*x%P;
	}
	return nans;
}
signed main(){
	T=qread();
	while(T--){
		memset(a,0,sizeof(a));
		n=qread();p=qread();ans=nfl=0;
		for(int i=1;i<=n;i++){
			np=qread();a[np]++;
		}
		if(p==1){
			if(n&1) printf("1\n");
			else printf("0\n");
			continue;
		}
		for(int i=1000000;i>=0;i--){
			if(nfl&&nx!=999999999) nx*=p;
			if(nx>1000000) nx=999999999;
			if(!a[i]) continue;
				if(!nfl){
					if(a[i]%2==0) continue;
					ans=(ans+ksm(p,i))%P;
					nfl=1;
					nx=1;
				}else{
					if(a[i]>=nx){
						if((a[i]-nx)&1) ans=(ksm(p,i))%P;
						else ans=0;
						nx=1;
					}else{
						if(nx!=999999999) nx-=a[i];
						ans=(ans-a[i]*ksm(p,i)%P+P)%P;
					}
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*

*/


#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define P 1000000007
#define inf 1000001
using namespace std;
int T,n,p,a[1000001],np,nfl,ans,nx,pi;
int qread(){
	int nans=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		nans=nans*10+c-'0';
		c=getchar();
	}
	return nans;
}
int ksm(int x,int y){
	pi=0;
	int nans=1;
	for(;y;y>>=1){
		if(y&1){
			if(nans*x>=inf) pi=1;
			nans=nans*x%P;
		}
		if(x*x>=inf) pi=1;
		x=x*x%P;
	}
	return nans;
}
signed main(){
	T=qread();
	while(T--){
		n=qread();p=qread();ans=nx=0;
		for(int i=1;i<=n;i++){
			a[i]=qread();
		}
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) printf("1\n");
			else printf("0\n");
			continue;
		}
		for(int i=n;i>=1;i--){
			if(nx>0){
				if(nx<inf) nx--;
				ans=(ans-ksm(p,a[i])+P)%P;
			}else{
				nx=1;
				ans=(ans+ksm(p,a[i]))%P;
			}
			if(a[i]!=a[i-1]&&nx<inf){
				nx=nx*ksm(p,a[i]-a[i-1]);
				if(pi||nx>=inf) nx=inf;
			}
			if(nx>inf) nx=inf;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*

*/


#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define P 1000000007
#define inf 999999999
using namespace std;
int T,n,p,a[1000001],np,nfl,ans,nx;
int qread(){
	int nans=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		nans=nans*10+c-'0';
		c=getchar();
	}
	return nans;
}
int ksm(int x,int y){
	int nans=1;
	for(;y;y>>=1){
		if(y&1) nans=nans*x%P;
		x=x*x%P;
	}
	return nans;
}
signed main(){
	T=qread();
	while(T--){
		n=qread();p=qread();ans=nx=0;
		for(int i=1;i<=n;i++){
			a[i]=qread();
		}
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) printf("1\n");
			else printf("0\n");
			continue;
		}
		for(int i=n;i>=1;i--){
			if(nx>0){
				if(nx<inf) nx--;
				ans=(ans-ksm(p,a[i])+P)%P;
			}else{
				nx=1;
				ans=(ans+ksm(p,a[i]))%P;
			}
			if(a[i]!=a[i-1]&&nx<inf) nx*=p;
			if(nx>inf) nx=inf;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*

*/


#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define P 1000000007
using namespace std;
int T,n,p,a[1000001],np,nfl,ans,nx;
int qread(){
	int nans=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		nans=nans*10+c-'0';
		c=getchar();
	}
	return nans;
}
int ksm(int x,int y){
	int nans=1;
	for(;y;y>>=1){
		if(y&1) nans=nans*x%P;
		x=x*x%P;
	}
	return nans;
}
signed main(){
	T=qread();
	while(T--){
		memset(a,0,sizeof(a));
		n=qread();p=qread();ans=nfl=0;
		for(int i=1;i<=n;i++){
			np=qread();a[np]++;
		}
		if(p==1){
			if(n&1) printf("1\n");
			else printf("0\n");
			continue;
		}
		for(int i=1000000;i>=0;i--){
			if(nfl) nx*=p;
			if(nx>1000000) nx=999999999;
			if(a[i]&1){
				if(!nfl){
					ans=(ans+ksm(p,i))%P;
					nfl=1;
					nx=1;
				}else{
					if(a[i]>=nx){
						if((a[i]-nx)&1) ans=(ksm(p,i))%P;
						else ans=0;
						nx=1;
					}else{
						nx-=a[i];
						ans=(ans-a[i]*ksm(p,i)%P+P)%P;
					}
				}
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*

*/


#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define P 1000000007
#define inf 1000001
using namespace std;
int T,n,p,a[1000001],np,nfl,ans,nx,pi;
int qread(){
	int nans=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		nans=nans*10+c-'0';
		c=getchar();
	}
	return nans;
}
int ksm(int x,int y){
	pi=0;
	int nans=1;
	for(;y;y>>=1){
		if(y&1){
			if(nans*x>=inf) pi=1;
			nans=nans*x%P;
		}
		if(x*x>=inf) pi=1;
		x=x*x%P;
	}
	return nans;
}
signed main(){
	T=qread();
	while(T--){
		n=qread();p=qread();ans=nx=0;
		for(int i=1;i<=n;i++){
			a[i]=qread();
		}
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) printf("1\n");
			else printf("0\n");
			continue;
		}
		for(int i=n;i>=1;i--){
			if(nx>0){
				if(nx<inf) nx--;
				ans=(ans-ksm(p,a[i])+P)%P;
			}else{
				nx=1;
				ans=(ans+ksm(p,a[i]))%P;
			}
			if(a[i]!=a[i-1]&&nx<inf){
				nx=nx*ksm(p,a[i]-a[i-1])%P;
				if(pi) nx=inf;
			}
			if(nx>inf) nx=inf;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*

*/


#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define P 1000000007
using namespace std;
int T,n,p,a[1000001],np,nfl,ans;
int qread(){
	int nans=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		nans=nans*10+c-'0';
		c=getchar();
	}
	return nans;
}
int ksm(int x,int y){
	int nans=1;
	for(;y;y>>=1){
		if(y&1) nans=nans*x%P;
		x=x*x%P;
	}
	return nans;
}
signed main(){
	T=qread();
	while(T--){
		memset(a,0,sizeof(a));
		n=qread();p=qread();ans=nfl=0;
		for(int i=1;i<=n;i++){
			np=qread();a[np]++;
		}
		if(p==1){
			for(int i=1000000;i>=0;i--){
			if(a[i]&1){
				if(!nfl) ans=(ans+ksm(p,i))%P;
				else ans=(ans-ksm(p,i)+P)%P;
				nfl=1-nfl;
			}
		}
		}else{
			for(int i=1000000;i>=0;i--){
			if(a[i]&1){
				if(!nfl) ans=(ans+ksm(p,i))%P;
				else ans=(ans-ksm(p,i)+P)%P;
				nfl=1;
			}
		}
		}

		printf("%lld\n",ans);
	}
	return 0;
}
/*

*/


#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define P 1000000007
#define inf 1000001
using namespace std;
int T,n,p,a[1000001],np,nfl,ans,nx,pi;
int qread(){
	int nans=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		nans=nans*10+c-'0';
		c=getchar();
	}
	return nans;
}
int ksm(int x,int y){
	pi=0;
	int nans=1;
	for(;y;y>>=1){
		if(y&1){
			if(nans*x>=inf) pi=1;
			nans=nans*x%P;
		}
		if(x*x>=inf) pi=1;
		x=x*x%P;
	}
	return nans;
}
signed main(){
	T=qread();
	while(T--){
		n=qread();p=qread();ans=nx=0;
		for(int i=1;i<=n;i++){
			a[i]=qread();
		}
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) printf("1\n");
			else printf("0\n");
			continue;
		}
		for(int i=n;i>=1;i--){
			if(nx>0){
				if(nx<inf) nx--;
				ans=(ans-ksm(p,a[i])+P)%P;
			}else{
				nx=1;
				ans=(ans+ksm(p,a[i]))%P;
			}
			if(a[i]!=a[i-1]&&nx<inf&&nx!=0){
				nx=nx*ksm(p,a[i]-a[i-1]);
				if(pi||nx>=inf) nx=inf;
			}
			if(nx>inf) nx=inf;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*

*/


#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define P 1000000007
#define inf 999999999
using namespace std;
int T,n,p,a[1000001],np,nfl,ans,nx;
int qread(){
	int nans=0;char c=getchar();while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		nans=nans*10+c-'0';
		c=getchar();
	}
	return nans;
}
int ksm(int x,int y){
	int nans=1;
	for(;y;y>>=1){
		if(y&1) nans=nans*x%P;
		x=x*x%P;
	}
	return nans;
}
signed main(){
	T=qread();
	while(T--){
		n=qread();p=qread();ans=nfl=0;
		for(int i=1;i<=n;i++){
			a[i]=qread();
		}
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) printf("1\n");
			else printf("0\n");
			continue;
		}
		for(int i=n;i>=1;i--){
			if(!nfl){
				nfl=1;
				ans=(ans+ksm(p,a[i]))%P;
				nx=1;
			}else{
				if(nx>0){
					nx--;
					ans=(ans-ksm(p,a[i])+P)%P;
				}else{
					nx=1;
					ans=ksm(p,a[i])%P;
				}
			}
			if(a[i]!=a[i-1]&&nx<inf) nx*=p;
			if(nx>inf) nx=inf;
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*

*/


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int arr[N], num1[N], num2[N];
set<int, greater<int>> dif, vis;
int n, p;

void check(int i) {
    vis.insert(i);
    if(num1[i] != num2[i]) dif.insert(i);
    else dif.erase(i);
}

void addnum1(int j) {
    int i = j;
    while(num1[i] == p - 1) {
        num1[i] = 0;
        check(i);
        i++;
    }
    num1[i]++;
    check(i);
}

void addnum2(int j) {
    int i = j;
    while(num2[i] == p - 1) {
        num2[i] = 0;
        check(i);
        i++;
    }
    num2[i]++;
    check(i);
}

void subnum1(int j) {
    int i = j;
    while(!num1[i]) {
        num1[i] = p - 1;
        check(i);
        i++;
    }
    num1[i]--;
    check(i);
}

void subnum2(int j) {
    int i = j;
    while(!num2[i]) {
        num2[i] = p - 1;
        check(i);
        i++;
    }
    num2[i]--;
    check(i);
}

bool ok() {
    if(!dif.size()) return 1;
    int i = *dif.begin();
    return num1[i] < num2[i];
}

void init() {
    for(auto i : vis) num1[i] = num2[i] = 0;
    vis.clear();
    dif.clear();
}

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        init();
        for(int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
            if(p > 1) addnum2(arr[i]);
        }
        sort(arr, arr + n);
        if(p == 1) {
            printf("%d\n", (n % 2));
            continue;
        }
        vector<int> res(n, 2);
        int ans = 0;
        for(int i = n - 1; i >= 0; i--) {
            addnum1(arr[i]);
            subnum2(arr[i]);
            if(ok()) {
                res[i] = 1;
                ans = (1ll * ans - fp(p, arr[i])) % mod;
            }
            else {
                subnum1(arr[i]);
                addnum2(arr[i]);
                ans = (1ll * ans + fp(p, arr[i])) % mod;
            }
        }
        if(ans < 0) ans += mod;
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int arr[N], dif[N];
set<int, greater<int>> on;
int n, p;

void add(int j) {
    int i = j;
    while(dif[i] == p - 1) {
        dif[i] = 0;
        on.erase(i);
        i++;
    }
    dif[i]++;
    on.insert(i);
}

bool sub(int j) {
    if(!on.size()) return 0;
    if(*on.begin() < j) return 0;
    int i = j;
    while(!dif[i]) {
        dif[i] = p - 1;
        on.insert(i);
        i++;
    }
    dif[i]--;
    if(!dif[i]) on.erase(i);
    else on.insert(i);
    return 1;
}

void init() {
    for(auto i : on) dif[i] = 0;
    on.clear();
}

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        init();
        int ans = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
            if(p > 1) add(arr[i]);
            ans = (1ll * ans + fp(p, arr[i])) % mod;
        }
        sort(arr, arr + n);
        if(p == 1) {
            printf("%d\n", (n % 2));
            continue;
        }
        for(int i = n - 1; i >= 0; i--) {
            bool bo = sub(arr[i]);
            if(sub(arr[i])) ans = (ans - 2ll * fp(p, arr[i]) % mod) % mod;
            else if(bo) add(arr[i]);
        }
        if(ans < 0) ans += mod;
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int n, p;

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        map<int, int, greater<int>> cnt;
        for(int i = 0; i < n; i++) {
            int x; scanf("%d", &x);
            cnt[x]++;
        }
        if(p == 1) {
            printf("%d\n", n % 2);
            continue;
        }
        int ans = 0;
        bool bo = 0;
        int dif = 0;
        int lst = -1;
        for(auto it : cnt) {
            if(lst < 0) {
                dif = it.second % 2;
                if(dif) ans = fp(p, it.first);
                lst = it.first;
                continue;
            }
            if(bo) ans = (ans - 1ll * it.second * fp(p, it.first) % mod) % mod;
            else {
                int delta = lst - it.first;
                if(!dif) delta = 0;
                for( ; 1ll * dif * p <= 1e6 && delta > 0; dif *= p, delta--);
                if(delta) {
                    bo = 1;
                    ans = (ans - 1ll * it.second * fp(p, it.first) % mod) % mod;
                }
                else {
                    int can = -min(dif, it.second);
                    if(abs(can) % 2 != it.second % 2) can++;
                    int x = (1ll * can + it.second) / 2;
                    ans = (ans + 1ll * x * fp(p, it.first) % mod) % mod;
                    ans = (ans - 1ll * (it.second - x) * fp(p, it.first) % mod) % mod;
                    dif += can;
                }
            }
            lst = it.first;
        }
        if(ans < 0) ans += mod;
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int arr[N], dif[N];
set<int, greater<int>> on;
set<int> vis;
vector<int> v;
int n, p;

void add(int j) {
    int i = j;
    while(dif[i] == p - 1) {
        dif[i] = 0;
        on.erase(i);
        vis.insert(i);
        i++;
    }
    dif[i]++;
    vis.insert(i);
    on.insert(i);
}

bool sub(int j) {
    if(!on.size()) return 0;
    if(*on.begin() < j) return 0;
    int i = j;
    while(!dif[i]) {
        dif[i] = p - 1;
        on.insert(i);
        vis.insert(i);
        i++;
    }
    dif[i]--;
    vis.insert(i);
    if(!dif[i]) on.erase(i);
    else on.insert(i);
    return 1;
}

void init() {
    for(auto i : vis) dif[i] = 0;
    vis.clear();
    on.clear();
}

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        init();
        for(int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
            if(p > 1) add(arr[i]);
        }
        sort(arr, arr + n);
        if(p == 1) {
            printf("%d\n", (n % 2));
            continue;
        }
        int ans = 0;
        for(int i = n - 1; i >= 0; i--) {
            bool ok = sub(arr[i] + 1);
            if(ok) ans = (1ll * ans - fp(p, arr[i])) % mod;
            else ans = (1ll * ans + fp(p, arr[i])) % mod;
        }
        if(ans < 0) ans += mod;
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int arr[N], dif[N];
set<int, greater<int>> on;
set<int> vis;
vector<int> v;
int n, p;

void add(int j) {
    int i = j;
    while(dif[i] == p - 1) {
        dif[i] = 0;
        on.erase(i);
        vis.insert(i);
        i++;
    }
    dif[i]++;
    vis.insert(i);
    on.insert(i);
}

bool sub(int j) {
    if(!on.size()) return 0;
    if(*on.begin() < j) return 0;
    int i = j;
    while(!dif[i]) {
        dif[i] = p - 1;
        on.insert(i);
        vis.insert(i);
        i++;
    }
    dif[i]--;
    vis.insert(i);
    if(!dif[i]) on.erase(i);
    else on.insert(i);
    return 1;
}

void init() {
    for(auto i : vis) dif[i] = 0;
    vis.clear();
    on.clear();
}

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        init();
        int ans = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
            if(p > 1) add(arr[i]);
            ans = (1ll * ans + fp(p, arr[i])) % mod;
        }
        sort(arr, arr + n);
        if(p == 1) {
            printf("%d\n", (n % 2));
            continue;
        }
        for(int i = n - 1; i >= 0; i--) {
            bool bo = sub(arr[i]);
            if(sub(arr[i])) ans = (ans - 2ll * fp(p, arr[i]) % mod) % mod;
            else if(bo) add(arr[i]);
        }
        printf("%d\n", abs(ans));
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int arr[N], dif[N];
set<int, greater<int>> on;
set<int> vis;
vector<int> v;
int n, p;

void add(int j) {
    int i = j;
    while(dif[i] == p - 1) {
        dif[i] = 0;
        on.erase(i);
        vis.insert(i);
        i++;
    }
    dif[i]++;
    vis.insert(i);
    on.insert(i);
}

bool sub(int j) {
    if(!on.size()) return 0;
    if(*on.begin() < j) return 0;
    int i = j;
    while(!dif[i]) {
        dif[i] = p - 1;
        on.insert(i);
        vis.insert(i);
        i++;
    }
    dif[i]--;
    vis.insert(i);
    if(!dif[i]) on.erase(i);
    else on.insert(i);
    return 1;
}

void init() {
    for(auto i : vis) dif[i] = 0;
    vis.clear();
    on.clear();
}

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        init();
        int ans = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
            if(p > 1) add(arr[i]);
            ans = (1ll * ans + fp(p, arr[i])) % mod;
        }
        sort(arr, arr + n);
        if(p == 1) {
            printf("%d\n", (n % 2));
            continue;
        }
        for(int i = n - 1; i >= 0; i--) {
            sub(arr[i]);
            if(sub(arr[i])) ans = (ans - 2ll * fp(p, arr[i]) % mod) % mod;
            else add(arr[i]);
        }
        printf("%d\n", abs(ans));
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int arr[N], dif[N];
set<int, greater<int>> on;
set<int> vis;
vector<int> v;
int n, p;

void add(int j) {
    int i = j;
    while(dif[i] == p - 1) {
        dif[i] = 0;
        on.erase(i);
        vis.insert(i);
        i++;
    }
    dif[i]++;
    vis.insert(i);
    on.insert(i);
}

bool sub(int j) {
    if(!on.size()) return 0;
    if(*on.begin() < j) return 0;
    int i = j;
    while(!dif[i]) {
        dif[i] = p - 1;
        on.insert(i);
        vis.insert(i);
        i++;
    }
    dif[i]--;
    vis.insert(i);
    if(!dif[i]) on.erase(i);
    else on.insert(i);
    return 1;
}

void init() {
    for(auto i : vis) dif[i] = 0;
    vis.clear();
    on.clear();
}

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        init();
        int ans = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
            if(p > 1) add(arr[i]);
            ans = (1ll * ans + fp(p, arr[i])) % mod;
        }
        sort(arr, arr + n);
        if(p == 1) {
            printf("%d\n", (n % 2));
            continue;
        }
        for(int i = n - 1; i >= 0; i--) {
            bool bo = sub(arr[i]);
            if(bo && sub(arr[i])) ans = (ans - 2ll * fp(p, arr[i]) % mod) % mod;
            else if(bo) add(arr[i]);
        }
        printf("%d\n", abs(ans));
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int n, p;

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        map<int, int, greater<int>> cnt;
        for(int i = 0; i < n; i++) {
            int x; scanf("%d", &x);
            cnt[x]++;
        }
        int ans = 0;
        bool bo = 0;
        int dif = 0;
        int lst = -1;
        for(auto it : cnt) {
            if(lst < 0) {
                dif = it.second % 2;
                if(dif) ans = fp(p, it.first);
                lst = it.first;
                continue;
            }
            if(bo) ans = (ans - 1ll * it.second * fp(p, it.first) % mod) % mod;
            else {
                int delta = lst - it.first;
                for( ; 1ll * dif * p <= 1e6 && delta > 0; dif *= p, delta--);
                if(delta) {
                    bo = 1;
                    ans = (ans - 1ll * it.second * fp(p, it.first) % mod) % mod;
                }
                else {
                    int can = -min(dif, it.second);
                    if(abs(can) % 2 != it.second % 2) can++;
                    int x = (1ll * can + it.second) / 2;
                    ans = (ans + 1ll * x * fp(p, it.first) % mod) % mod;
                    ans = (ans - 1ll * (it.second - x) * fp(p, it.first) % mod) % mod;
                    dif += can;
                }
            }
            lst = it.first;
        }
        if(ans < 0) ans += mod;
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int arr[N], dif[N];
set<int, greater<int>> on;
int n, p;

void add(int j) {
    int i = j;
    while(dif[i] == p - 1) {
        dif[i] = 0;
        on.erase(i);
        i++;
    }
    dif[i]++;
    on.insert(i);
}

bool sub(int j) {
    if(!on.size()) return 0;
    if(*on.begin() < j) return 0;
    int i = j;
    while(!dif[i]) {
        dif[i] = p - 1;
        on.insert(i);
        i++;
    }
    dif[i]--;
    if(!dif[i]) on.erase(i);
    else on.insert(i);
    return 1;
}

void init() {
    for(auto i : on) dif[i] = 0;
    on.clear();
}

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        init();
        int ans = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
            if(p > 1) add(arr[i]);
            ans = (1ll * ans + fp(p, arr[i])) % mod;
        }
        sort(arr, arr + n);
        if(p == 1) {
            printf("%d\n", (n % 2));
            continue;
        }
        for(int i = n - 1; i >= 0; i--) {
            bool bo = sub(arr[i]);
            if(sub(arr[i])) ans = (ans - 2ll * fp(p, arr[i]) % mod) % mod;
            else if(bo) add(arr[i]);
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <numeric>
#include <chrono>
#include <bitset>
#include <tuple>
#include <queue>
#include <map>
#include <cstring>
#include <cassert>
#include <math.h>
using namespace std;

const int N = 1e6 + 10, mod = 1e9 + 7;

int n, p;

int fp(int a, int b) {
    int ret = 1;
    while(b) {
        if(b & 1) ret = 1ll * ret * a % mod;
        b >>= 1;
        a = 1ll * a * a % mod;
    }
    return ret;
}

int main() {
    // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tc = 1;
    scanf("%d", &tc);
    for(int cn = 1; cn <= tc; cn++) {
        scanf("%d%d", &n, &p);
        map<int, int, greater<int>> cnt;
        for(int i = 0; i < n; i++) {
            int x; scanf("%d", &x);
            cnt[x]++;
        }
        int ans = 0;
        bool bo = 0;
        int dif = 0;
        int lst = -1;
        for(auto it : cnt) {
            if(lst < 0) {
                dif = it.second % 2;
                if(dif) ans = fp(p, it.first);
                lst = it.first;
                continue;
            }
            if(bo) ans = (ans - 1ll * it.second * fp(p, it.first) % mod) % mod;
            else {
                int delta = lst - it.first;
                if(!dif) delta = 0;
                for( ; 1ll * dif * p <= 1e6 && delta > 0; dif *= p, delta--);
                if(delta) {
                    bo = 1;
                    ans = (ans - 1ll * it.second * fp(p, it.first) % mod) % mod;
                }
                else {
                    int can = -min(dif, it.second);
                    if(abs(can) % 2 != it.second % 2) can++;
                    int x = (1ll * can + it.second) / 2;
                    ans = (ans + 1ll * x * fp(p, it.first) % mod) % mod;
                    ans = (ans - 1ll * (it.second - x) * fp(p, it.first) % mod) % mod;
                    dif += can;
                }
            }
            lst = it.first;
        }
        if(ans < 0) ans += mod;
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod1 = 1e9 + 7, mod2 = 1e9 + 3;
int pow(int a, int b, int mod)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		for (int i = 1; i <= n; ++i)
		{
			k[i] = read();
		}
		sort(k + 1, k + n + 1);
		long long ans1 = 0, ans2 = 0;
		for (int i = n; i; --i)
		{
			if(!ans1 && !ans2)
			{
				ans1 = (ans1 + pow(p, k[i], mod1)) % mod1;
				ans2 = (ans2 + pow(p, k[i], mod2)) % mod2;
			}
			else
			{
				ans1 = (ans1 - pow(p, k[i], mod1) + mod1) % mod1;
				ans2 = (ans2 - pow(p, k[i], mod2) + mod2) % mod2;
			}
		 }
		printf("%lld\n", ans1);
		for (int i = 1; i <= n; ++i)
		{
			k[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod = 1e9 + 7;
int pow(int a, int b)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
//	freopen("1.in", "r", stdin);
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		for (int i = 1; i <= n; ++i)
		{
				k[i] = read();
		}
		if(p == 1)
		{
			printf("%d\n", n % 2);
		}
		else
		{
			sort(k + 1, k + n + 1);
			int cnt = 0, cmax = -1, i;
			for (i = n; i; --i)
			{
				if(cmax == -1)
				{
					cmax = k[i];
				}
				if(k[i] == cmax)
				{
					++cnt;
				}
				else
				{
					if(cnt % 2 == 0)
					{
						cmax = -1;
						cnt = 0;
					}
					else
					{
						break;
					}
				}
			}
			long long ans1 = pow(p, cmax), ans2 = 0;
			for (int j = i; j; --j)
			{
				ans2 += pow(p, k[j]);
				ans2 %= mod;
			}
			printf("%lld\n", (int)abs(ans1 - ans2) % mod);
		}
		for (int i = 1; i <= n; ++i)
		{
			k[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod = 1e9 + 7;
int pow(int a, int b)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
//	freopen("1.in", "r", stdin);
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		for (int i = 1; i <= n; ++i)
		{
			k[i] = read();
		}
		if(p == 1)
		{
			printf("%d\n", n % 2);
		}
		else
		{
			int cmax = -1;
			sort(k + 1, k + n + 1);
			for (int i = 1; i <= n; ++i)
			{
				h[k[i]]++;
			}
			for (int i = n; i; --i)
			{
				if(cmax == -1)
				{
					if(h[k[i]] % 2 == 1)
					{
						cmax = k[i];
						h[cmax] = 1;
						break;
					}
					else
					{
						h[k[i]] = 0;
					}
				}
			}
			for (int i = 0; i <= cmax; ++i)
			{
				h[i + 1] += h[i] / p;
				h[i] %= p;
			}
			while(h[cmax + 1])
			{
				++cmax;
			}
			long long ans1 = pow(p, cmax), ans2 = 0;
			for (int i = 0; i < cmax; ++i)
			{
				ans2 += h[i] * pow(p, i);
				ans2 %= mod;
			}
			printf("%d\n", (int)abs(ans1 - ans2) % mod);
			for (int i = 1; i <= n; ++i)
			{
				k[i] = 0;
			}
			for (int i = 0; i <= cmax; ++i)
			{
				h[i] = 0;
			}
		}
	}
	return 0;
}


#pragma GCC optimize(3)
#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod = 1e9 + 7;
int pow(int a, int b)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
//freopen("1.in", "r", stdin);
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		for (int i = 1; i <= n; ++i)
		{
			k[i] = read();
		}
		if(p == 1)
		{
			printf("%d\n", n % 2);
		}
		else
		{
			int cmax = -1;
			sort(k + 1, k + n + 1);
			for (int i = 1; i <= n; ++i)
			{
				h[k[i]]++;
			}
			for (int i = n; i; --i)
			{
				if(cmax == -1)
				{
					if(h[k[i]] % 2 == 1)
					{
						cmax = k[i];
						h[cmax] = 1;
						break;
					}
					else
					{
						h[k[i]] = 0;
					}
				}
			}
			for (int i = 0; i <= cmax; ++i)
			{
				h[i + 1] += h[i] / p;
				h[i] %= p;
			}
			while(h[cmax + 1])
			{
				++cmax;
			}
			if(cmax == -1)
			{
				puts("0");
			}
			else
			{
				long long ans1 = 0, ans2 = 0;
				for (int i = cmax; i >= 0; --i)
				{
					if(h[i] % 2 == 1)
					{
						ans1 = pow(p, i);
						for (int j = 0; j < i; ++j)
						{
							ans2 += 1ll * h[j] * pow(p, j);
							ans2 %= mod;
						}
						break;
					}
				}
				printf("%d\n", (int)abs(ans1 - ans2) % mod);
				for (int i = 1; i <= n; ++i)
				{
					k[i] = 0;
				}
				for (int i = 0; i <= cmax; ++i)
				{
					h[i] = 0;
				}
			}
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod1 = 1e9 + 7, mod2 = 3515146523;
int pow(int a, int b, int mod)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		for (int i = 1; i <= n; ++i)
		{
			k[i] = read();
		}
		sort(k + 1, k + n + 1);
		long long ans1 = 0, ans2 = 0;
		for (int i = n; i; --i)
		{
			if(!ans1 && !ans2)
			{
				ans1 = (ans1 + pow(p, k[i], mod1)) % mod1;
				ans2 = (ans2 + pow(p, k[i], mod2)) % mod2;
			}
			else
			{
				ans1 = (ans1 - pow(p, k[i], mod1) + mod1) % mod1;
				ans2 = (ans2 - pow(p, k[i], mod2) + mod2) % mod2;
			}
		 }
		printf("%lld\n", ans1);
		for (int i = 1; i <= n; ++i)
		{
			k[i] = 0;
		}
	}
	return 0;
}