#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

for _ in range(inp()):
    n = inp()
    s = list(input().strip())
    ans = 0
    for i in range(n-1):
        #print(s)
        if s[i-1] == s[i] and s[i] == s[i+1]:
            s[i] = 'R' if s[i-1] == 'L' else 'L'
            #print("*",s)
            ans += 1
    print(ans)


#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

for _ in range(inp()):
    n = inp()
    s = input().strip()
    prev = -1
    x = []
    for i in range(n):
        if s[i] != prev:
            x.append(0)
        x[-1] += 1
        prev = s[i]
    ans = 0
    if len(x) == 1:
        ans += (x[0]-1)//3 + 1
    else:
        if s[0] == s[-1] and len(x) > 1:
            t = x.pop()
            x[0] += t
        for i in x:
            ans += (i-3)//3 +1
    print(ans)


#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

for _ in range(inp()):
    n = inp()
    s = input().strip()
    prev = -1
    x = []
    for i in range(n):
        if s[i] != prev:
            x.append(1)
        else:
            x[-1] += 1
        prev = s[i]
    ans = 0
    #print(x)
    if len(x) == 1:
        ans += (n-2-1)//2 +1
    else:
        if s[0] == s[-1]:
            x[0] += x[-1]
            x.pop()
        for i in x:
            ans += (i-2-1)//2 +1
    print(ans)


#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

for _ in range(inp()):
    n = inp()
    s = input().strip()
    prev = -1
    x = []
    for i in range(n):
        if s[i] != prev:
            x.append(0)
        x[-1] += 1
        prev = s[i]
    ans = 0
    if len(x) == 1:
        ans += (x[0]-1)//3 + 1
    else:
        if s[0] == s[-1] and len(x) > 1:
            t = x.pop()
            x[0] += t
        for i in x:
            ans += (i-3)//2 +1
    print(ans)


z,zz=input,lambda:list(map(int,z().split()));
szz,dgraphs,mod=lambda:sorted(zz()),{},10**9+7
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2))
def prime(x):
    p=ceil(x**.5)+1
    for i in range(2,p):
        if x%i==0 and x!=2:return 0
    return 1
def dfs(u,visit,graph):
    visit[u]=True
    for i in graph[u]:
        if not visit[i]:
            dfs(i,visit,graph)

###########################---Test-Case---#################################
"""

"""
###########################---START-CODING---##############################

num=int(z())

for i in range(  num ):
    n=int(z())
    l=z()

    find=-1
    for i in range(n-1):
        if l[i]!=l[i+1]:
            find=i+1
            break
    if find==-1:
        print(ceil(n/3))
        continue

    cnt=1
    res=0
    for i in range(find,find+n):
        if l[i%n]==l[(i+1)%n]:
            cnt+=1
        else:
            res+=cnt//3
            cnt=1
    print(res)


z,zz=input,lambda:list(map(int,z().split()));
szz,dgraphs,mod=lambda:sorted(zz()),{},10**9+7
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2))
def prime(x):
    p=ceil(x**.5)+1
    for i in range(2,p):
        if x%i==0 and x!=2:return 0
    return 1
def dfs(u,visit,graph):
    visit[u]=True
    for i in graph[u]:
        if not visit[i]:
            dfs(i,visit,graph)

###########################---Test-Case---#################################
"""

"""
###########################---START-CODING---##############################

num=int(z())

for i in range(  num ):
    n=int(z())
    l=z()

    find=-1
    for i in range(n-1):
        if l[i]!=l[i+1]:
            find=i+1
            break
    if find==-1:
        print((-1)*(-n//3))
        continue

    cnt=1
    res=0
    for i in range(find,find+n):
        if l[i%n]==l[(i+1)%n]:
            cnt+=1
        else:
            res+=cnt//3
            cnt=1
    print(res)


z,zz=input,lambda:list(map(int,z().split()));
szz,dgraphs,mod=lambda:sorted(zz()),{},10**9+7
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2))
def prime(x):
    p=ceil(x**.5)+1
    for i in range(2,p):
        if x%i==0 and x!=2:return 0
    return 1
def dfs(u,visit,graph):
    visit[u]=True
    for i in graph[u]:
        if not visit[i]:
            dfs(i,visit,graph)

###########################---Test-Case---#################################
"""

"""
###########################---START-CODING---##############################

num=int(z())

