#include <bits/stdc++.h>

using namespace std;

int main() {
    float t,n,k;
    cin >> t;
    while(t--){
    	cin >> n >> k;
    	float x = 1;
    	if(n < k || k == 1){
    		cout << n << endl;
    	}else{
    		int in = k;
    		while(n > pow(k,x)){
    			x++;
    		}
    		cout << n << " " << pow(k,x-1) << endl;
    	}
    }
    return 0;
}
