#include <bits/stdc++.h>
using namespace std;
#define inf 5e14+5
#define ll unsigned long long
struct Node {
ll x, y;
}b[300005];
ll n, k, t, a[300005], ans;
void check(ll num)
{
if (num <= 10)
return;
ll x, y, cnt = 0;
x = num / 10, y = num % 10, cnt++;
while (x>0)
{
if ((ll)(num / pow(10, cnt)) % 10 == 0) {
cnt++;
continue;
}
if (x <= n && y <= a[x]) {
b[++ans].x = x;
b[ans].y = y;
}
cnt++;
x = num / (ll)pow(10, cnt);
y = num % (ll)pow(10, cnt);
}
}
bool cmp(Node x,Node y) {
if (x.x != y.x)
return x.x < y.x;
return x.y < y.y;
}
void solve() {
ll s = k;
while (1)
{
if (s > inf)
break;
check(s);
s *= k;
}
sort(b + 1, b + 1 + ans,cmp);
cout << ans << endl;
for (int i = 1; i <= ans; i++)
cout << b[i].x << " " << b[i].y << endl;
}
int main() {
cin >> t;
while (t--)
{
ans = 0;
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
if (k == 1) {
cout << ans << endl;
continue;
}
solve();
}
return 0;
}