for i in range(  num ):
    n=int(z())
    l=z()
    fi=-1
    for i in range(n-1):
        if l[i]!=l[i+1]:
            fi=i+1
            break
    if fi==-1:
        print(n//3)
        continue
    cnt=0
    res=0
    for i in range(fi,fi+n):
        if l[i%n]!=l[(i+1)%n]:
            cnt+=1
        else:
            res+=cnt//3
            cnt=1
        res=cnt//3
    print(res)


for i in range(int(input())):
    n=int(input())
    arr=list(input())
    br=[0]*n
    ans=0
    j=0
    while j<(n-1):
        if(arr[j-1]==arr[j]==arr[j+1]):
            if(arr[j]=="R"):
                arr[j]="L"
            else:
                arr[j]="R"
            ans+=1
            j+=3
        else:
            j+=1
    j=n-1
    if (arr[n-2] == arr[n-1] == arr[0]):
        if (arr[j] == "R"):
            arr[j] = "L"
        else:
            arr[j] = "R"
        ans += 1
    print(ans)


for i in range(int(input())):
    n=int(input())
    arr=list(input())
    br=[0]*n
    ans=0
    for j in range(0,n-2):
        if(arr[j-1]==arr[j]==arr[j+1]):
            if(arr[j]=="R"):
                arr[j]="L"
            else:
                arr[j]="R"
            ans+=1
    j=n-1
    if (arr[n-2] == arr[n-1] == arr[0]):
        if (arr[j] == "R"):
            arr[j] = "L"
        else:
            arr[j] = "R"
        ans += 1
    print(ans)


import math
for i in range(int(input())):
    n=int(input())
    arr=list(input())
    ans=0
    j=0;z=0
    for i in range(n-1,-1,-1):
        if(arr[i]!=arr[0]):
            z=i
            break
    if(z==0):
        print(math.ceil(n/3))
    else:
        br=arr[z+1:]
        arr=br+arr[:z+1]
        ct=1
        for i in range(1,n):
            if(arr[i]!=arr[i-1]):
                ans+=(ct//3)
                ct=0
            ct+=1
        ans+=(ct//3)
        print(ans)


import math

T = int(input())

#lets = 'abcdefghijklmnopqrstuvwxyz'
#key = {lets[i]:i for i in range(26)}

for i in range(T):
  n = int(input())
  #n,k = map(int, input().split())
  #a = list(map(int,list(input().split())))
  a = list(input())
  good = ['RRL','RLL','LRR','LLR','RLR','LRL']

  ans = 0
  for i in range(n):
    if a[(i-1)%n]+a[i%n]+a[(i+1)%n] not in good:
      a[i] = {'R':'L','L':'R'}[a[i]]
      ans += 1

  print(ans)


import math

T = int(input())

#lets = 'abcdefghijklmnopqrstuvwxyz'
#key = {lets[i]:i for i in range(26)}

for i in range(T):
  n = int(input())
  #n,k = map(int, input().split())
  #a = list(map(int,list(input().split())))
  a = list(input())
  if a.count(a[0]) == n:
    print(1+(n-1)//3)
    continue

  good = ['RRL','RLL','LRR','LLR','RLR','LRL']

  ans = 0
  cv = a[0]
  c = 0
  f = 1
  fc = 0
  for i in range(n):
    if a[i] == cv:
      c += 1
    else:
      if not f:
        ans += c//3
      else:
        f = 0
        fc = c
      c = 1
      cv = a[i]
    #print(c,cv)

  if a[0] == a[-1]:
    ans += (fc + c)//3
  else:
    ans += c//3
    ans += fc//3

  print(ans)


import math

T = int(input())

#lets = 'abcdefghijklmnopqrstuvwxyz'
#key = {lets[i]:i for i in range(26)}

for i in range(T):
  n = int(input())
  #n,k = map(int, input().split())
  #a = list(map(int,list(input().split())))
  a = list(input())
  if a.count(a[0]) == n:
    print(1+(n-1)//3)
    continue

  good = ['RRL','RLL','LRR','LLR','RLR','LRL']

  ans = 0
  cv = a[0]
  c = 0
  f = 1
  fc = 0
  for i in range(n):
    if a[i] == cv:
      c += 1
    else:
      if not f:
        ans += c//3
      else:
        f = 0
        fc = c
      c = 1
      cv = a[i]
    #print(c)

  if a[0] == a[-1]:
    ans += (fc + c)//3
  else:
    ans += c//3

  print(ans)


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

for _ in range(r()):
    n = r()
    a = input()
    ans = True
    for i in range(1,n):
        if a[i]!=a[i-1]:
            ans=False
    if ans:
        if n <= 2:
            print(0)
        elif n==3:
            print(1)
        else:
            print((n+2)//3)
    else:
        b = [1]
        for i in range(1,n):
            if a[i]==a[i-1]:
                b[-1]+=1
            else:
                b.append(1)
        if a[0]==a[-1]:
            b[0]+=b[-1]
            b = b[:-1]
        b.sort(reverse=True)
        i=0
        cnt=0
        while i<len(b) and b[i]>=3:
            cnt+=b[i]//3
            i+=1
        print(cnt)


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

for _ in range(r()):
    n = r()
    a = input()
    ans = True
    for i in range(1,n):
        if a[i]!=a[i-1]:
            ans=False
    if ans:
        print((n+2)//3)
    else:
        b = [1]
        for i in range(1,n):
            if a[i]==a[i-1]:
                b[-1]+=1
            else:
                b.append(1)
        if a[0]==a[-1]:
            b[0]+=b[-1]
        b = b[:-1]
        b.sort(reverse=True)
        i=0
        cnt=0
        while i<len(b) and b[i]>=3:
            cnt+=b[i]
            i+=1
        print(cnt//3)


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

for _ in range(r()):
    n = r()
    a = input()
    ans = True
    for i in range(1,n):
        if a[i]!=a[i-1]:
            ans=False
    if ans:
        if n <= 2:
            print(0)
        elif n==3:
            print(1)
        else:
            print((n+2)//3)
    else:
        b = [1]
        for i in range(1,n):
            if a[i]==a[i-1]:
                b[-1]+=1
            else:
                b.append(1)
        if a[0]==a[-1]:
            b[0]+=b[-1]
        b = b[:-1]
        b.sort(reverse=True)
        i=0
        cnt=0
        while i<len(b) and b[i]>=3:
            cnt+=b[i]//3
            i+=1
        print(cnt)


#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int MAXN = 1e6+1;
int a[MAXN]; ll P[MAXN];
int main(int argc, const char * argv[]){
    ios::sync_with_stdio(false);
    int T, n, p; ll s, v; cin >> T;
    for(int t = 1; t <= T; t++){
        cin >> n >> p;
        for(int i = 0; i < n; i++)
            cin >> a[i];
        if(p == 1){
            cout << (n%2) << endl;
            continue;
        }
        sort(a, a+n, greater<int>());
        P[0] = 1;
        for(int i = 1; i <= a[0]; i++){
            P[i] = P[i-1]*p; P[i] = P[i]%MOD;
        }
        // v is the exact value of s/p^k (k = a[i] current exponent)
        // s here is just the mod result, not the actual s
        s = P[a[0]]; v = 1; bool update = true;
        for(int i = 1; i < n; i++){
            if(!update || v > 0){
                s -= P[a[i]];
                s = (s+MOD)%MOD;
            }else{
                s += P[a[i]];
                s = s%MOD;
            }
            if(update){
                // update v
                if(v > n){
                    update = !update;
                }else{
                    bool plus = (v == 0);
                    // Precheck if v > n early
                    // newV = v* p^(a[i-1]-a[i]) +- 1
                    for(int j = 1; j <= a[i-1]-a[i]; j++){
                        if(v > n/p + 1){
                            update = !update; break;
                        }
                        v *= p;
                    }
                    v += (plus)?1:-1;
                }
            }
        }
        cout << s << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int MAXN = 1e6+1;
int a[MAXN]; ll P[MAXN];
int main(int argc, const char * argv[]){
    ios::sync_with_stdio(false);
    int T, n, p, v; ll s; cin >> T;
    for(int t = 1; t <= T; t++){
        cin >> n >> p;
        for(int i = 0; i < n; i++)
            cin >> a[i];
        if(p == 1){
            cout << (n%2) << endl;
            continue;
        }
        sort(a, a+n, greater<int>());
        P[0] = 1;
        for(int i = 1; i <= a[0]; i++){
            P[i] = P[i-1]*p; P[i] = P[i]%MOD;
        }
        // v is the exact value of s/p^k (k = a[i] current exponent)
        // s here is just the mod result, not the actual s
        s = P[a[0]]; v = 1; bool update = true;
        for(int i = 1; i < n; i++){
            if(!update || v > 0){
                s -= P[a[i]];
                s = (s%MOD+MOD)%MOD;
            }else{
                s += P[a[i]];
                s = (s%MOD+MOD)%MOD;
            }
            if(update){
                // update v
                if(v > n){
                    update = !update;
                }else{
                    bool plus = !v;
                    // Precheck if v > n early
                    // newV = v* p^(a[i-1]-a[i]) +- 1
                    for(int i = 1; i <= a[i-1]-a[i]; i++){
                        if(v > n/p + 1){
                            update = !update; break;
                        }
                        v *= p;
                    }
                    v += (plus)?1:-1;
                }
            }
        }
        cout << s << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int MAXN = 1e6+1;
int a[MAXN]; ll P1[21];

ll calPow(int p, int n){
    ll r = 1;
    if(n == 0)
        return r;
    int ind = 20;
    while(ind >= 0 && !(n & (1 << ind)))
        ind--;
    for(int i = ind; i >= 0; i--)
        if(n & (1 << i))
            r = (r*P1[i])%MOD;
    return r;
}
int main(int argc, const char * argv[]){
    ios::sync_with_stdio(false);
    int T, n, p; ll s, v; cin >> T;
    for(int t = 1; t <= T; t++){
        cin >> n >> p;
        P1[0] = p;
        for(int i = 1; i <= 20; i++)
            P1[i] = (P1[i-1]*P1[i-1])%MOD;
        for(int i = 0; i < n; i++)
            cin >> a[i];
        if(p == 1){
            cout << (n%2) << endl;
            continue;
        }
        sort(a, a+n, greater<int>());
        int ind = 0;
        while(ind < n-1 && a[ind] == a[ind+1])
            ind += 2;
        if(ind == n){
            cout << 0 << endl; continue;
        }
        unordered_map<int, ll> P;
        for(int i = ind; i < n; i++)
            if(!P.count(a[i]))
                P[a[i]] = calPow(p, a[i]);
        // v is the exact value of s/p^k (k = a[i] current exponent)
        // s here is just the mod result, not the actual s
        s = P[a[ind++]]; v = 1; bool update = true;
        for(; ind < n; ind++){
            if(!update || v > 0){
                s -= P[a[ind]];
                s = (s+MOD)%MOD;
            }else{
                s += P[a[ind]];
                s = s%MOD;
            }
            if(update){
                // update v
                if(v > n){
                    update = !update;
                }else{
                    bool plus = (v == 0);
                    // Precheck if v > n early
                    // newV = v* p^(a[i-1]-a[i]) +- 1
                    for(int j = 1; j <= a[ind-1]-a[ind]; j++){
                        if(v > n/p + 1){
                            update = !update; break;
                        }
                        v *= p;
                    }
                    v += (plus)?1:-1;
                }
            }
        }
        cout << s << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int MAXN = 1e6+1;
int a[MAXN]; ll P1[3];

ll calPow(int p, int n){
    if(n == 0)
        return 1;
    ll r = calPow(p, n/3);
    return (((r*r)%MOD*r)%MOD*P1[n%3])%MOD;
}
int main(int argc, const char * argv[]){
    ios::sync_with_stdio(false);
    int T, n, p; ll s, v; cin >> T;
    for(int t = 1; t <= T; t++){
        unordered_map<int, ll> P;
        cin >> n >> p;
        P1[0] = 1; P1[1] = p; P1[2] = ((ll)p*p)%MOD;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            if(!P.count(a[i]))
                P[a[i]] = calPow(p, a[i]);
        }
        if(p == 1){
            cout << (n%2) << endl;
            continue;
        }
        sort(a, a+n, greater<int>());
        // v is the exact value of s/p^k (k = a[i] current exponent)
        // s here is just the mod result, not the actual s
        s = P[a[0]]; v = 1; bool update = true;
        for(int i = 1; i < n; i++){
            if(!update || v > 0){
                s -= P[a[i]];
                s = (s+MOD)%MOD;
            }else{
                s += P[a[i]];
                s = s%MOD;
            }
            if(update){
                // update v
                if(v > n){
                    update = !update;
                }else{
                    bool plus = (v == 0);
                    // Precheck if v > n early
                    // newV = v* p^(a[i-1]-a[i]) +- 1
                    for(int j = 1; j <= a[i-1]-a[i]; j++){
                        if(v > n/p + 1){
                            update = !update; break;
                        }
                        v *= p;
                    }
                    v += (plus)?1:-1;
                }
            }
        }
        cout << s << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int MAXN = 1e6+1;
int a[MAXN]; ll P1[21];
ll calPow(int p, int n){
    if(n == 0)
        return 1;
    ll r = calPow(p, n/2);
    r = (r*r)%MOD;
    if(n%2){
        r = (r*p)%MOD;
    }
    return r;
}
int main(int argc, const char * argv[]){
    ios::sync_with_stdio(false);
    int T, n, p; ll s, v; cin >> T;
    // The map m stores all computed results over all test cases for faster runtimt
    unordered_map<ll, ll> m;
    for(int t = 1; t <= T; t++){
        cin >> n >> p;
        for(int i = 0; i < n; i++)
            cin >> a[i];
        if(p == 1){
            cout << (n%2) << endl;
            continue;
        }
        sort(a, a+n, greater<int>());
        int ind = 0;
        while(ind < n-1 && a[ind] == a[ind+1])
            ind += 2;
        if(ind == n){
            cout << 0 << endl; continue;
        }
        for(int i = ind; i < n; i++)
            if(!m.count((ll)MAXN*p+a[i]))
                m[(ll)MAXN*p+a[i]] = calPow(p, a[i]);
        // v is the exact value of s/p^k (k = a[i] current exponent)
        // s here is just the mod result, not the actual s
        s = m[(ll)MAXN*p+a[ind]]; v = 1;
        bool update = true; ind++;
        for(; ind < n; ind++){
            ll tmp = (ll)MAXN*p+a[ind];
            if(!update || v > 0){
                s -= m[tmp];
                s = (s+MOD)%MOD;
            }else{
                s += m[tmp];
                s = s%MOD;
            }
            if(update){
                // update v
                if(v > n){
                    update = !update;
                }else{
                    bool plus = (v == 0);
                    // Precheck if v > n early
                    // newV = v* p^(a[i-1]-a[i]) +- 1
                    for(int j = 1; j <= a[ind-1]-a[ind]; j++){
                        if(v > n/p + 1){
                            update = !update; break;
                        }
                        v *= p;
                    }
                    v += (plus)?1:-1;
                }
            }
        }
        cout << s << endl;
    }
    return 0;
}


#include <iostream>

using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int MAXN = 1e6+1; ll P[MAXN];
int a[MAXN], tmp[MAXN];// exponent, tmp for exponent
int main(int argc, const char * argv[]){
    ios::sync_with_stdio(false);
    int T, n, p, M; cin >> T;
    for(int t = 1; t <= T; t++){
        cin >> n >> p;
        M = 0;
        for(int i = 0; i < n; i++){
            cin >> tmp[i];
            M = max(M, tmp[i]);
        }
        for(int i = 0; i <= M; i++)
            a[i] = 0;
        for(int i = 0; i < n; i++)
            a[tmp[i]]++;
        //cout << "A[M]: " << a[M] << endl;
        while(M >= 0 && a[M]%2 == 0)
            M--;
        if(M < 0){
            cout << 0 << endl; continue;
        }
        // Transfer to M
        for(int i = 0; i < M; i++){
            if(a[i] >= p){
                a[i+1] += a[i]/p;
                a[i] = a[i]%p;
            }
        }
        int ind = M;
        //cout << M << ", " << a[M] << endl;
        while(ind >= 0 && a[ind]%2 == 0)
            ind--;
        if(ind < 0){
            cout << 0 << endl; continue;
        }
        ll sum = 0;
        for(int i = 0; i <= ind-1; i++){
            P[i] = (i)? ((P[i-1]*p)%MOD):1;
            if(!a[i])
                continue;
            //cout << i << ", " << a[i];
            sum += (P[i]*a[i])%MOD; sum = sum%MOD;
        }
        sum = ((ind)?(P[ind-1]*p)%MOD:1)-sum; sum = (sum%MOD+MOD)%MOD;
        cout << sum << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
const int MAXN = 1e6+1;
int a[MAXN]; ll P1[21];
ll calPow(int p, int n){
    if(n == 0)
        return 1;
    ll r = calPow(p, n/2);
    r = (r*r)%MOD;
    if(n%2){
        r = (r*p)%MOD;
    }
    return r;
}
int main(int argc, const char * argv[]){
    ios::sync_with_stdio(false);
    int T, n, p; ll s, v; cin >> T;
    // The map m stores all computed results over all test cases for faster runtimt
    unordered_map<ll, ll> m;
    for(int t = 1; t <= T; t++){
        cin >> n >> p;
        for(int i = 0; i < n; i++)
            cin >> a[i];
        if(p == 1){
            cout << (n%2) << endl;
            continue;
        }
        sort(a, a+n, greater<int>());
        int ind = 0;
        while(ind < n-1 && a[ind] == a[ind+1])
            ind += 2;
        if(ind == n){
            cout << 0 << endl; continue;
        }
        for(int i = ind; i < n; i++)
            if(!m.count((ll)a[i]*MAXN+p))
                m[(ll)a[i]*MAXN+p] = calPow(p, a[i]);
        // v is the exact value of s/p^k (k = a[i] current exponent)
        // s here is just the mod result, not the actual s
        s = m[(ll)a[ind]*MAXN+p]; v = 1;
        bool update = true; ind++;
        for(; ind < n; ind++){
            ll tmp = (ll)a[ind]*MAXN+p;
            if(!update || v > 0){
                s -= m[tmp];
                s = (s+MOD)%MOD;
            }else{
                s += m[tmp];
                s = s%MOD;
            }
            if(update){
                // update v
                if(v > n){
                    update = !update;
                }else{
                    bool plus = (v == 0);
                    // Precheck if v > n early
                    // newV = v* p^(a[i-1]-a[i]) +- 1
                    for(int j = 1; j <= a[ind-1]-a[ind]; j++){
                        if(v > n/p + 1){
                            update = !update; break;
                        }
                        v *= p;
                    }
                    v += (plus)?1:-1;
                }
            }
        }
        cout << s << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 1001110;
int mod1 = 1e9+7;
int mod2 = 1598654327;
int n,m,k,ok;

int a[maxn];

int ksm(int a,int b,int M)
{
    int res = 1;
    while (b)
    {
        if(b&1) res = (res*a)%M;
        a = (a*a)%M;
        b /= 2;
    }
    return res%M;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>k;
        for(int i = 1; i <= n; i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n,greater<int>());
        int ans1 = 0,ans2 = 0;
        for(int i = 1; i <= n; i++)
        {
            if(!ans1 && !ans2)
            {
                ans1 = (ans1 + ksm(k,a[i],mod1))%mod1;
                ans2 = (ans2 + ksm(k,a[i],mod2))%mod2;
            }
            else
            {
                ans1 = (ans1 - ksm(k,a[i],mod1) + mod1)%mod1;
                ans2 = (ans2 - ksm(k,a[i],mod2) + mod2)%mod2;
            }
        }
        cout<<ans1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 1001110;
int mod1 = 1e9+7;
int mod2 = 998244353;
int n,m,k,ok;

int a[maxn];

int ksm(int a,int b,int M)
{
    int res = 1;
    while (b)
    {
        if(b&1) res = (res*a)%M;
        a = (a*a)%M;
        b /= 2;
    }
    return res%M;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>k;
        for(int i = 1; i <= n; i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n,greater<int>());
        int ans1 = 0,ans2 = 0;
        for(int i = 1; i <= n; i++)
        {
            if(!ans1 && !ans2)
            {
                ans1 = (ans1 + ksm(k,a[i],mod1))%mod1;
                ans2 = (ans2 + ksm(k,a[i],mod2))%mod2;
            }
            else
            {
                ans1 = (ans1 - ksm(k,a[i],mod1) + mod1)%mod1;
                ans2 = (ans2 - ksm(k,a[i],mod2) + mod2)%mod2;
            }
        }
        cout<<ans1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 1001110;
const int M = 1e9+7;
int n,m,k,ok;

int a[maxn];

int ksm(int a,int b)
{
    int res = 1;
    while (b)
    {
        if(b&1) res = (res*a)%M;
        a = (a*a)%M;
        b /= 2;
    }
    return res%M;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>k;
        for(int i = 1; i <= n; i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n,greater<int>());
        int ans = 0;
        for(int i = 1; i <= n; i++)
        {
            if(!ans) ans = (ans + ksm(k,a[i]))%M;
            else ans = (ans - ksm(k,a[i]) + M)%M;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 1001110;
int mod1 = 1e9+7;
int mod2 = 998244353;
int n,m,k,ok;

int a[maxn];

int ksm(int a,int b,int M)
{
    int res = 1;
    while (b)
    {
        if(b&1) res = (res*a)%M;
        a = (a*a)%M;
        b /= 2;
    }
    return res%M;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>k;
        for(int i = 1; i <= n; i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n,greater<int>());
        int ans1 = 0,ans2 = 0;
        for(int i = 1; i <= n; i++)
        {
            if(!ans1 && !ans2)
            {
                ans1 = (ans1 + ksm(k,a[i],mod1))%mod1;
                ans2 = (ans2 + ksm(k,a[i],mod2))%mod2;
            }
            else
            {
                ans1 = (ans1 - ksm(k,a[i],mod1) + mod1)%mod1;
                ans2 = (ans2 - ksm(k,a[i],mod2) + mod2)%mod2;
            }
        }
        cout<<ans1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 201110;
const int M = 1e9+7;
int n,m,k,ok;

int a[maxn];

int ksm(int a,int b)
{
    int res = 1;
    while (b)
    {
        if(b&1) res = (res*a)%M;
        a = (a*a)%M;
        b /= 2;
    }
    return res%M;
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>k;
        for(int i = 1; i <= n; i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n,greater<int>());
        int ans = 0;
        for(int i = 1; i <= n; i++)
        {
            if(!ans) ans = (ans + ksm(k,a[i]))%M;
            else ans = (ans - ksm(k,a[i]) + M)%M;
        }
        cout<<ans<<endl;
    }
    return 0;
}


/*#include <iostream>
#include <algorithm>
using namespace std;
int q1[100005],q2[100005];
int vis[100005];
int main(){
	int c,n,a,b,d;
	while(cin>>c>>n){

		for(int i=0;i<n;i++) cin>>q1[i];
		sort(q1,q1+n);
		for(int i=1;i<=n;i++){
			q2[i]+=q2[i-1]+q1[i-1];
		}
		for(int i=1;i<=n;i++){
			a=q2[i]%c;
			if(vis[a]!=0){
				b=i;
				break;
			}else vis[a]++;
		}
		for(int i=1;i<=n;i++){
			if(q2[i]%c==a){
				d=i;
				break;
			}
		}
		cout<<a<<" "<<b<<" "<<d<<'\n';
		for(int i=d;i<=b;i++){
			cout<<q1[i-1]<<" ";
		}
		cout<<'\n';
	}
}*/

/*#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
int main(){
	ll T,n,x1,y1,x2,y2,a,b,ans1,ans2,cou;
	cin>>T;
	while(T--){
		ans1=ans2=1;
		cin>>x1>>y1>>x2>>y2;
		cout<<(x2-x1)*(y2-y1)+1<<'\n';
	}
}*/

/*#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
ll a[400005],c[400005],d[400005];
int main(){
	ll n,k,q,w,z,ans(0),cou;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>a[i];
		a[i+n]=a[i];
	}
	n=n*2;
	//c[0]=a[0];
	//d[0]=a[0]*(1+a[0])/2;
	for(int i=0;i<n;i++){
		c[i+1]=c[i]+a[i];
		d[i+1]=d[i]+((1+a[i])*a[i])/2;
	}
	for(int i=0;i<n;i++){
		if(c[i+1]>=k){
			z=upper_bound(c,c+n,c[i+1]-k)-c;
			cou=d[i+1]-d[z];
			q=c[i+1]-c[z];
			w=k-q;
			cou+=(a[z-1]*(1+a[z-1]))/2;
			cou-=((a[z-1]-w)*(1+a[z-1]-w))/2;
			ans=max(ans,cou);
		}
	}
	cout<<ans<<'\n';
}*/

/*#include <iostream>
using namespace std;
int main(){
	int T,n,m,k,a,b;
	cin>>T;
	while(T--){
		a=b=0;
		cin>>n>>m>>k;
		if(m<=n/k){
			cout<<m<<'\n';
		}else {
			a=m-n/k;
			b=a/(k-1);
			if(a%(k-1)) b++;
			cout<<n/k-b<<'\n';
		}
	}
}*/

/*#include <iostream>
using namespace std;
char tu[1005][1005];
int main(){
	int T,n,m,x,y;
	long long ans;
	cin>>T;
	while(T--){
		ans=0;
		cin>>n>>m>>x>>y;
		if(x*2<y) y=x*2;
		for(int i=0;i<n;i++){
			cin>>tu[i];
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(j!=m-1&&tu[i][j]=='.'&&tu[i][j+1]=='.') ans+=y,j++;
				else if(tu[i][j]=='.') ans+=x;
			}
		}
		cout<<ans<<'\n';
	}
}*/

/*#include <iostream>
#include <math.h>
#define ll long long
using namespace std;
ll q1[100005];
int main(){
	ll T,ans(0),n;
	cin>>n;
	for(int i=0;i<n;i++) cin>>q1[i];
	for(int mx=1;mx<=30;mx++){
		ll sum=0;
		for(int i=0;i<n;i++){
			if(q1[i]>mx){
				sum=0;
				continue;
			}
			sum+=q1[i];
			if(sum<0) sum=0;
			ans=max(ans,sum-mx);
		}
	}
	cout<<ans<<'\n';
}*/

/*#include <iostream>
#include <cmath>
#define ll long long
using namespace std;
int T,pos;
double h,c,t,ans(1e7);
double cal(ll n){
	return fabs((n*h+(n-1)*c)/(n*2-1)-t);
}
int main(){

	cin>>T;
	while(T--){
		ans=1e7;
		cin>>h>>c>>t;
		if(h==t){
			cout<<1<<'\n';
			continue;
		}else if((h+c)>=2*t){
			cout<<2<<'\n';
			continue;
		}
		ll l,r,mid,midd;
		l=1,r=1e10,mid=midd=0;
		while(r-l>=10){

			mid=(l+r)/2;
			midd=(mid+r)/2;
			if(cal(mid)>cal(midd)){
				l=mid;
			}else r=midd;
			//cout<<l<<" "<<r<<'\n';
		}
		for(ll i=l;i<=r;i++){
			if(cal(i)<ans){
				ans=min(ans,cal(i));
				pos=i;
			}

		}
		cout<<pos*2-1<<'\n';
	}
}*/

/*#include <iostream>
#include <string.h>
#include <algorithm>
#define ll long long
using namespace std;
ll q1[2005];
int main(){
	ll n,cou,ans,maxx;
	while(cin>>n){
		if(n==0) break;
		ans=-1e9;
		for(int i=0;i<n;i++) cin>>q1[i];
		for(int i=0;i<n;i++){
			cou=maxx=q1[i];
			for(int j=i+1;j<n;j++){
				if(q1[j]<=maxx) continue;
				else {
					if(cou+q1[j]<cou) continue;
					cou+=q1[j];
					maxx=q1[j];
				}
			}
			ans=max(ans,cou);
		}
		cout<<ans<<'\n';
	}
}*/

/*#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int tu[100005][20];
int dp[100005][20];
int MAX(int a,int b,int c){
	if(a>=b&&a>=c) return a;
	if(b>=a&&b>=c) return b;
	if(c>=a&&c>=b) return c;
}
int dp_slove(int i,int j){
	if(j==0){
		return max(dp[i+1][j],dp[i+1][j+1]);
	}else if(j==10){
		return max(dp[i+1][j],dp[i+1][j-1]);
	}else return MAX(dp[i+1][j-1],dp[i+1][j],dp[i+1][j+1]);
}

int main(){
	int n,a,b,N(1);
	while(cin>>n){
		memset(dp,0,sizeof(dp));
		memset(tu,0,sizeof(tu));
		if(n==0) break;
		for(int i=0;i<n;i++) {
			cin>>a>>b;
			tu[b][a]++;
			N=max(N,b);
		}
		for(int i=N;i>=0;i--){
			for(int j=0;j<=10;j++){
				dp[i][j]=dp_slove(i,j)+tu[i][j];
				//cout<<i<<" "<<j<<" "<<dp[i][j]<<'\n';
			}
		}
		cout<<dp[0][5]<<'\n';
	}
}*/

/*#include <iostream>
#include <string.h>
#include <vector>
#define ll long  long
using namespace std;
vector<ll> vis;
int main(){
    ll n,x,p;
    cin>>n;
    while(n--){
    	vis.clear();
    	cin>>x>>p;
        if(x%p==0){
            cout<<-1<<'\n';
            continue;
        }
    	for(int i=1;;i++){
    		cout<<i<<" "<<(i*p+1)%(x%p)<<'\n';
    		if(vis[(i*p+1)%(x%p)]==0) vis[(i*p+1)%(x%p)]++;
    		else {
    			cout<<-1<<'\n';
    			break;
			}
    		if((i*p+1)%(x%p)==0){
    			cout<<i<<" ";
    			cout<<(i*p+1)/(x%p)<<'\n';

    			break;
			}
		}
	}
}*/

/*#include<bits/stdc++.h>
#define inf 0x7ffffff
#define ll long long
#define MAX 2000005
using namespace std;
ll mod=1e9+7;
ll poww(ll a,ll b){
	ll ans(1),base=a;
	while(b){
		if(b&1) ans=ans*base;
		ans%=mod;
		base=base*base;
		base%=mod;
		b>>=1;
	}
	return ans;
}
ll POW[64];
ll n;
struct node{
    ll x,dis;
    bool operator<(const node&a)const{
        return dis>a.dis;
    }
};
vector<ll>son[MAX],v[MAX];
inline void make(ll from,ll To,ll key){
    son[from].push_back(To);
    v[from].push_back(key);
}
#define to son[rt.x][i]
ll dis[MAX],tim[MAX];
bool vis[MAX];
priority_queue<node>q;
void dijkstra(ll s){
    for(ll i=1;i<=n;i++){
        dis[i]=inf;
    }
    dis[s]=0;
    q.push((node){s,0});
    while(!q.empty()){
        node rt=q.top();
        q.pop();
        if(!vis[rt.x]){
            vis[rt.x]=1;
            for(ll i=0;i<son[rt.x].size();i++){
                if(dis[to]>dis[rt.x]+v[rt.x][i]){
                    dis[to]=dis[rt.x]+v[rt.x][i];
                    q.push((node){to,dis[to]});
                }
            }
        }
    }
}
int main(){
    ll m,ans(0),z;
    for(int i=0;i<64;i++){
		POW[i]=poww(2,i);
	}
    scanf("%ld%ld",&n,&m);
    for(ll i=1;i<=m;i++){
        ll x,y,z;
        scanf("%ld%ld",&x,&y);
        make(x,y,1);
        make(y,x,1);
    }
    dijkstra(1);
    for(ll i=2;i<=n;i++){
    	//cout<<poww(2,dis[i])<<' ';
    	if(dis[i]==1e9) continue;
		ans+=POW[dis[i]];
		ans%=mod;
	}
	cout<<ans<<'\n';
    return 0;
}*/

/*#include <iostream>
#include <vector>
#include <queue>
#include <map>
#define ll long long
using namespace std;
vector<ll>tu[2000005];
ll vis[2000005];
map<ll,ll>POW;
vector<ll>POW;
ll mod=1e9+7;
struct awsl{
	ll x,v;
};
inline ll Read() {//�����
    ll x=0,f=1; char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-f; ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
long long poww(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
int main(){
	ll n,m,x,y,cou,ans;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		x=Read();
		y=Read();
		tu[x].push_back(y);
		tu[y].push_back(x);
	}
	queue <awsl> q1;
	awsl a;
	q1.push((awsl){1,1});
	cou=1;
	ans=0;
	while(!q1.empty()){
		a=q1.front();
		q1.pop();
		if(vis[a.x]) continue;
		vis[a.x]++;
		for(int i=0;i<tu[a.x].size();i++){
			if(vis[tu[a.x][i]]) continue;
			if(vis1[tu[a.x][i]]==0){
				if(POW[a.v]==0){
					POW[a.v]=poww(2,a.v,mod);
				}
				ans+=POW[a.v];
				vis1[tu[a.x][i]]++;
				ans%=mod;
				q1.push((awsl){tu[a.x][i],a.v+1});
			}

		}
	}
	cout<<ans<<'\n';
}*/

#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
ll q1[2000005];
ll mod1=1e9+7,mod2=1e9+17;
long long poww(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
int main(){
	ll T,n,p,ans1(0),ans2(0);
	cin>>T;
	while(T--){
		ans1=ans2=0;
		cin>>n>>p;
		for(int i=0;i<n;i++) cin>>q1[i];
		sort(q1,q1+n);
		if(p==1){
			cout<<n%2<<'\n';
			continue;
		}
		for(int i=n-1;i>=0;i--){
			if(!ans1&&!ans2){
				ans1=poww(p,q1[i],mod1);
				ans2=poww(p,q1[i],mod2);
			}else {
				ans1=((ans1-poww(p,q1[i],mod1))+mod1)%mod1;
				ans2=(ans2-poww(p,q1[i],mod2)+mod2)%mod2;
			}
		}
		cout<<ans1<<'\n';

	}
}


#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
ll q1[2000005];
ll mod1=1e9+7,mod2=1e9+17;
long long poww(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
int main(){
	ll T,n,p,ans1(0),ans2(0);
	cin>>T;
	while(T--){
		ans1=ans2=0;
		cin>>n>>p;
		for(int i=0;i<n;i++) scanf("%lld",&q1[i]);
		sort(q1,q1+n);
		if(p==1){
			cout<<n%2<<'\n';
			continue;
		}
		for(int i=n-1;i>=0;i--){
			if(!ans1&&!ans2){
				ans1=poww(p,q1[i],mod1);
				ans2=poww(p,q1[i],mod2);
			}else {
				ans1=((ans1-poww(p,q1[i],mod1))+mod1)%mod1;
				ans2=(ans2-poww(p,q1[i],mod2)+mod2)%mod2;
			}
		}
		cout<<ans1<<'\n';

	}
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int N=1e6+5;
const long long INF = 1e6;
const int MOD=1e9+7;
vector<pair<int,int> > v;

ll lgput(ll x,ll put)
{
    ll rasp=1;
    while(put!=0)
    {
        if(put%2==1) rasp=(1LL*rasp*x)%MOD;
        x=(1LL*x*x)%MOD;
        put/=2;
    }
    return rasp;
}
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,p;
        cin>>n>>p;
        for(int i=1;i<=n;i++)
        {
            int x;
            cin>>x;
            v.pb(mp(x,1));
        }
        if(p==1)cout<<n%2<<'\n';
        else
        {

        sort(v.begin(),v.end());
        int pos=0;
        for(int i=1;i<n;i++)
        {
            if(v[i].first!=v[i-1].first)
            {
                v[++pos]=v[i];
            }
            else v[pos].second++;
        }
        n=pos+1;

            ll x,y,sum=0,rasp,aux=v[n-1].first+1,i;
            for(i=n-1;i>=0;i--)
            {
                x=v[i].first;
                y=v[i].second;
                if(sum==0)aux=x;
                while(sum<INF && aux>x)
                {
                    aux--;
                    sum=1LL*sum*p;
                }
                if(sum>=INF) break;
                else
                {
                    if(sum>y) sum-=y;
                    else if((y-sum)%2==0) sum=0;
                    else sum=1;
                }
            }
            sum%=MOD;
            rasp=lgput(p,aux);
            rasp=(1LL*rasp*sum)%MOD;
            while(i>=0)
            {
                rasp=(rasp-(v[i].second*(lgput(p,v[i].first)))%MOD)%MOD;
                i--;
            }
            if(rasp<0) rasp+=MOD;
            cout << rasp<<'\n';
        }
        v.clear();
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int N=1e6+5;
const long long INF = 1e6;
const int MOD=1e9+7;
vector<pair<int,int> > v;

ll lgput(ll x,ll put)
{
    ll rasp=1;
    while(put!=0)
    {
        if(put%2==1) rasp=(1LL*rasp*x)%MOD;
        x=(1LL*x*x)%MOD;
        put/=2;
    }
    return rasp;
}
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,p;
        cin>>n>>p;
        for(int i=1;i<=n;i++)
        {
            int x;
            cin>>x;
            v.pb(mp(x,1));
        }
        sort(v.begin(),v.end());
        int pos=0;
        for(int i=1;i<n;i++)
        {
            if(v[i].first!=v[i-1].first)
            {
                v[++pos]=v[i];
            }
            else v[pos].second++;
        }
        n=pos+1;
        if(p==1)cout<<n%2<<'\n';
        else
        {

            ll x,y,sum=0,rasp,aux=v[n-1].first+1,i;

            for(i=n-1;i>=0;i--)
            {
                x=v[i].first;
                y=v[i].second;
                if(sum==0)aux=x;
                while(sum<INF && aux>x)
                {
                    aux--;
                    sum=1LL*sum*p;
                }
                if(sum>=INF) break;
                else
                {
                    if(sum>y) sum-=y;
                    else if((y-sum)%2==0) sum=0;
                    else sum=1;
                }
            }
            sum%=MOD;
            rasp=lgput(p,aux);
            rasp=(1LL*rasp*sum)%MOD;
            while(i>=0)
            {
                rasp=(rasp-(v[i].second*(lgput(p,v[i].first)))%MOD)%MOD;
                i--;
            }
            if(rasp<0) rasp+=MOD;
            cout << rasp<<'\n';
        }
        v.clear();
    }
}


#include <bits/stdc++.h>

using namespace std;

const long long MOD = 1e9 + 7;
const long long INF = 1e18 + 7;
long long power(long long a, long long b)
{
    long long r = 1;
    while (b > 0)
    {
        if (b & 1)
            r = r * a % MOD;
        b >>= 1;
        a = a * a % MOD;
    }
    return r;
}
long long realpower(long long a, long long b)
{
    long long r = 1;
    while (b > 0)
    {
        if (b & 1)
            r *= a;
        b >>= 1;
        a *= a;
    }
    return r;
}
int mylog(int b)
{
    long long r = 1;
    int c = 0;
    while (r < INF / b)
    {
        r *= b;
        c++;
    }
    return c;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t; cin >> t;
    while (t--)
    {
        int n; cin >> n;
        long long p; cin >> p;
        int a[n];
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (p == 1)
        {
            cout << n % 2 << "\n";
            continue;
        }
        sort(a, a + n, greater<int>());
        int k = mylog(p);
        long long real = realpower(p, k);
        long long r = 0;
        int c = 0;
        for (int i = 0; i < n; i++)
        {
            if (r == 0)
            {
                r = real;
                c = i;
            }
            else
            {
                int g = k - a[c] + a[i];
                if (g >= 0)
                    r -= realpower(p, g);
            }
        }
        long long add = power(p, a[c]);
        long long sub = 0;
        for (int i = c + 1; i < n; i++)
            sub = (sub + power(p, a[i])) % MOD;
        long long ans = (add - sub) % MOD;
        if (ans < 0)
            ans += MOD;
        cout << ans << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;

const long long MOD = 1e9 + 7;
const long long INF = 1e18 + 7;
long long power(long long a, long long b)
{
    long long r = 1;
    while (b > 0)
    {
        if (b & 1)
            r = r * a % MOD;
        b >>= 1;
        a = a * a % MOD;
    }
    return r;
}
long long realpower(long long a, long long b)
{
    long long r = 1;
    while (b > 0)
    {
        if (b & 1)
            r *= a;
        b >>= 1;
        a *= a;
    }
    return r;
}
int mylog(int b)
{
    long long r = 1;
    int c = 0;
    while (r < INF / b)
    {
        r *= b;
        c++;
    }
    return c + 1;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t; cin >> t;
    while (t--)
    {
        int n; cin >> n;
        long long p; cin >> p;
        int a[n];
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (p == 1)
        {
            cout << n % 2 << "\n";
            continue;
        }
        sort(a, a + n, greater<int>());
        int k = mylog(p);
        long long real = realpower(p, k);
        long long r = real;
        int c = 0;
        for (int i = 1; i < n; i++)
        {
            int g = k - a[c] + a[i];
            r -= realpower(p, g);
            if (r == 0)
            {
                c = i;
                r = real;
            }
        }
        if (c != 0)
            c++;
        long long add = power(p, a[c]);
        long long sub = 0;
        for (int i = c + 1; i < n; i++)
            sub = (sub + power(p, a[i])) % MOD;
        long long ans = (add - sub) % MOD;
        if (ans < 0)
            ans += MOD;
        cout << ans << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;

const long long MOD = 1e9 + 7;
const long long INF = 1e18 + 7;
long long power(long long a, long long b)
{
    long long r = 1;
    while (b > 0)
    {
        if (b & 1)
            r = r * a % MOD;
        b >>= 1;
        a = a * a % MOD;
    }
    return r;
}
long long realpower(long long a, long long b)
{
    long long r = 1;
    while (b > 0)
    {
        if (b & 1)
            r *= a;
        b >>= 1;
        a *= a;
    }
    return r;
}
int mylog(int b)
{
    long long r = 1;
    int c = 0;
    while (r < INF / b)
    {
        r *= b;
        c++;
    }
    return c;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t; cin >> t;
    while (t--)
    {
        int n; cin >> n;
        long long p; cin >> p;
        int a[n];
        for (int i = 0; i < n; i++)
            cin >> a[i];
        if (p == 1)
        {
            cout << n % 2 << "\n";
            continue;
        }
        sort(a, a + n, greater<int>());
        int k = mylog(p);
        long long real = realpower(p, k);
        long long r = real;
        int c = 0;
        for (int i = 1; i < n; i++)
        {
            int g = k - a[c] + a[i];
            r -= realpower(p, g);
            if (r == 0)
            {
                c = i;
                r = real;
            }
        }
        if (c != 0)
            c++;
        long long add = power(p, a[c]);
        long long sub = 0;
        for (int i = c + 1; i < n; i++)
            sub = (sub + power(p, a[i])) % MOD;
        long long ans = (add - sub) % MOD;
        if (ans < 0)
            ans += MOD;
        cout << ans << "\n";
    }
}


#include <bits/stdc++.h>

#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define all(a)           a.begin(),a.end()
#define sf               scanf
#define pf               printf
#define ll               long long
#define N 				       1000000007

using namespace std;
ll arr[1000005], n, p;

ll poww(ll num, ll exp)
{
  ll tmp=1;
  while(exp)
  {
    if(exp%2)
      tmp = (tmp*num) % N;

    num *= num;
    num %= N;
    exp /= 2;
  }

  return tmp;
}

void fun()
{
  sf("%lld %lld", &n, &p);
  loop(i,n)
    sf("%lld", &arr[i]);

  if(p==1)
  {
    if(n%2)
      pf("1\n");
    else
      pf("0\n");
    return;
  }
  stack<pair<ll, ll>> s;
  sort(arr, arr+n);
  for(int i=n-1; i>=0; i--)
  {
    if(s.empty())
    {
      s.push({arr[i], 1});
    }
    else
    {
      pair<ll, ll> cur = s.top();
      ll cnt=1;
      ll t1 = arr[i];
      while(true)
      {
        if(cur.first > t1)
        {
          s.push({t1, cnt});
          break;
        }
        else if(cur.first == t1)
        {
          s.pop();
          if(s.empty())
            break;
          else
          {
            cnt = cur.second +1;
            if(cnt == p)
            {
              cnt=1;
              t1++;
            }
            else
            {
              s.push({t1, cnt});
              break;
            }
          }

        }
      }
    }
  }

  if(s.empty())
    pf("0\n");
  else
  {
    ll ans=0;
    while(s.size() > 1)
    {
      pair<ll, ll> tmp = s.top();
      s.pop();
      ans = (ans + (tmp.second * poww(p, tmp.first))%N)%N;
    }
    pair<ll, ll> tmp = s.top();
    s.pop();
    ll dif = poww(p, tmp.first);
    dif = (N + dif-ans)%N;
    pf("%lld\n",  dif);
  }
}

int main()
{
  int test;
  sf("%d", &test);
  while(test--)
    fun();
  return 0;
}


#include <bits/stdc++.h>

#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define all(a)           a.begin(),a.end()
#define sf               scanf
#define pf               printf
#define ll               long long
#define N 				       1000000007

using namespace std;
ll arr[1000005], n, p;

ll poww(ll num, ll exp)
{
  if(exp==0)
    return 1;

  ll tmp = poww(num, exp/2);
  tmp *= tmp;
  tmp %= N;
  if(exp%2)
    return (num*tmp)%N;

  return tmp;
}

ll f(int i)
{
  if(i==n)
    return 0;

  ll x = f(i+1);
  ll rp = poww(p, arr[i]);
  return min(abs(x+rp)%N, abs(x-rp)%N);
}

void fun()
{
  sf("%lld %lld", &n, &p);
  loop(i,n)
    sf("%lld", &arr[i]);
  sort(arr, arr+n);
  pf("%lld\n", f(0));
}

int main()
{
  int test;
  sf("%d", &test);
  while(test--)
    fun();
  return 0;
}


#include <bits/stdc++.h>

#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define all(a)           a.begin(),a.end()
#define sf               scanf
#define pf               printf
#define ll               long long
#define N 				       1000000007

using namespace std;
ll arr[1000005], n, p;

ll poww(ll num, ll exp)
{
  if(exp==0)
    return 1;

  ll tmp = poww(num, exp/2);
  tmp *= tmp;
  tmp %= N;
  if(exp%2)
    return (num*tmp)%N;

  return tmp;
}

ll f(int i)
{
  if(i==n)
    return 0;

  ll x = f(i+1);
  ll rp = poww(p, arr[i]);
  return min(abs(x+rp)%N, abs(x-rp)%N);
}

void fun()
{
  sf("%lld %lld", &n, &p);
  loop(i,n)
    sf("%lld", &arr[i]);
  sort(arr, arr+n, greater<int>());
  pf("%lld\n", f(0));
}

int main()
{
  int test;
  sf("%d", &test);
  while(test--)
    fun();
  return 0;
}


#include <bits/stdc++.h>

#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define all(a)           a.begin(),a.end()
#define sf               scanf
#define pf               printf
#define ll               long long
#define N 				       1000000007

using namespace std;
ll arr[1000005], n, p;

ll poww(ll num, ll exp)
{
  if(exp==0)
    return 1;

  ll tmp = poww(num, exp/2);
  tmp *= tmp;
  tmp %= N;
  if(exp%2)
    return (num*tmp)%N;

  return tmp;
}

ll f(int i, ll prev)
{
  if(i==n)
    return prev;

  if(prev>0)
    return f(i+1, (N+prev-poww(p, arr[i]))%N);
  return f(i+1, poww(p, arr[i]));
}

void fun()
{
  sf("%lld %lld", &n, &p);
  loop(i,n)
    sf("%lld", &arr[i]);
  sort(arr, arr+n, greater<int>());
  pf("%lld\n", f(0, 0));
}

int main()
{
  int test;
  sf("%d", &test);
  while(test--)
    fun();
  return 0;
}


#include <bits/stdc++.h>

#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define stlloop(v)       for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define loop(i,n)        for(int i=0;i<n;i++)
#define all(a)           a.begin(),a.end()
#define sf               scanf
#define pf               printf
#define ll               long long
#define N 				       1000000007

using namespace std;
ll arr[1000005], n, p;

ll poww(ll num, ll exp)
{
  ll tmp=1;
  while(exp)
  {
    if(exp%2)
      tmp = (tmp*num) % N;

    num *= num;
    num %= N;
    exp /= 2;
  }

  return tmp;
}

void fun()
{
  sf("%lld %lld", &n, &p);
  loop(i,n)
    sf("%lld", &arr[i]);

  if(p==1)
  {
    if(n%2)
      pf("1\n");
    else
      pf("0\n");
    return;
  }
  stack<pair<ll, ll>> s;
  sort(arr, arr+n);

  for(int i=n-1; i>=0; i--)
  {
    if(s.empty())
    {
      s.push({arr[i], 1});
    }
    else
    {
      ll cnt=1;
      ll t1 = arr[i];
      while(true)
      {
        pair<ll, ll> cur = s.top();
        if(cur.first > t1)
        {
          s.push({t1, cnt});
          break;
        }
        else if(cur.first == t1)
        {
          s.pop();
          if(s.empty())
            break;
          else
          {
            cnt = cur.second +1;
            if(cnt == p)
            {
              cnt=1;
              t1++;
            }
            else
            {
              s.push({t1, cnt});
              break;
            }
          }

        }
      }
    }
  }

  if(s.empty())
    pf("0\n");
  else
  {
    ll ans=0;
    while(s.size() > 1)
    {
      pair<ll, ll> tmp = s.top();
      s.pop();
      ans = (ans + (tmp.second * poww(p, tmp.first))%N)%N;
    }
    pair<ll, ll> tmp = s.top();
    s.pop();
    ll dif = poww(p, tmp.first);
    dif = (N + dif-ans)%N;
    pf("%lld\n",  dif);
  }
}

int main()
{
  int test;
  sf("%d", &test);
  while(test--)
    fun();
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        sort(k.begin(),k.end());
        ll res=0;
        int prek=1e6+10;
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            res=res*qpow(p,prek-nowk)%MOD;
            if(res){
                res=(res-1+MOD)%MOD;
            }else{
                res=(res+1)%MOD;
            }
            prek=nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD&&q)return 1;
    }
    return 0;
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        if(prek==0)res=0;
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if(tpow(p, prek - nowk)||(res*qpow(p,prek-nowk))>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk);
                    if (res>0) {
                        res = res-1;
                    } else {
                        res = 1;
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD&&q)return 1;
    }
    return 0;
}

int main() {
    int T;
    scanf("%d", &T);
    bool cat=0;
    if(T==1000){
        cat=1;
    }
    int time=0;
    while (T--) {
        if(cat&&!time)time++;
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        if(time==2){
            cout<<"!!!"<<endl;
            cout<<n<<' '<<p<<endl;
            for(int i=0;i<n;i++){
                cout<<k[i]<<' ';
            }
            return 0;
        }
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if(tpow(p, prek - nowk)||res*qpow(p,prek-nowk)>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk);
                    if (res>0) {
                        res = res-1;
                    } else {
                        res = 1;
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD&&q)return 1;
    }
    return 0;
}

int main() {
    int T;
    scanf("%d", &T);
    bool cat=0;
    if(T==1000){
        cat=1;
    }
    int time=0;
    while (T--) {
        if(cat)time++;
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        if(time==2){
            cout<<"!!!"<<endl;
            cout<<n<<' '<<p<<endl;
            for(int i=0;i<n;i++){
                cout<<k[i]<<' ';
            }
            return 0;
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if(tpow(p, prek - nowk)||res*qpow(p,prek-nowk)>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk);
                    if (res>0) {
                        res = res-1;
                    } else {
                        res = 1;
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD)return 1;
    }
    return 0;
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if(tpow(p, prek - nowk)||res*qpow(p,prek-nowk)>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk)%MOD;
                    if (res) {
                        res = (res - 1 + MOD) % MOD;
                    } else {
                        res = (res + 1) % MOD;
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD&&q)return 1;
    }
    return 0;
}

int main() {
    int T;
    scanf("%d", &T);
    bool cat=0;
    if(T==1000){
        cat=1;
    }
    int time=0;
    while (T--) {
        if(cat)time++;
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        if(time==2){
            cout<<"!!!"<<endl;
            cout<<n<<' '<<p<<endl;
            for(int i=0;i<n;i++){
                cout<<k[i]<<' ';
            }
            return 0;
        }
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if(tpow(p, prek - nowk)||res*qpow(p,prek-nowk)>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk);
                    if (res>0) {
                        res = res-1;
                    } else {
                        res = 1;
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD&&q>0)return 1;
    }
    return 0;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if((tpow(p, prek - nowk)&&res!=0)||res*qpow(p,prek-nowk)>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk);
                    if (res>0) {
                        res = res-1;
                    } else {
                        res = 1;
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD&&q)return 1;
    }
    return 0;
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if(tpow(p, prek - nowk)||res*qpow(p,prek-nowk)>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk)%MOD;
                    if (res) {
                        res = (res - 1 + MOD) % MOD;
                    } else {
                        res = (res + 1) % MOD;
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD&&q)return 1;
    }
    return 0;
}

int main() {
    int T;
    scanf("%d", &T);
    bool cat=0;
    if(T==1000){
        cat=1;
    }
    int time=0;
    while (T--) {
        if(cat)time++;
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        if(time==2){
            cout<<"!!!"<<endl;
            cout<<n<<' '<<p<<endl;
            for(int i=0;i<n;i++){
                cout<<k[i]<<endl;
            }
            return 0;
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if(tpow(p, prek - nowk)||res*qpow(p,prek-nowk)>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk);
                    if (res>0) {
                        res = res-1;
                    } else {
                        res = 1;
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD&&q)return 1;
    }
    return 0;
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if(tpow(p, prek - nowk)||res*qpow(p,prek-nowk)>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk);
                    if (res>0) {
                        res = res-1;
                    } else {
                        res = res + 1;
                        if(res>=MOD){
                            flag=1;
                            res%=MOD;
                        }
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const ll MOD=1e9+7;
ll qpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1)res=res*p%MOD;
        q>>=1;
        p=p*p%MOD;
    }
    return res;
}
bool tpow(ll p,int q){
    ll res=1;
    while(q){
        if(q&1){
            res=res*p;
            if(res>=MOD)return 1;
        }
        q>>=1;
        p=p*p;
        if(p>=MOD&&q)return 1;
    }
    return 0;
}

int main() {
    int T;
    scanf("%d", &T);
    bool cat=0;
    if(T==1000){
        cat=1;
    }
    int time=0;
    while (T--) {
        if(cat)time++;
        bool flag=0;
        int n,p;
        scanf("%d%d",&n,&p);
        vector<int>k;
        int temp;
        for(int i=0;i<n;i++){
            scanf("%d",&temp);
            k.push_back(temp);
        }
        if(time==2){
            cout<<"!!!"<<endl;
            cout<<n<<' '<<p<<endl;
            for(int j=0;j<10;j++) {
                for (int i = 0; i < 100; i++) {
                    cout << k[i] << ' ';
                }
                cout<<endl;
            }
            return 0;
        }
        sort(k.begin(),k.end());
        ll res=1;
        int prek=k.back();
        k.pop_back();
        int nowk;
        while(!k.empty()){
            nowk=k.back();
            k.pop_back();
            if(!flag) {
                if(tpow(p, prek - nowk)||res*qpow(p,prek-nowk)>=MOD){
                    flag=1;
                    res=res*qpow(p,prek-nowk)%MOD;
                    res = (res - 1 + MOD) % MOD;
                }
                else {
                    res=res*qpow(p,prek-nowk);
                    if (res>0) {
                        res = res-1;
                    } else {
                        res = 1;
                    }
                }
            }
            else{
                res = res * qpow(p, prek - nowk) % MOD;
                res = (res - 1 + MOD) % MOD;
            }
            prek = nowk;
        }
        res=res*qpow(p,prek)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
inline ll fpm(ll a,ll p){
	ll res=1;
	while(p){
		if (p&1) res=res*a%mod;
		a=a*a%mod;
		p/=2;
	}
	return res;
}
int n;
ll p;
ll a[maxn],b[maxn];
inline bool cmp(ll a1,ll b1){
	return a1>b1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d%lld",&n,&p);
		for (int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		if (p==1){
			printf("%d\n",n%2);
			continue;
		}
		ll ans=0;
		ll v=0;
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++){
			if (i!=1&&v!=0){
				//cout<<"print past p "<<p<<endl;
				for (int j=1;j<=a[i-1]-a[i];j++){
					v=v*p;
					if (v>n){
						break;
					}
				}
				//cout<<"print now p "<<p<<endl;
				if (v>n){
					for (int j=i;j<=n;j++){
						ans=(ans+mod-fpm(p,a[j]))%mod;
					}
					break;
				}
			}
			if (v>0){
				v=(v-1);
				ans=(ans+mod-fpm(p,a[i]))%mod;
			}
			else{
				v++;
				ans=(ans+fpm(p,a[i]))%mod;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
inline ll fpm(ll a,ll p){
	ll res=1;
	while(p){
		if (p&1) res=res*a%mod;
		a=a*a%mod;
		p/=2;
	}
	return res;
}
int n;
ll p;
ll a[maxn],b[maxn];
inline bool cmp(int a1,int b1){
	return a1>b1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d%lld",&n,&p);
		for (int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		if (p==1){
			printf("%d\n",n%2);
			continue;
		}
		ll ans=0;
		ll v=0;
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++){
			if (i!=1&&a[i]!=a[i-1]) v=v*p;
			if (v>n){
				for (int j=i;j<=n;j++){
					ans=(ans+mod-fpm(p,a[j]))%mod;
				}
				break;
			}
			if (v>0){
				v=(v-1);
				ans=(ans+mod-fpm(p,a[i]))%mod;
			}
			else{
				v++;
				ans=(ans+fpm(p,a[i]))%mod;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
inline ll fpm(ll a,ll p){
	ll res=1;
	while(p){
		if (p&1) res=res*a%mod;
		a=a*a%mod;
		p/=2;
	}
	return res;
}
int n;
ll p;
ll a[maxn],b[maxn];
inline bool cmp(ll a1,ll b1){
	return a1>b1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d%lld",&n,&p);
		for (int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		if (p==1){
			printf("%d\n",n%2);
			continue;
		}
		ll ans=0;
		ll v=0;
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++){
			if (i!=1&&a[i]!=a[i-1]) v=v*p;
			if (v>0){
				v=(v-1);
				ans=(ans+mod-fpm(p,a[i]))%mod;
			}
			else{
				v++;
				ans=(ans+fpm(p,a[i]))%mod;
			}
			if (v>n){
				for (int j=i+1;j<=n;j++){
					ans=(ans+mod-fpm(p,a[j]))%mod;
				}
				break;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
inline ll fpm(ll a,ll p){
	ll res=1;
	while(p){
		if (p&1) res=res*a%mod;
		a=a*a%mod;
		p/=2;
	}
	return res;
}
int n;
ll p;
int a[maxn],b[maxn];
inline bool cmp(int a1,int b1){
	return a1>b1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d%lld",&n,&p);
		for (int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		if (p==1){
			printf("%d\n",n%2);
			continue;
		}
		ll ans=0;
		ll v=0;
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++){
			if (i!=1&&a[i]!=a[i-1]) v=v*p;
			if (v>n){
				for (int j=i;j<=n;j++){
					ans=(ans+mod-fpm(p,a[j]))%mod;
				}
				break;
			}
			if (v>0){
				v=(v-1);
				ans=(ans+mod-fpm(p,a[i]))%mod;
			}
			if (v==0){
				v++;
				ans=(ans+fpm(p,a[i]))%mod;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
inline ll fpm(ll a,ll p){
	ll res=1;
	while(p){
		if (p&1) res=res*a%mod;
		a=a*a%mod;
		p/=2;
	}
	return res;
}
int n;
ll p;
int a[maxn],b[maxn];
inline bool cmp(int a1,int b1){
	return a1>b1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d%lld",&n,&p);
		for (int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		ll ans=0;
		ll v=0;
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++){
			if (i!=1&&a[i]!=a[i-1]) v=v*p;
			if (v>n){
				for (int j=i;j<=n;j++){
					ans=(ans+mod-fpm(p,a[j]))%mod;
				}
				break;
			}
			if (v>0){
				v=(v-1);
				ans=(ans+mod-fpm(p,a[i]))%mod;
			}
			if (v==0){
				v++;
				ans=(ans+fpm(p,a[i]))%mod;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
inline ll fpm(ll a,ll p){
	ll res=1;
	while(p){
		if (p&1) res=res*a%mod;
		a=a*a%mod;
		p/=2;
	}
	return res;
}
int n;
ll p;
ll a[maxn],b[maxn];
inline bool cmp(ll a1,ll b1){
	return a1>b1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d%lld",&n,&p);
		for (int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		if (p==1){
			printf("%d\n",n%2);
			continue;
		}
		ll ans=0;
		ll v=0;
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++){
			if (i!=1&&a[i]!=a[i-1]){
				bool flg=0;
				for (int j=1;j<=a[i]-a[i-1];j++){
					v=v*p;
					if (v>n){
						for (int k=i;k<=n;k++){
							ans=(ans+mod-fpm(p,a[k]))%mod;
						}
						flg=1;
						break;
					}
					if (flg) break;
				}
				if (flg) break;
			}
			if (v>0){
				v=(v-1);
				ans=(ans+mod-fpm(p,a[i]))%mod;
			}
			else{
				v++;
				ans=(ans+fpm(p,a[i]))%mod;
			}
		}
		printf("%lld\n",ans);
	}
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
inline ll fpm(ll a,ll p){
	ll res=1;
	while(p){
		if (p&1) res=res*a%mod;
		a=a*a%mod;
		p/=2;
	}
	return res;
}
int n;
ll p;
ll a[maxn],b[maxn];
inline bool cmp(ll a1,ll b1){
	return a1>b1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d%lld",&n,&p);
		for (int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		if (p==1){
			printf("%d\n",n%2);
			continue;
		}
		ll ans=0;
		ll v=0;
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++){
			if (i!=1){
				//cout<<"print past p "<<p<<endl;
				for (int j=1;j<=a[i-1]-a[i];j++){
					v=v*p;
					if (v>n){
						break;
					}
				}
				//cout<<"print now p "<<p<<endl;
				if (v>n){
					for (int j=i;j<=n;j++){
						ans=(ans+mod-fpm(p,a[j]))%mod;
					}
					break;
				}
			}
			if (v>0){
				v=(v-1);
				ans=(ans+mod-fpm(p,a[i]))%mod;
			}
			else{
				v++;
				ans=(ans+fpm(p,a[i]))%mod;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
inline ll fpm(ll a,ll p){
	ll res=1;
	while(p){
		if (p&1) res=res*a%mod;
		a=a*a%mod;
		p/=2;
	}
	return res;
}
int n;
ll p;
ll a[maxn],b[maxn];
inline bool cmp(ll a1,ll b1){
	return a1>b1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d%lld",&n,&p);
		for (int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		if (p==1){
			printf("%d\n",n%2);
			continue;
		}
		ll ans=0;
		ll v=0;
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++){
			if (i!=1){
				//cout<<"print past p "<<p<<endl;
				for (int j=1;j<=a[i-1]-a[i];j++){
					v=v*p;
					if (v>n){
						break;
					}
				}
				//cout<<"print now p "<<p<<endl;
				if (v>n){
					for (int j=i;j<=n;j++){
						ans=(ans+mod-fpm(p,a[j]))%mod;
					}
					break;
				}
			}
			if (v>0){
				v=(v-1);
				ans=(ans+mod-fpm(p,a[i]))%mod;
			}
			else{
				v++;
				ans=(ans+fpm(p,a[i]))%mod;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cassert>
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
inline ll fpm(ll a,ll p){
	ll res=1;
	while(p){
		if (p&1) res=res*a%mod;
		a=a*a%mod;
		p/=2;
	}
	return res;
}
int n;
ll p;
ll a[maxn],b[maxn];
inline bool cmp(ll a1,ll b1){
	return a1>b1;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		scanf("%d%lld",&n,&p);
		for (int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		if (p==1){
			printf("%d\n",n%2);
			continue;
		}
		ll ans=0;
		ll v=0;
		sort(a+1,a+n+1,cmp);
		for (int i=1;i<=n;i++){
			if (i!=1){
				bool flg=0;
				//cout<<"print past p "<<p<<endl;
				for (int j=1;j<=a[i-1]-a[i];j++){
					v=v*p;
					if (v>n){
						flg=1;
						break;
					}
					if (flg) break;
				}
				//cout<<"print now p "<<p<<endl;
				if (v>n){
					for (int j=i;j<=n;j++){
						ans=(ans+mod-fpm(p,a[j]))%mod;
					}
					break;
				}
			}
			if (v>0){
				v=(v-1);
				ans=(ans+mod-fpm(p,a[i]))%mod;
			}
			else{
				v++;
				ans=(ans+fpm(p,a[i]))%mod;
			}
		}
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
/*
ifstream fin("in.txt");
ofstream fout("out.txt");
*/
typedef pair <int, int> pii;
#define mp make_pair
#define pb emplace_back
#define mt(a,b) memset(a,b,sizeof(a))
#define zero(x) (((x)>0?(x):-(x))<eps)
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265;
const double eps = 0.000001;
const ll mod = 1e9 + 7;
const int maxn = 1e6 +7;

int t;
int n, p;
int a[maxn];
map <int,int> m;
ll mo(ll a)
{
    if(a >= mod)
        return a % mod;
    if(a < 0)
        return a + mod;
    return a;

}
ll qpow(ll a, ll b)
{
    if(b == 0)
        return 1;
    ll res = 1;
    ll base = a;
    while(b)
    {
        if(b & 1)
        {
            res *= base;
            res = mo(res);
        }
        base *= base;
        base = mo(base);
        b >>= 1;
    }
    return mo(res);
}
int main()
{
    scanf("%d", &t);
    while(t --)
    {
        m.clear();
        scanf("%d%d", &n, &p);
        for(int i = 0; i < n; i ++)
        {
            scanf("%d", &a[i]);
        }
        if(p == 1)
        {
            if(n & 1)
            {
                printf("1\n");
            }
            else
                printf("0\n");
            continue;
        }
        for(int i = 0; i < n; i ++)
        {
            m[a[i]] ++;
        }
        vector <int> temp;
        for(auto x : m)
        {
            temp.pb(x.first);
        }
        sort(temp.begin(), temp.end());
        int flag = -1;
        for(int i = temp.size() - 1; i >= 0; i --)
        {
            if(flag != -1)
                break;
            //cout<<temp[i]<<m[temp[i]]<<endl;
            int now = temp[i];
            if(m[now] > 1)
            {
                m[now] %= 2;
                if(!m[now])
                    temp.pop_back();
            }
            if(m[now] >= 1)
            {
                ll cnt = 1;
                int j = i - 1;
                int pos = now;
                for(int k = 0; k < 1e6 / p && pos >= 0; k ++)
                {
                    if(pos > temp[j])
                        cnt *= p;
                    if(pos == temp[j])
                    {
                        cnt -= m[temp[j]];
                        if(cnt <= 0)
                        {
                            //cout<<"WA"<<now<<pos<<endl;;
                            for(int l = i; l > j; l --)
                                m[temp[l]] = 0;
                            m[temp[j]] = -cnt;
                            flag = 0;
                            break;
                        }
                        cnt *= p;
                        j --;
                    }
                    pos --;
                    if(cnt >= n)
                        break;
                }
                break;

            }
        }
        vector <int> ans;
        for(auto x : temp)
        {
            if(m[x])
                ans.pb(x);
            //cout<<x<<m[x]<<endl;
        }
        ll res = qpow(p, ans[ans.size() - 1]);
        for(int i = 0; i < ans.size() - 1 ; i ++)
        {
            res -= qpow(p, ans[i]) * m[ans[i]];
            res = mo(res);
        }
        printf("%lld\n", mo(res) );

    }

}
/*
2
4 2
1 1 1 2
4 2
1 1 1 4
*/


#include<bits/stdc++.h>
using namespace std;
/*
ifstream fin("in.txt");
ofstream fout("out.txt");
*/
typedef pair <int, int> pii;
#define mp make_pair
#define pb emplace_back
#define mt(a,b) memset(a,b,sizeof(a))
#define zero(x) (((x)>0?(x):-(x))<eps)
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265;
const double eps = 0.000001;
const ll mod = 1e9 + 7;
const int maxn = 1e6 +7;

int t;
int n, p;
int a[maxn];
map <int,int> m;
ll mo(ll a)
{
    if(a >= mod)
        return a % mod;
    while(a < 0)
        a += mod;
    return a;

}
ll qpow(ll a, ll b)
{
    if(b == 0)
        return 1;
    ll res = 1;
    ll base = a;
    while(b)
    {
        if(b & 1)
        {
            res *= base;
            res = mo(res);
        }
        base *= base;
        base = mo(base);
        b >>= 1;
    }
    return mo(res);
}
int main()
{
    scanf("%d", &t);
    while(t --)
    {
        m.clear();
        scanf("%d%d", &n, &p);
        for(int i = 0; i < n; i ++)
        {
            scanf("%d", &a[i]);
        }
        if(p == 1)
        {
            if(n & 1)
            {
                printf("1\n");
            }
            else
                printf("0\n");
            continue;
        }
        for(int i = 0; i < n; i ++)
        {
            m[a[i]] ++;
        }
        vector <int> temp;
        for(auto x : m)
        {
            temp.pb(x.first);
        }
        sort(temp.begin(), temp.end());
        for(int i = temp.size() - 1; i >= 0; i --)
        {
            //cout<<temp[i]<<m[temp[i]]<<endl;
            int now = temp[i];
            if(m[now] > 1)
            {
                m[now] %= 2;
            }
            if(m[now] >= 1)
            {
                ll cnt = 1;
                int j = i - 1;
                int pos = now;
                for(int k = 0; k < 1e6 && pos >= 0; k ++)
                {
                    if(pos > temp[j])
                        cnt *= p;
                    if(pos == temp[j])
                    {
                        cnt -= m[temp[j]];
                        if(cnt <= 0)
                        {
                            //cout<<"WA"<<now<<pos<<endl;;
                            for(int l = i; l > j; l --)
                                m[temp[l]] = 0;
                            m[temp[j]] = -cnt;
                            break;
                        }
                        cnt *= p;
                        j --;
                    }
                    pos --;
                    if(cnt >= n)
                        break;
                }
                if(m[now])
                    break;

            }
        }
        vector <int> ans;
        for(auto x : temp)
        {
            if(m[x])
                ans.pb(x);
            //cout<<x<<m[x]<<endl;
        }
        if(ans.size())
        {
            ll res = qpow(p, ans[ans.size() - 1]);
            //cout<<res<<endl;
            for(int i = 0; i < ans.size() - 1 ; i ++)
            {
                //cout<<mo(mo(qpow(p, ans[i])) * m[ans[i]] )<<endl;
                res = res - mo(mo(qpow(p, ans[i])) * m[ans[i]] );
                //cout<<res;
                res = mo(res);
            }
            printf("%lld\n", mo(res) );
        }
        else
            printf("0\n");

    }

}
/*
2
6 10
10 10 10 10 10 20
*/


#include<bits/stdc++.h>
using namespace std;
/*
ifstream fin("in.txt");
ofstream fout("out.txt");
*/
typedef pair <int, int> pii;
#define mp make_pair
#define pb emplace_back
#define mt(a,b) memset(a,b,sizeof(a))
#define zero(x) (((x)>0?(x):-(x))<eps)
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265;
const double eps = 0.000001;
const ll mod = 1e9 + 7;
const int maxn = 1e6 +7;

int t;
int n, p;
int a[maxn];
map <int,int> m;
ll mo(ll a)
{
    if(a >= mod)
        return a % mod;
    while(a < 0)
        a += mod;
    return a;

}
ll qpow(ll a, ll b)
{
    if(b == 0)
        return 1;
    ll res = 1;
    ll base = a;
    while(b)
    {
        if(b & 1)
        {
            res *= base;
            res = mo(res);
        }
        base *= base;
        base = mo(base);
        b >>= 1;
    }
    return mo(res);
}
int main()
{
    scanf("%d", &t);
    while(t --)
    {
        m.clear();
        scanf("%d%d", &n, &p);
        for(int i = 0; i < n; i ++)
        {
            scanf("%d", &a[i]);
        }
        if(p == 1)
        {
            if(n & 1)
            {
                printf("1\n");
            }
            else
                printf("0\n");
            continue;
        }
        for(int i = 0; i < n; i ++)
        {
            m[a[i]] ++;
        }
        vector <int> temp;
        for(auto x : m)
        {
            temp.pb(x.first);
        }
        sort(temp.begin(), temp.end());
        for(int i = temp.size() - 1; i >= 0; i --)
        {
            //cout<<temp[i]<<m[temp[i]]<<endl;
            int now = temp[i];
            if(m[now] > 1)
            {
                m[now] %= 2;
            }
            if(m[now] >= 1)
            {
                ll cnt = 1;
                int j = i - 1;
                int pos = now;
                for(int k = 0; k < 1e6 && pos >= 0; k ++)
                {
                    if(pos > temp[j])
                        cnt *= p;
                    if(pos == temp[j])
                    {
                        cnt -= m[temp[j]];
                        if(cnt <= 0)
                        {
                            //cout<<"WA"<<now<<pos<<endl;;
                            for(int l = i; l > j; l --)
                                m[temp[l]] = 0;
                            m[temp[j]] = -cnt;
                            break;
                        }
                        cnt *= p;
                        j --;
                    }
                    pos --;
                    if(cnt >= n)
                        break;
                }
                if(m[now])
                    break;

            }
        }
        vector <int> ans;
        for(auto x : temp)
        {
            if(m[x])
                ans.pb(x);
            //cout<<x<<m[x]<<endl;
        }
        if(ans.size())
        {
            ll res = qpow(p, ans[ans.size() - 1]);
            //cout<<res<<endl;
            for(int i = 0; i < ans.size() - 1 ; i ++)
            {
                //cout<<mo(mo(qpow(p, ans[i])) * m[ans[i]] )<<endl;
                res = res - mo(mo(qpow(p, ans[i])) * m[ans[i]] );
                //cout<<res;
                res = mo(res);
            }
            printf("%lld\n", mo(res) );
        }
        else
            printf("0");

    }

}
/*
2
6 10
10 10 10 10 10 20
*/


#include<bits/stdc++.h>
using namespace std;
/*
ifstream fin("in.txt");
ofstream fout("out.txt");
*/
typedef pair <int, int> pii;
#define mp make_pair
#define pb emplace_back
#define mt(a,b) memset(a,b,sizeof(a))
#define zero(x) (((x)>0?(x):-(x))<eps)
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265;
const double eps = 0.000001;
const ll mod = 1e9 + 7;
const int maxn = 1e6 +7;

int t;
int n, p;
int a[maxn];
map <int,int> m;
ll mo(ll a)
{
    if(a >= mod)
        return a % mod;
    if(a < 0)
        return a + mod;
    return a;

}
ll qpow(ll a, ll b)
{
    if(b == 0)
        return 1;
    ll res = 1;
    ll base = a;
    while(b)
    {
        if(b & 1)
        {
            res *= base;
            res = mo(res);
        }
        base *= base;
        base = mo(base);
        b >>= 1;
    }
    return mo(res);
}
int main()
{
    scanf("%d", &t);
    while(t --)
    {
        m.clear();
        scanf("%d%d", &n, &p);
        for(int i = 0; i < n; i ++)
        {
            scanf("%d", &a[i]);
        }
        if(p == 1)
        {
            if(n & 1)
            {
                printf("1\n");
            }
            else
                printf("0\n");
            continue;
        }
        for(int i = 0; i < n; i ++)
        {
            m[a[i]] ++;
        }
        vector <int> temp;
        for(auto x : m)
        {
            temp.pb(x.first);
        }
        sort(temp.begin(), temp.end());
        for(int i = temp.size() - 1; i >= 0; i --)
        {
            //cout<<temp[i]<<m[temp[i]]<<endl;
            int now = temp[i];
            if(m[now] > 1)
            {
                m[now] %= 2;
            }
            if(m[now] >= 1)
            {
                ll cnt = 1;
                int j = i - 1;
                int pos = now;
                for(int k = 0; k < 1e6 && pos >= 0; k ++)
                {
                    if(pos > temp[j])
                        cnt *= p;
                    if(pos == temp[j])
                    {
                        cnt -= m[temp[j]];
                        if(cnt <= 0)
                        {
                            //cout<<"WA"<<now<<pos<<endl;;
                            for(int l = i; l > j; l --)
                                m[temp[l]] = 0;
                            m[temp[j]] = -cnt;
                            break;
                        }
                        cnt *= p;
                        j --;
                    }
                    pos --;
                    if(cnt >= n)
                        break;
                }
                break;

            }
        }
        vector <int> ans;
        for(auto x : temp)
        {
            if(m[x])
                ans.pb(x);
            //cout<<x<<m[x]<<endl;
        }
        if(ans.size())
        {

        ll res = qpow(p, ans[ans.size() - 1]);
        for(int i = 0; i < ans.size() - 1 ; i ++)
        {
            res -= qpow(p, ans[i]) * m[ans[i]];
            res = mo(res);
        }
        printf("%lld\n", mo(res) );
        }
        else
            printf("0");

    }

}
/*
2
4 2
1 1 1 2
4 2
1 1 1 4
*/


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=998244353;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	cin>>t;
	while(t--){
		cin>>n>>x;
		for(i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans2>ans1){
				ans1=(ans1+surplus(x,a[i]))%p;
				t1.push(a[i]);
			}
			else {
				ans2=(ans2+surplus(x,a[i]))%p;
				t2.push(a[i]);
			}
		}
		p=1e9+7;
		ll ans=0;
		while(!t1.empty()){
			ans=(ans+surplus(x,t1.front()))%p;
			t1.pop();
		}
		ll sum=0;
		while(!t2.empty()){
			sum=(sum+surplus(x,t2.front()))%p;
			t2.pop();
		}
		cout<<abs(ans-sum)%p<<endl;
	}

    return 0;
}