#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3*1e5+10;
struct node {
int mo,d;
}b[10*N];
bool cmp(node x,node y){
if (x.mo!=y.mo) return x.mo<y.mo;
else return x.d<y.d;
}
ll n,k,t,mon[N];
signed main(){
std::ios::sync_with_stdio(0);
std::cin.tie(0);std::cout.tie(0);
cin>>t;
for (int i=1;i<=t;i++){
ll ans=0;
cin>>n>>k;
ll o=0;
for (int j=1;j<=n;j++){
cin>>mon[j];
o=max(o,mon[j]);
}
if (k==1){
cout<<"0"<<endl;
continue;
}
ll u=o;
ll x=0;
while (u){
u/=10;
x++;
}
ll m=n*pow(10,x)+o;
ll y=1;
for (int j=1;y<=m;j++){
y*=k;
ll uuu=y;
ll s=0;
while (uuu){
uuu/=10;
s++;
}
for (int l=1;l<s;l++){
ll month=y/pow(10,s-l);
ll day=y%int(pow(10,s-l));
int uo=ceil(log10(day+1));
int yo=month*pow(10,uo)+day;
if (month<=n&&mon[month]>=day&&yo==y){
b[++ans].mo=month;
b[ans].d=day;
}
}
}
sort(b+1,b+1+ans,cmp);
cout<<ans<<endl;
for (int j=1;j<=ans;j++){
cout<<b[j].mo<<' '<<b[j].d<<endl;
}
}
return 0;
}