蒟蒻在线orz
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+5;
const ll inf=4e15;
int T;
int n,k;
int a[N];
struct date{
int x,y;
};
int cnt;
date ans[N];
void div(ll x){
ll h=x,t=0,mod=1;
while(x/mod>n) mod*=10;
while(h){
while((x*10/mod)%10==0 && x/mod) mod*=10;
h=x/mod;
t=x%mod;
if(!h) break;
if(t<=a[h] && t>=1){
cnt++;
ans[cnt].x=h;
ans[cnt].y=t;
}
mod*=10;
}
}
bool cmp(date a,date b){
return a.x==b.x ? a.y<b.y : a.x<b.x;
}
int main(){
cin>>T;
while(T--){
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
if(k==1){
printf("0\n");
continue;
}
cnt=0;
for(ll p=k;p<=inf;p*=k) div(p);
sort(ans+1,ans+1+cnt,cmp);
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++)
printf("%d %d\n",ans[i].x,ans[i].y);
}
return 0;
}