#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define ll long long
#define pi pair<int,int>
#define pl pair<ll,ll>
#define pd pair<double,double>
#define ld long double
#define pld pair<ld,ld>
#define lg length()
#define sz size()
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pi>
#define vpl vector<pl>
#define pb push_back
#define INF 1000000005
#define LINF 1000000000000000005
#define all(x) (x).begin(), (x).end()

int n,m,k,x,y,z,ans,a[10],b[200],f;

vector <int> t[200];
vector <pi> g[200005],temp;
vector <int> h[200005];

void Gen(int p){
    if(p==k+1){
        ans++;
        return;
    }
    for(int i=1;i<=p;i++){
        if(!b[(p<<4)^i]){
            a[p]=i;
            f=(p<<4)^i;
            for(int j : t[f]) b[j]++;
            Gen(p+1);
            for(int j : t[f]) b[j]--;
        }
    }
}

int32_t main(){
    ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
#ifdef LOCAL_DEFINE
    ifstream cin("input.txt");
#endif
    cin >> n >> m >> k;
    for(int i=1;i<=m;i++){
        cin >> x >> y >> z;
        g[x].pb({z,y});
        h[y].pb(x);
    }
    for(int i=1;i<=n;i++) sort(all(g[i]));
    for(int i=1;i<=n;i++){
        temp.clear();
        for(int j : h[i]){
            for(int k=0;k<g[j].sz;k++){
                if(g[j][k].y==i){
                    temp.pb({g[j].sz,k+1});
                    break;
                }
            }
        }
        for(pi j : temp){
            b[(j.x<<4)^j.y]--;
            for(pi k : temp){
                if(j.x<k.x) t[(j.x<<4)^j.y].pb((k.x<<4)^k.y);
                if(k==j) b[(j.x<<4)^j.y]++;
            }
        }
    }
    for(int i=1;i<=k;i++){
        for(int j=1;j<=i;j++){
            t[(i<<4)^j].erase(unique(all(t[(i<<4)^j])),t[(i<<4)^j].end());
        }
    }
    Gen(1);
    cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define ll long long
#define pi pair<int,int>
#define pl pair<ll,ll>
#define pd pair<double,double>
#define ld long double
#define pld pair<ld,ld>
#define lg length()
#define sz size()
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pi>
#define vpl vector<pl>
#define pb push_back
#define INF 1000000005
#define LINF 1000000000000000005
#define all(x) (x).begin(), (x).end()

int n,m,k,x,y,z,ans,a[10],b[10][10][10][10];

vector <pi> t[10][10],temp[200005];
vector <pi> g[200005];
vector <int> h[200005];

void Gen(int p){
    if(p==k+1){
        for(int i=1;i<=k;i++){
            for(int j=i;j<=k;j++){
                if(b[i][a[i]][j][a[j]]) return;
            }
        }
        ans++;
        return;
    }
    for(int i=1;i<=p;i++){
        a[p]=i;
        Gen(p+1);
    }
}

int32_t main(){
    ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
#ifdef LOCAL_DEFINE
    ifstream cin("input.txt");
#endif
    cin >> n >> m >> k;
    for(int i=1;i<=m;i++){
        cin >> x >> y >> z;
        g[x].pb({z,y});
        h[y].pb(x);
    }
    for(int i=1;i<=n;i++) sort(all(g[i]));
    for(int i=1;i<=n;i++){
        for(int k=0;k<g[i].sz;k++){
            temp[g[i][k].y].pb({g[i].sz,k+1});
        }
    }
    for(int i=1;i<=n;i++){
        for(pi j : temp[i]) b[j.x][j.y][j.x][j.y]++;
        temp[i].erase(unique(all(temp[i])),temp[i].end());
        for(pi j : temp[i]) b[j.x][j.y][j.x][j.y]-=2;
        for(pi j : temp[i]){
            for(pi k : temp[i]){
                b[j.x][j.y][k.x][k.y]++;
            }
        }
    }
    Gen(1);
    cout << ans;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <set>
#define pii pair<int,int>
#define F first
#define S second
using namespace std;

const int maxN = 2e5 + 326;
vector<pii> adj[maxN];
vector<int> deg[10];
set<int> st;

int N, M, K, u, v, w, can[10][10][10][10], cur[10], ans;

void dfs(int lay = 1){
	if(lay == K + 1){
		ans++;
		return;
	}
	for(int i = 1; i <= lay; i++){
		bool f = true;
		for(int j = 1; j < lay; j++){
			if(!can[j][cur[j]][lay][i]){
				f = false;
				break;
			}
		}
		if(f){
			cur[lay] = i;
			dfs(lay + 1);
		}
	}
}

int main(){
	cin >> N >> M >> K;
	for(int i = 0; i < M; i++){
		cin >> u >> v >> w;
		adj[u].emplace_back(w, v);
	}
	for(int i = 1; i <= N; i++){
		sort(adj[i].begin(), adj[i].end());
		deg[adj[i].size()].emplace_back(i);
	}
	for(int a = 1; a <= K; a++) for(int b = 1; b <= a; b++) for(int c = a + 1; c <= K; c++) for(int d = 1; d <= c; d++){
		//deg a at bth, deg c at dth
		st = set<int>();
		bool f = true;
		for(int x : deg[a]){
			if(st.count(adj[x][b - 1].S)){
				can[a][b][c][d] = false;
				f = false;
				break;
			}
			st.insert(adj[x][b - 1].S);
		}
		if(!f) continue;
		for(int x : deg[c]){
			if(st.count(adj[x][d - 1].S)){
				can[a][b][c][d] = false;
				f = false;
				break;
			}
			st.insert(adj[x][d - 1].S);
		}
		if(!f) continue;
		can[a][b][c][d] = true;
	}
	dfs();
	cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <set>
#define pii pair<int,int>
#define F first
#define S second
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int maxN = 2e5 + 326;
vector<pii> adj[maxN];
vector<int> deg[12];
unordered_set<int> st;

int N, M, K, u, v, w, can[12][12][12][12], self[12][12], cur[12], ans;

void dfs(int lay = 1){
	if(lay == K + 1){
		ans++;
		return;
	}
	for(int i = 1; i <= lay; i++){
		bool f = self[lay][i];
		for(int j = 1; j < lay && f; j++){
			if(!can[j][cur[j]][lay][i]){
				f = false;
				break;
			}
		}
		if(f){
			cur[lay] = i;
			dfs(lay + 1);
		}
	}
}

signed main(){
	ericxiao;
	cin >> N >> M >> K;
	for(int i = 0; i < M; i++){
		cin >> u >> v >> w;
		adj[u].emplace_back(w, v);
	}
	for(int i = 1; i <= N; i++){
		sort(adj[i].begin(), adj[i].end());
		deg[adj[i].size()].emplace_back(i);
	}
	for(int a = 1; a <= K; a++) for(int b = 1; b <= a; b++){
		st = unordered_set<int>();
		self[a][b] = true;
		for(int x : deg[a]){
			if(st.count(adj[x][b - 1].S)){
				self[a][b] = false;
				break;
			}
			st.insert(adj[x][b - 1].S);
		}
		if(!self[a][b]){
			continue;
		}
		self[a][b] = true;
		for(int c = a + 1; c <= K; c++) for(int d = 1; d <= c; d++){
			//deg a at bth, deg c at dth
			st = unordered_set<int>();
			bool f = true;
			for(int x : deg[a]){
				if(st.count(adj[x][b - 1].S)){
					can[a][b][c][d] = false;
					f = false;
					break;
				}
				st.insert(adj[x][b - 1].S);
			}
			if(!f) continue;
			for(int x : deg[c]){
				if(st.count(adj[x][d - 1].S)){
					can[a][b][c][d] = false;
					f = false;
					break;
				}
				st.insert(adj[x][d - 1].S);
			}
			if(!f) continue;
			can[a][b][c][d] = true;
		}
	}
	dfs();
	cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <set>
#define pii pair<int,int>
#define F first
#define S second
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int maxN = 2e5 + 326;
vector<pii> adj[maxN], in[maxN];
vector<int> deg[12];
unordered_set<int> st;

int N, M, K, u, v, w, can[12][12][12][12], self[12][12], cur[12], ans;

void dfs(int lay = 1){
	if(lay == K + 1){
		ans++;
		return;
	}
	for(int i = 1; i <= lay; i++){
		bool f = can[lay][i][lay][i];
		for(int j = 1; j < lay && f; j++){
			if(!can[j][cur[j]][lay][i]){
				f = false;
				break;
			}
		}
		if(f){
			cur[lay] = i;
			dfs(lay + 1);
		}
	}
}

signed main(){
	ericxiao;
	cin >> N >> M >> K;
	for(int i = 0; i < M; i++){
		cin >> u >> v >> w;
		adj[u].emplace_back(w, v);
	}
	for(int i = 1; i <= N; i++){
		sort(adj[i].begin(), adj[i].end());
		int cnt = 1;
		for(auto [w, v] : adj[i]){
			in[v].emplace_back(cnt++, i);
		}
		deg[adj[i].size()].emplace_back(i);
	}
	for(int a = 1; a <= K; a++)
		for(int b = 1; b <= a; b++)
			for(int c = a; c <= K; c++)
				for(int d = 1; d <= c; d++)
					can[a][b][c][d] = true;

	for(int i = 1; i <= N; i++){
		for(auto [r1, f1] : in[i]){
			for(auto [r2, f2] : in[i]){
				if(f1 != f2){
					if(f1 > f2){
						swap(f1, f2);
						swap(r1, r2);
					}
					//cout << "can[" << adj[f1].size() << "][" << r1 << "][" << adj[f2].size() << "][" << r2 << "] = false" << endl;
					can[adj[f1].size()][r1][adj[f2].size()][r2] = false;
				}
			}
		}
	}
	dfs();
	cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <set>
#define pii pair<int,int>
#define F first
#define S second
#define int long long int
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int maxN = 2e5 + 326;
vector<pii> adj[maxN];
vector<int> deg[12];
set<int> st;

int N, M, K, u, v, w, can[12][12][12][12], self[12][12], cur[12], ans;

void dfs(int lay = 1){
	if(lay == K + 1){
		ans++;
		return;
	}
	for(int i = 1; i <= lay; i++){
		bool f = self[lay][i];
		for(int j = 1; j < lay && f; j++){
			if(!can[j][cur[j]][lay][i]){
				f = false;
				break;
			}
		}
		if(f){
			cur[lay] = i;
			dfs(lay + 1);
		}
	}
}

signed main(){
	ericxiao;
	cin >> N >> M >> K;
	for(int i = 0; i < M; i++){
		cin >> u >> v >> w;
		adj[u].emplace_back(w, v);
	}
	for(int i = 1; i <= N; i++){
		sort(adj[i].begin(), adj[i].end());
		deg[adj[i].size()].emplace_back(i);
	}
	for(int a = 1; a <= K; a++) for(int b = 1; b <= a; b++){
		st = set<int>();
		self[a][b] = true;
		for(int x : deg[a]){
			if(st.count(adj[x][b - 1].S)){
				self[a][b] = false;
				break;
			}
			st.insert(adj[x][b - 1].S);
		}
		if(!self[a][b]){
			continue;
		}
		self[a][b] = true;
		for(int c = a + 1; c <= K; c++) for(int d = 1; d <= c; d++){
			//deg a at bth, deg c at dth
			st = set<int>();
			bool f = true;
			for(int x : deg[a]){
				if(st.count(adj[x][b - 1].S)){
					can[a][b][c][d] = false;
					f = false;
					break;
				}
				st.insert(adj[x][b - 1].S);
			}
			if(!f) continue;
			for(int x : deg[c]){
				if(st.count(adj[x][d - 1].S)){
					can[a][b][c][d] = false;
					f = false;
					break;
				}
				st.insert(adj[x][d - 1].S);
			}
			if(!f) continue;
			can[a][b][c][d] = true;
		}
	}
	dfs();
	cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#define pii pair<int,int>
#define F first
#define S second
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int maxN = 2e5 + 326;
vector<pii> adj[maxN], in[maxN];
vector<int> deg[12];

int N, M, K, u, v, w, can[12][12][12][12], self[12][12], cur[12], ans, frq[maxN];

void dfs(int lay = 1){
	if(lay == K + 1){
		ans++;
		return;
	}
	for(int i = 1; i <= lay; i++){
		bool f = self[lay][i];
		for(int j = 1; j < lay && f; j++){
			if(!can[j][cur[j]][lay][i]){
				f = false;
				break;
			}
		}
		if(f){
			cur[lay] = i;
			dfs(lay + 1);
		}
	}
}

signed main(){
	ericxiao;
	cin >> N >> M >> K;
	for(int i = 0; i < M; i++){
		cin >> u >> v >> w;
		adj[u].emplace_back(w, v);
	}
	for(int i = 1; i <= N; i++){
		sort(adj[i].begin(), adj[i].end());
		int cnt = 1;
		for(auto [w, v] : adj[i]){
			in[v].emplace_back(cnt++, i);
		}
		deg[adj[i].size()].emplace_back(i);
	}
	for(int a = 1; a <= K; a++)
		for(int b = 1; b <= a; b++){
			self[a][b] = true;
			for(int x : deg[a]){
				if(frq[adj[x][b - 1].S]){
					self[a][b] = false;
				}
				frq[adj[x][b - 1].S]++;
			}
			if(!self[a][b]){
				for(int x : deg[a]){
					frq[adj[x][b - 1].S]--;
				}
				continue;
			}
			for(int c = a + 1; c <= K; c++)
				for(int d = 1; d <= c; d++){
					can[a][b][c][d] = true;
					for(int x : deg[c]){
						if(frq[adj[x][d - 1].S]){
							can[a][b][c][d] = false;
						}
						frq[adj[x][d - 1].S]++;
					}
					for(int x : deg[c]){
						frq[adj[x][d - 1].S]--;
					}
				}
			for(int x : deg[a]){
				frq[adj[x][b - 1].S]--;
			}
		}

	dfs();
	cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <set>
#define pii pair<int,int>
#define F first
#define S second
#define ericxiao ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int maxN = 2e5 + 326;
vector<pii> adj[maxN], in[maxN];
vector<int> deg[12];
unordered_set<int> st;

int N, M, K, u, v, w, can[12][12][12][12], self[12][12], cur[12], ans;

void dfs(int lay = 1){
	if(lay == K + 1){
		ans++;
		return;
	}
	for(int i = 1; i <= lay; i++){
		bool f = can[lay][i][lay][i];
		for(int j = 1; j < lay && f; j++){
			if(!can[j][cur[j]][lay][i]){
				f = false;
				break;
			}
		}
		if(f){
			cur[lay] = i;
			dfs(lay + 1);
		}
	}
}

signed main(){
	ericxiao;
	cin >> N >> M >> K;
	for(int i = 0; i < M; i++){
		cin >> u >> v >> w;
		adj[u].emplace_back(w, v);
	}
	for(int i = 1; i <= N; i++){
		sort(adj[i].begin(), adj[i].end());
		int cnt = 1;
		for(auto [w, v] : adj[i]){
			in[v].emplace_back(cnt++, i);
		}
		deg[adj[i].size()].emplace_back(i);
	}
	for(int a = 1; a <= K; a++)
		for(int b = 1; b <= a; b++)
			for(int c = 1; c <= K; c++)
				for(int d = 1; d <= c; d++)
					can[a][b][c][d] = true;

	for(int i = 1; i <= N; i++){
		for(auto [r1, f1] : in[i]){
			for(auto [r2, f2] : in[i]){
				if(f1 != f2){
					//cout << "i = " << i << endl;
					//cout << "f1 = " << f1 << ", f2 = " << f2 << endl;
					//cout << "can[" << adj[f1].size() << "][" << r1 << "][" << adj[f2].size() << "][" << r2 << "] = false" << endl;
					can[adj[f1].size()][r1][adj[f2].size()][r2] = false;
				}
			}
		}
	}
	dfs();
	cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <set>
#define pii pair<int,int>
#define F first
#define S second
#define int long long int
using namespace std;

const int maxN = 2e5 + 326;
vector<pii> adj[maxN];
vector<int> deg[12];
set<int> st;

int N, M, K, u, v, w, can[12][12][12][12], cur[12], ans;

void dfs(int lay = 1){
	if(lay == K + 1){
		ans++;
		return;
	}
	for(int i = 1; i <= lay; i++){
		bool f = true;
		for(int j = 1; j < lay; j++){
			if(!can[j][cur[j]][lay][i]){
				f = false;
				break;
			}
		}
		if(f){
			cur[lay] = i;
			dfs(lay + 1);
		}
	}
}

signed main(){
	cin >> N >> M >> K;
	for(int i = 0; i < M; i++){
		cin >> u >> v >> w;
		adj[u].emplace_back(w, v);
	}
	for(int i = 1; i <= N; i++){
		sort(adj[i].begin(), adj[i].end());
		deg[adj[i].size()].emplace_back(i);
	}
	for(int a = 1; a <= K; a++) for(int b = 1; b <= a; b++) for(int c = a + 1; c <= K; c++) for(int d = 1; d <= c; d++){
		//deg a at bth, deg c at dth
		st = set<int>();
		bool f = true;
		for(int x : deg[a]){
			if(st.count(adj[x][b - 1].S)){
				can[a][b][c][d] = false;
				f = false;
				break;
			}
			st.insert(adj[x][b - 1].S);
		}
		if(!f) continue;
		for(int x : deg[c]){
			if(st.count(adj[x][d - 1].S)){
				can[a][b][c][d] = false;
				f = false;
				break;
			}
			st.insert(adj[x][d - 1].S);
		}
		if(!f) continue;
		can[a][b][c][d] = true;
	}
	dfs();
	cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <set>
#define pii pair<int,int>
#define F first
#define S second
#define int long long int
using namespace std;

const int maxN = 2e5 + 326;
vector<pii> adj[maxN];
vector<int> deg[12];
set<int> st;

int N, M, K, u, v, w, can[12][12][12][12], self[12][12], cur[12], ans;

void dfs(int lay = 1){
	if(lay == K + 1){
		ans++;
		return;
	}
	for(int i = 1; i <= lay; i++){
		bool f = self[lay][i];
		for(int j = 1; j < lay && f; j++){
			if(!can[j][cur[j]][lay][i]){
				f = false;
				break;
			}
		}
		if(f){
			cur[lay] = i;
			dfs(lay + 1);
		}
	}
}

signed main(){
	cin >> N >> M >> K;
	for(int i = 0; i < M; i++){
		cin >> u >> v >> w;
		adj[u].emplace_back(w, v);
	}
	for(int i = 1; i <= N; i++){
		sort(adj[i].begin(), adj[i].end());
		deg[adj[i].size()].emplace_back(i);
	}
	for(int a = 1; a <= K; a++) for(int b = 1; b <= a; b++){
		st = set<int>();
		self[a][b] = true;
		for(int x : deg[a]){
			if(st.count(adj[x][b - 1].S)){
				self[a][b] = false;
				break;
			}
			st.insert(adj[x][b - 1].S);
		}
		if(!self[a][b]){
			continue;
		}
		self[a][b] = true;
		for(int c = a + 1; c <= K; c++) for(int d = 1; d <= c; d++){
			//deg a at bth, deg c at dth
			st = set<int>();
			bool f = true;
			for(int x : deg[a]){
				if(st.count(adj[x][b - 1].S)){
					can[a][b][c][d] = false;
					f = false;
					break;
				}
				st.insert(adj[x][b - 1].S);
			}
			if(!f) continue;
			for(int x : deg[c]){
				if(st.count(adj[x][d - 1].S)){
					can[a][b][c][d] = false;
					f = false;
					break;
				}
				st.insert(adj[x][d - 1].S);
			}
			if(!f) continue;
			can[a][b][c][d] = true;
		}
	}
	dfs();
	cout << ans << endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <set>
#define pii pair<int,int>
#define F first
#define S second
#define int long long int
using namespace std;

const int maxN = 2e5 + 326;
vector<pii> adj[maxN];
vector<int> deg[12];
set<int> st;

int N, M, K, u, v, w, can[12][12][12][12], cur[12], ans;

void dfs(int lay = 1){
	if(lay == K + 1){
		ans++;
		return;
	}
	for(int i = 1; i <= lay; i++){
		bool f = can[lay][i][lay][i];
		for(int j = 1; j < lay && f; j++){
			if(!can[j][cur[j]][lay][i]){
				f = false;
				break;
			}
		}
		if(f){
			cur[lay] = i;
			dfs(lay + 1);
		}
	}
}

signed main(){
	cin >> N >> M >> K;
	for(int i = 0; i < M; i++){
		cin >> u >> v >> w;
		adj[u].emplace_back(w, v);
	}
	for(int i = 1; i <= N; i++){
		sort(adj[i].begin(), adj[i].end());
		deg[adj[i].size()].emplace_back(i);
	}
	for(int a = 1; a <= K; a++) for(int b = 1; b <= a; b++) for(int c = a; c <= K; c++) for(int d = 1; d <= c; d++){
		//deg a at bth, deg c at dth
		st = set<int>();
		bool f = true;
		for(int x : deg[a]){
			if(st.count(adj[x][b - 1].S)){
				can[a][b][c][d] = false;
				f = false;
				break;
			}
			st.insert(adj[x][b - 1].S);
		}
		if(!f) continue;
		for(int x : deg[c]){
			if(st.count(adj[x][d - 1].S)){
				can[a][b][c][d] = false;
				f = false;
				break;
			}
			st.insert(adj[x][d - 1].S);
		}
		if(!f) continue;
		can[a][b][c][d] = true;
	}
	dfs();
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mxN = 2e5;
const int mxK = 9;

int n, m, k;
int c[mxK];
int dsu[mxK*mxK];
int aux[mxN];
vector<pii> g[mxN];

auto f = [](int i, int j){
    return i*k+j;
};

int rep(int a){
    return (dsu[a] == -1 ? a : dsu[a] = rep(dsu[a]));
}

void merge(int a, int b){
    int repa = rep(a);
    int repb = rep(b);
    if(repa != repb) dsu[repa] = repb;
}

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m >> k;
    memset(dsu, -1, sizeof(dsu));
    memset(aux, -1, sizeof(int)*n);
    for(int i=0; i<m; ++i){
        int u,v,w;
        cin >> u >> v >> w;
        u--; v--;
        g[u].push_back({v,w});
    }
    for(int i=0; i<n; ++i){
        sort(g[i].begin(), g[i].end(), [](pii a, pii b){
                return a.second > b.second;
                });
        // create dsu for pruning
        int s = g[i].size() - 1;
        for(int j=0; j<=s; ++j){
            int nxt = g[i][j].first;
            if(aux[nxt] == -1) aux[nxt] = f(s,j);
            else merge(f(s,j), aux[nxt]);
        }
    }
    // test all solutions
    int ans = 0;
    while(c[0] == 0){
        // prune
        bool prune = false;
        for(int i=1; i<k; ++i)
            for(int j=0; j<i; ++j)
                if(rep(f(i,c[i])) == rep(f(j,c[j])))
                    prune = true;
        // check
        if(!prune){
            int cnt = 0;
            int u = 0;
            do {
                cnt++;
                int s = g[u].size()-1;
                u = g[u][c[s]].first;
            } while(cnt < n && u != 0);
            // cerr << cnt << " " << u << endl;
            if(cnt == n && u == 0) ans++;
        }
        // gen c
        for(int h=k-1; h>=0; --h){
            if(++c[h] > h && h) c[h]=0;
            else break;
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mxN = 2e5;
const int mxK = 9;

int n, m, k;
int c[mxK];
vector<pii> g[mxN];
set<int> preimage[mxN]; // for inyectivity
bool not_disjoint[mxK*mxK][mxK*mxK];

auto f = [](int i, int j){
    return i*k+j;
};

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m >> k;
    for(int i=0; i<m; ++i){
        int u,v,w;
        cin >> u >> v >> w;
        u--; v--;
        g[u].push_back({v,w});
    }
    for(int i=0; i<n; ++i){
        sort(g[i].begin(), g[i].end(), [](pii a, pii b){
                return a.second > b.second;
                });
        int s = g[i].size();
        for(int j=0; j<s; ++j){
            int nxt = g[i][j].first;
            int hsh = f(s-1,j);
            for(int x : preimage[nxt])
                not_disjoint[x][hsh] = not_disjoint[hsh][x] = true;
            preimage[nxt].insert(hsh);
        }
    }
    // test all solutions
    int ans = 0;
    while(c[0] == 0){
        bool iny = true;
        // check iny
        for(int i=0; i<k; ++i)
            for(int j=0; j<=i; ++j)
                if(not_disjoint[f(i,c[i])][f(j,c[j])])
                    iny = false;
        // add to ans
        ans += iny;
        // gen c
        for(int h=k-1; h>=0; --h){
            if(++c[h] > h && h) c[h]=0;
            else break;
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mxN = 2e5;
const int mxK = 9;

int n, m, k;
int c[mxK];
int dsu[mxK*mxK];
int aux[mxN];
vector<pii> g[mxN];
set<int> image[mxK*mxK];

auto f = [](int i, int j){
    return i*k+j;
};

int rep(int a){
    return (dsu[a] == -1 ? a : dsu[a] = rep(dsu[a]));
}

void merge(int a, int b){
    int repa = rep(a);
    int repb = rep(b);
    if(repa != repb) dsu[repa] = repb;
}

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m >> k;
    memset(dsu, -1, sizeof(dsu));
    memset(aux, -1, sizeof(int)*n);
    for(int i=0; i<m; ++i){
        int u,v,w;
        cin >> u >> v >> w;
        u--; v--;
        g[u].push_back({v,w});
    }
    for(int i=0; i<n; ++i){
        sort(g[i].begin(), g[i].end(), [](pii a, pii b){
                return a.second > b.second;
                });
        // create dsu for pruning
        int s = g[i].size() - 1;
        for(int j=0; j<=s; ++j){
            int nxt = g[i][j].first;
            if(aux[nxt] == -1) aux[nxt] = f(s,j);
            else merge(f(s,j), aux[nxt]);
            image[f(s,j)].insert(nxt);
        }
    }
    // test all solutions
    int ans = 0;
    while(c[0] == 0){
        bool iny = true, sur = true;
        // check iny
        for(int i=1; i<k; ++i)
            for(int j=0; j<i; ++j)
                if(rep(f(i,c[i])) == rep(f(j,c[j])))
                    iny = false;
        // check sur
        int cnt = 0;
        for(int i=0; i<k; ++i)
            cnt += image[f(i,c[i])].size();
        sur = (cnt >= n);
        // add to asn
        ans += (iny && sur);
        // gen c
        for(int h=k-1; h>=0; --h){
            if(++c[h] > h && h) c[h]=0;
            else break;
        }
    }
    cout << ans << endl;
}


#include <iostream>
using namespace std;
int main() {
long long b;
cin>>b;
long long n[b];
long long k[b];
long long i[b];
for(int t=0;t<b;t++){
	i[t]=0;
}
    for (int t = 0; t < b; t++) {
cin>> n[t]>>k[t];
}
for(int t=0;t<b;t++)
while(n[t]!=0)	{
if(n[t]%k[t]==0){n[t]=n[t]/k[t];
	}
	else {n[t]=n[t]-1;}
	i[t]++;
}
for(int t=0;t<b;t++){
	cout<<i[t]<<endl;
}
    return 0;}


#include <iostream>
#include <vector>
using namespace std;
int main() {
 int b;
  cin>>b;
long long n[b];
long long k[b];
long long i[b];
for(int t=0;t<b;t++){
	i[t]=0;
}
    for (int t = 0; t < b; t++) {
cin>> n[t]>>k[t];
}
for(int t=0;t<b;t++)
while(n[t]!=0)	{
if(n[t]%k[t]==0){n[t]=n[t]/k[t];
	}
	else {n[t]=n[t]-1;}
	i[t]++;

}
for(int t=0;t<b;t++){
	cout<<i[t]<<endl;
}

    return 0;}


#include <iostream>

using namespace std;

int main() {
	int n,k;
	cin>>n>>k;
	int i=0;
	int a;
while(n!=0)	{
if(n%k==0){n=n/k;
	}
	else {n=n-1;}
	i++;
}
cout<<i;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int t;
ll n, k, ans;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> k;
		while (n > 0) {
			int x = n%k;
			n -= x;
			ans += x
			n/=k;
			if (n !=0) {
			  ans++;
			  break ;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int t;
ll n, k, ans;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> k;
		while (n > 0) {
			int x = n%k;
			n -= x;
			ans += x
			n/=k;
			if (n!=0)
			  ans++;
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int t;
ll n, k, ans;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> k;
		while (n > 0) {
			int x = n%k;
			n -= x;
			ans += x;
				n /= k;
			  ans++;
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int t;
ll n, k, ans;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> k;
		while (n > 0) {
			int x = n%k;
			n -= x;
			ans += x;
			if (n < k) {
				n /= k;
			    ans++;
		  	  break ;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int t;
ll n, k, ans;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> k;
		while (n > 0) {
			int x = n%k;
			n -= x;
			ans += x;
			n /= k;
			ans++;
			if (n < k) {
				ans += n;
				break;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int t;
ll n, k, ans;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> k;
		while (n > 0) {
			int x = n%k;
			n -= x;
			ans += x;
			if (n < k) {
				n /= k;
			  ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int t;
ll n, k, ans;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> k;
		while (n > 0) {
			if (n%k == 0)
				n /= k;
			else
				n--;
			ans++;
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int main() {
	//freopen("input.txt", "r", stdin);
	int t;
	scanf("%d", &t);
	while (t--) {
		ll n, k;
		scanf("%lld %lld", &n, &k);
		ll ans = 0;
		while (n >= 1) {
			if (n%k == 0) {
				n /= k;
				ans++;
			}
			else {
				ans += n % k;
				n -= n % k;
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int t;
ll n, k, ans;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> k;
		while (n > 0) {
			int x = n%k;
			n -= x;
			ans += x;
			if (n !=0 && n<k) {
			n/=k;
			  ans++
			  break;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <utility>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#include <cstdlib>
typedef long long ll;
using namespace std;
int t;
ll n, k, ans;
int main() {
	//freopen("input.txt", "r", stdin);
	cin >> t;
	while (t--) {
		ans = 0;
		cin >> n >> k;
		while (n > 0) {
			int x = n%k;
			n -= x;
			ans += x;
			if (n !=0&&n<k) {
				n /= k;
			  ans++;
			  break ;

			}
		}
		cout << ans << endl;
	}
	return 0;
}


// Probably getting a WA/Tle
// Still trying
// Apna time ayega

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll t,n,m,a,b,x,y,c=0,d,i,e,f,mn=INT_MAX,mx=0;
    cin>>t;
    while(t--){
        int ans=0;
        cin>>a>>b;
        while(1){
            if(a==0)break;
            if(a%b==0 && (a/b)>=0){
                a=a/b;
            }
            else a--;
            ans++;
        }
    cout<<ans<<endl;
    }

    return 0;
}


// Probably getting a WA/Tle
// Still trying
// Apna time ayega

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll t,n,m,a,b,x,y,c=0,d,i,e,f,mn=INT_MAX,mx=0;
    cin>>t;
    while(t--)
    {
        int ans=0;
        cin>>a>>b;
        if(b>a)
            cout<<a<<endl;
        else
        {
            while(1)
            {
                if(a==0)
                    break;
                if(a%b==0 && (a/b)>=0)
                {
                    a=a/b;
                }
                else
                    a--;
                if(b>a){
                    ans+=a;
                    break;
                }
                ans++;
            }
            cout<<ans<<endl;
        }

    }

    return 0;
}


// Probably getting a WA/Tle
// Still trying
// Apna time ayega

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll t,n,m,a,b,x,y,c=0,d,i,e,f,mn=INT_MAX,mx=0;
    cin>>t;
    while(t--)
    {
        int ans=0;
        cin>>a>>b;
        if(b>a)
            cout<<a<<endl;
        else
        {
            while(1)
            {
                if(a==0)
                    break;
                if(a%b==0 && (a/b)>=0)
                {
                    a=a/b;
                    ans++;
                }
                else{
                    ans+=(a%b);
                    a-=(a%b);
                }
            }
            cout<<ans<<endl;
        }

    }

    return 0;
}


// Probably getting a WA/Tle
// Still trying
// Apna time ayega

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll t,n,m,a,b,x,y,c=0,d,i,e,f,mn=INT_MAX,mx=0;
    cin>>t;
    while(t--)
    {
        int ans=0;
        cin>>a>>b;
            while(1)
            {
                if(a==0)
                    break;
                if(a%b==0)
                {
                    a=a/b;
                    ans++;
                }
                else{
                    ans+=(a%b);
                    a-=(a%b);
                }
            }
            cout<<ans<<endl;

    }

    return 0;
}


// Probably getting a WA/Tle
// Still trying
// Apna time ayega

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll t,n,m,a,b,x,y,c=0,d,i,e,f,mn=INT_MAX,mx=0;
    cin>>t;
    while(t--)
    {
        ll ans=0;
        cin>>a>>b;
            while(1)
            {
                if(a%b==0)
                {
                    a=a/b;
                    ans++;
                }
                else{
                    ans+=(a%b);
                    a-=(a%b);
                }
                if(a==0)
                    break;
            }
            cout<<ans<<endl;

    }

    return 0;
}


// Probably getting a WA/Tle
// Still trying
// Apna time ayega

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll t,n,m,a,b,x,y,c=0,d,i,e,f,mn=INT_MAX,mx=0;
    cin>>t;
    while(t--)
    {
        int ans=0;
        cin>>a>>b;
        if(b>a)
            cout<<a<<endl;
        else
        {
            while(1)
            {
                if(a==0)
                    break;
                if(a%b==0 && (a/b)>=0)
                {
                    a=a/b;
                }
                else
                    a--;
                if(b>a){
                    ans+=(a+1);
                    break;
                }
                ans++;
            }
            cout<<ans<<endl;
        }

    }

    return 0;
}


// Probably getting a WA/Tle
// Still trying
// Apna time ayega

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll t,n,m,a,b,x,y,c=0,d,i,e,f,mn=INT_MAX,mx=0;
    cin>>t;
    while(t--)
    {
        int ans=0;
        cin>>a>>b;
        if(b>a)
            cout<<a<<endl;
        else
        {
            while(1)
            {
                if(a==0)
                    break;
                if(a%b==0 && (a/b)>=0)
                {
                    a=a/b;
                }
                else
                    a--;
                ans++;
            }
            cout<<ans<<endl;
        }

    }

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for (int i=0;i<t;i++)
    {
        long long n,c=0,m;
        cin >> n >> m;
        while (n>0)
        {
            if ((n%m)==0)
            {
                n/=m;
                c++;
            }
            else
            {
                n--;
                c++;
            }
        }
        cout << c << endl;
    }

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for (int i=0;i<t;i++)
    {
        long long n,m;
        int c;
        cin >> n >> m;
        while (n>0)
        {
            if ((n%m)==0)
            {
                n/=m;
                c++;
            }
            else
            {
                n--;
                c++;
            }
        }
        cout << c << endl;
    }

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for (int i=0;i<t;i++)
    {
        long long n,c=0,m;
        cin >> n >> m;
        while (n>0)
        {
            if ((n%m)==0)
            {
                n/=m;
                c++;
            }
            else
            {
                n--;
                c++;
            }
            cout << n << endl;
        }
        cout << c << endl;
    }

    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for (int i=0;i<t;i++)
    {
        long long n,m;
        int c=0;
        cin >> n >> m;
        while (n>0)
        {
            if ((n%m)==0)
            {
                n/=m;
                c++;
            }
            else
            {
                n--;
                c++;
            }
        }
        cout << c << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n, k;
int main(){
    int x;
    cin >> x;
    for(int i = 0; i < x; ++i)
    {
long long result = 0;
    	cin >> n >> k;
   while(n > 0)
   {
   	    if(n % k == 0)
   {
         n /= k;
         result++;
   }
   else
   {
        long long remainder = n % k;
        result += remainder;
	        n -= remainder;
   }
   }
    	cout << result << endl;
    }
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin >> t;
    for (int i=0;i<t;i++)
    {
        long long n,m;
        int c=0;
        cin >> n >> m;
        while (n>0)
        {
            if ((n%m)==0)
            {
                n/=m;
                c++;
            }
            else
            {
                n--;
                c++;
            }
        }
        cout << c << endl;
    }

    return 0;
}


#include <cstdio>
#include <iostream>

int main() {
    int t;
    long long count, n, k;
    scanf("%d", &t);

    for (int i = 0; i < t; i++) {
        scanf("%lld %lld", &n, &k);
        count = 0;
        while (n > 0) {
            //if (n % k == 0) {
            //    n /= k;
            //    count++;
            //}
            //else {
            //    count += n % k;
            //    n -= n % k;
            //}
            n = (n % k == 0) ? n / k : --n;
            count++;
        }
        printf("%lld\n", count);
    }
}


#include <cstdio>
#include <iostream>

int main() {
    int t, n, k;
    scanf("%d", &t);
    int count;

    for (int i = 0; i < t; i++) {
        scanf("%d %d", &n, &k);
        count = 0;
        while (n > 0) {
            n = (n % k == 0) ? n / k : --n;
            count++;
        }
        printf("%d\n", count);
    }
}


#include <cstdio>
#include <iostream>
#include <vector>

int main() {
    int t, count;
    unsigned long int n, k;
    scanf("%d", &t);
    std::vector<int> v;

    for (int i = 0; i < t; i++) {
        scanf("%ld %ld", &n, &k);
        count = 0;
        while (n > 0) {
            n = (n % k == 0) ? n / k : --n;
            count++;
        }
        v.push_back(count);
    }
    for (auto i : v)
        printf("%d\n", i);
}


#include <cstdio>
#include <iostream>

int main() {
    int t;
    long long count, n, k;
    scanf("%d", &t);

    for (int i = 0; i < t; i++) {
        scanf("%lld %lld", &n, &k);
        count = 0;
        while (n > 0) {
            if (n % k == 0) {
                n /= k;
                count++;
            }
            else {
                count += n % k;
                n -= n % k;
            }
        }
        printf("%lld\n", count);
    }
}


#include <cstdio>
#include <iostream>

int main() {
    int t, count;
    unsigned long int n, k;
    scanf("%d", &t);

    for (int i = 0; i < t; i++) {
        scanf("%ld %ld", &n, &k);
        count = 0;
        while (n > 0) {
            n = (n % k == 0) ? n / k : --n;
            count++;
        }
        printf("%d\n", count);
    }
}


#include <cstdio>
#include <iostream>

int main() {
    int t;
    long long count, n, k;
    scanf("%d", &t);

    for (int i = 0; i < t; i++) {
        scanf("%ld %ld", &n, &k);
        count = 0;
        while (n > 0) {
            if (n % k == 0) {
                n /= k;
                count++;
            }
            else {
                count += n % k;
                n -= n % k;
            }
            //n = (n % k == 0) ? n / k : --n;
            //count++;
        }
        printf("%d\n", count);
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	cin>>test;

	while(test>0)
	{
		long long n,k;
	     cin>>n>>k;
		long long count=0;
	while(n>0)
	{
		if(n%k==0)
		{
			n/=k;

			count++;
		}
		else
		{
			count++;
			n--;
		}

	}
	cout<<count<<endl;
	test--;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	cin>>test;
	while(test>0)
	{
		long long n,k;
	    cin>>n>>k;
		long long count=0;
	while(n>0)
	{
		while(n%k==0)
		{
		n=n/k;
		count++;
		}
	    n--;
		count++;
	}
	cout<<count<<endl;
	test--;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	cin>>test;
	long long n,k;
	cin>>n>>k;
	long long i=n,count=0;
	while(test>0)
	{
	while(i>=0)
	{
		if(i%k==0)
		{
			i=i/k;
			i--;
			count++;
		}
		else
		{
			i--;
			count++;
		}
	}
	cout<<count<<endl;
	test--;
	count=0;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	cin>>test;
	long long n,k;
	cin>>n>>k;
	long long i=n,count=0;
	while(test>0)
	{
	while(i>=0)
	{
		if(i%k==0)
		{
			i=i/k;
			i--;
			count++;
		}
		else
		{
			i--;
			count++;
		}
	}
	cout<<count;
	test--;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	cin>>test;
	while(test>0)
	{
		long long n,k;
	    cin>>n>>k;
		long long count=0;
	while(n>0)
	{
		if(n%k==0)
		n=n/k;
		else
	    n--;
		count++;
	}
	cout<<count<<endl;
	test--;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	cin>>test;

	while(test>0)
	{
		long long n,k;
	     cin>>n>>k;
		long long i=n,count=0;
	while(i>=0)
	{
		if(i%k==0)
		{
			i=i/k;
			i--;
			count++;
		}
		else
		{
			i--;
			count++;
		}
	}
	cout<<count<<endl;
	test--;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	long long n,k,count=0;
	cin>>test;
	while(test--)
	{
		count=0;
	    cin>>n>>k;
	while(n)
	{
		count+=n%k;
		n/=k;
		count++;
	}
	cout<<count-1<<endl;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	cin>>test;
	long long n,k;
	cin>>n>>k;
	long long i=n,count=0;
	while(i>=0)
	{
		if(i%k==0)
		{
			i=i/k;
			i--;
			count++;
		}
		else
		{
			i--;
			count++;
		}
	}
	cout<<count;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	cin>>test;
	while(test>0)
	{
		long int n,k;
	    cin>>n>>k;
		long int count=0;
	while(n>0)
	{
		while(n%k==0)
		{
		n=n/k;
		count++;
		}
	    n--;
		count++;
	}
	cout<<count<<endl;
	test--;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	scanf("%d",&test);
	while(test>0)
	{
		long long n,k;
	    scanf("%d%d",&n,&k);
		long long count=0;
	while(n>0)
	{
		if(n%k==0)
		n=n/k;
		else
	    n--;
		count++;
	}
	printf("%d\n",count);
	test--;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	long long n,k,count=0;
	cin>>test;
	while(test--)
	{
		count=0;
	    cin>>n>>k;
	while(n)
	{
		if(n%k==0)
		n=n/k;
		else
	    n--;
		count++;
	}
	cout<<count<<endl;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	long long n,k,count=0;
	cin>>test;
	while(test>0)
	{
		count=0;
	    cin>>n>>k;
	while(n>0)
	{
		if(n%k==0)
		n=n/k;
		else
	    n--;
		count++;
	}
	cout<<count<<endl;
	test--;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int test;
	scanf("%d",&test);
	while(test>0)
	{
		long long n,k;
	    scanf("%ld%ld",&n,&k);
		long long count=0;
	while(n>0)
	{
		if(n%k==0)
		n=n/k;
		else
	    n--;
		count++;
	}
	printf("%ld\n",count);
	test--;
}
}


#include <iostream>
using namespace std;

int main()
{
    int dal, skait, a=0, cik;
    cin  >> cik;
    for(int i=0; i<cik;i++)
    {
        cin >> skait >> dal;
        while(true){
            a++;
            if(skait%dal==0)
            {
                skait = skait / dal;
            }
            else
            {
                skait--;
            }
            if(skait==0)
            {
                break;
            }
        }
        cout << a << endl;
    }
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    long long x, y, gaj=0, cik;
    cin >> cik;
    int atb[cik];
    for(int a=0; a<cik; a++){
        gaj=0;
        cin >> x >> y;
        while(true){
            gaj += x%y;
            x=x/y;
            gaj++;
            if(x==0){
                atb[a]=gaj;
                break;
            }
        }
    }
    for(int a=0; a<cik; a++){cout << atb[a]-1 << endl;}
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    long long x, y, gaj=0, cik;
    cin >> cik;
    long long atb[cik];
    for(int a=0; a<cik; a++){
        gaj=0;
        cin >> x >> y;
        while(true){
            gaj += x%y;
            x=x/y;
            gaj++;
            if(x==0){
                atb[a]=gaj;
                break;
            }
        }
    }
    for(int a=0; a<cik; a++){cout << atb[a]-1 << endl;}
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    long x, y, gaj=0, cik;
    cin >> cik;
    int atb[cik];
    for(int a=0; a<cik; a++){
        gaj=0;
        cin >> x >> y;
        while(true){
            gaj += x%y;
            x=x/y;
            gaj++;
            if(x==0){
                atb[a]=gaj;
                break;
            }
        }
    }
    for(int a=0; a<cik; a++){cout << atb[a]-1 << endl;}
//
}


#include <iostream>
using namespace std;

int main()
{
    int dal, skait, cik;
    cin  >> cik;
    int atb[cik];
    for(int i=0; i<cik;i++)
    {
        int a=0;
        cin >> skait >> dal;
        while(true){
            a++;
            if(skait%dal==0)
            {
                skait = skait / dal;
            }
            else
            {
                skait--;
            }
            if(skait==0)
            {
                break;
            }
        }
        atb[i] = a;
    }
    for(int p=0; p<cik; p++){
        cout << atb[p] << endl;
    }
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    long x, y, gaj=0, cik;
    cin >> cik;
    int atb[cik];
    for(int a=0; a<cik; a++){
        gaj=0;
        cin >> x >> y;
        while(true){
            gaj += x%y;
            x=x/y;
            gaj++;
            if(x==0){
                atb[a]=gaj;
                break;
            }
        }
    }
    for(int a=0; a<cik; a++){cout << atb[a]-1 << endl;}
}


#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    long x, y, gaj=0, cik;
    cin >> cik;
    for(int a=0; a<cik; a++){
        gaj=0;
        cin >> x >> y;
        while(true){
            gaj += x%y;
            if(x==0){
                break;
            }
            x=x/y;
            gaj++;
        }
        cout << gaj-1;
    }
}


#include <iostream>
using namespace std;

int main()
{
    long dal, skait, cik;
    cin  >> cik;
    int atb[cik];
    for(int i=0; i<cik;i++)
    {
        cin >> skait >> dal;
        int a=0;
        while(true){
            a++;
            if(skait%dal==0)
            {
                skait = skait / dal;
            }
            else
            {
                skait--;
            }
            if(skait==0)
            {
                atb[i] = a;
                break;
            }
        }
    }
    for(int p=0; p<cik; p++){
        cout << atb[p] << endl;
    }
}


#include <iostream>
using namespace std;

int main()
{
    int dal, skait, cik;
    cin  >> cik;
    for(int i=0; i<cik;i++)
    {
        int a=0;
        cin >> skait >> dal;
        while(true){
            a++;
            if(skait%dal==0)
            {
                skait = skait / dal;
            }
            else
            {
                skait--;
            }
            if(skait==0)
            {
                break;
            }
        }
        cout << a << endl;
    }
}


#include <iostream>
using namespace std;

int main()
{
    long dal, skait, cik;
    cin  >> cik;
    int atb[cik];
    for(int i=0; i<cik;i++)
    {
        cin >> skait >> dal;
        int a=0;
        while(true){
            a++;
            if(skait%dal==0)
            {
                skait = skait / dal;
            }
            else
            {
                skait--;
            }
            if(skait==0)
            {
                cout << a;
                break;
            }
        }
    }
    /*for(int p=0; p<cik; p++){
        cout << atb[p] << endl;
    }*/
}


#include<iostream>
using namespace std;
 main() {
  int t,s=0;
  long long n,k;
  cin>>t;
  for(int i=0;i<t;i++)
  {
    cin>>n>>k;
    while(n!=0){
    if(n%k==0)
    n/=k;
    else
    n--;
    s++;}
      cout<<s<<"\n";
  }
}


#include<iostream>
using namespace std;
 main() {
  int t;
  long long n,k,s;
  cin>>t;
  for(int i=0;i<t;i++)
  {
      s=0;
    cin>>n>>k;
    while(n!=0){
    if(n%k==0){
    n/=k;
  s++;}
    else
    {
    long long x=n%k;
  n-=x;
s+=x;}
    }
      cout<<s<<"\n";
  }
}


#include<iostream>
using namespace std;
 main() {
  int t,n,k,s=0;
  cin>>t;
  for(int i=0;i<t;i++)
  {
    cin>>n>>k;
    while(n!=0){
    if(n%k==0)
    n/=k;
    else
    n--;
    s++;}
      cout<<s<<"\n";
  }
}


#include<iostream>
using namespace std;
 main() {
  int t;
  long long n,k,s;
  cin>>t;
  for(int i=0;i<t;i++)
  {
      s=0;
    cin>>n>>k;
    while(n!=0){
    if(n%k==0)
    n/=k;
    else
    n--;
    s++;}
      cout<<s<<"\n";
  }
}


#include <iostream>

using namespace std;

main()
{
     int t;
        long n,k;
    cin>>t;
        for (int i = 0; i <t ; i++) {
            int s=0;
           cin>>n>>k;
            while(n!=0){
                if(n%k==0)
                    n/=k;
                else n--;
                s++;
            }
            cout<<s;
        }
}


#include <iostream>

using namespace std;

main()
{
    int t,n,k,s;
    cin>>t;
    for (int i = 0; i < t; i++) {
        s=0;
        cin>>n>>k;
        while(n!=0)
        {
            if(n%k==0)
            n/=k;
            else
            n--;
            s++;
        }
        std::cout << s << std::endl;
    }
}


#include<iostream>
using namespace std;
 main() {
  int t,k,s=0;
  long long n;
  cin>>t;
  for(int i=0;i<t;i++)
  {
    cin>>n>>k;
    while(n!=0){
    if(n%k==0)
    n/=k;
    else
    n--;
    s++;}
      cout<<s<<"\n";
  }
}


#include<iostream>
using namespace std;
 main() {
  int t,n,k,s=0;
  cin>>t;
  for(int i=0;i<t;i++)
  {
      cin>>n>>k;
    while(n!=0){
    if(n%k==0)
    n/=k;
    else
    n--;
    s++;}
      cout<<s<<"\n";
  }
}


#include<iostream>
using namespace std;
 main() {
  int t;
  long long n,k,s;
  cin>>t;
  for(int i=0;i<t;i++)
  {
      s=0;
    cin>>n>>k;
    while(n!=0){
    if(n%k==0){
    n/=k;
  s++;}
    else
    {
    int x=n%k;
  n-=x;
s+=x;}
    }
      cout<<s<<"\n";
  }
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
int main()
{
	long long n, k;
	int cnt = 0;
	int t;
	scanf("%d", &t);
	while (t--)
	{
		cnt = 0;
		scanf("%lld%lld", &n, &k);
		while (n)
		{
			if (n % k == 0)
				n /= k;
			else
				n -= 1;
			++cnt;
		}
		printf("%d\n", cnt);
	}
//	system("pause");
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
int bfs(long long n, long long k)
{

}
int main()
{
	long long n, k;
	int cnt = 0;
	int t;
	scanf("%d", &t);
	while (t--)
	{
		cnt = 0;
		scanf("%lld%lld", &n, &k);
		while (n)
		{
			if (n % k == 0)
			{
				n /= k;
				++cnt;
			}
			else
			{
				cnt += n % k;
				n -= n % k;
			}
		}
		printf("%d\n", cnt);
	}
//	system("pause");
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
struct node
{
	long long n, step;
	bool operator < (const node& other)const
	{
		return step > other.step;
	}
} temp, kk;
int bfs(long long n, long long k)
{
	priority_queue<node> q;
	q.push(node{ n,0 });
	while (!q.empty())
	{
		kk = q.top();
		if (kk.n == 1)return kk.step + 1;
		if (kk.n == 0)return kk.step;
		q.pop();
		if (kk.n % k == 0)
		{
			q.push(node{ kk.n / k,kk.step + 1 });
			q.push(node{ kk.n - k,kk.step + k });
		}
		else
		{
			q.push(node{ kk.n - kk.n % k,kk.step + kk.n % k });
		}
	}
}
int main()
{
	long long n, k;
	int cnt = 0;
	int t;
	scanf("%d", &t);
	while (t--)
	{
		cnt = 0;
		scanf("%lld%lld", &n, &k);
		printf("%d\n", bfs(n, k));
	}
//	system("pause");
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
struct node
{
	long long n, step;
	bool operator < (const node& other)const
	{
		if (step != other.step)
			return step > other.step;
		return n > other.n;
	}
} temp, kk;
int bfs(long long n, long long k)
{
	priority_queue<node> q;
	q.push(node{ n,0 });
	while (!q.empty())
	{
		kk = q.top();
		if (kk.n == 1)return kk.step + 1;
		if (kk.n == 0)return kk.step;
		q.pop();
		if (kk.n % k == 0)
		{
			q.push(node{ kk.n / k,kk.step + 1 });
			q.push(node{ kk.n - k,kk.step + k });
		}
		else
		{
			q.push(node{ kk.n - kk.n % k,kk.step + kk.n % k });
		}
	}
}
int main()
{
	long long n, k;
	int cnt = 0;
	int t;
	scanf("%d", &t);
	while (t--)
	{
		cnt = 0;
		scanf("%lld%lld", &n, &k);
		printf("%d\n", bfs(n, k));
	}
//	system("pause");
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
struct node
{
	long long n, step;
	bool operator < (const node& other)const
	{
		if (step != other.step)
			return step > other.step;
		return n > other.n;
	}
} temp, kk;
int bfs(long long n, long long k)
{
	priority_queue<node> q;
	q.push(node{ n,0 });
	while (!q.empty())
	{
		kk = q.top();
		if (kk.n == 1)return kk.step + 1;
		if (kk.n == 0)return kk.step;
		q.pop();
		if (kk.n % k == 0)
		{
			q.push(node{ kk.n / k,kk.step + 1 });
			q.push(node{ kk.n - k,kk.step + k });
		}
		else
		{
			q.push(node{ kk.n - kk.n % k,kk.step + kk.n % k });
		}
	}
}
int main()
{
	long long n, k;
	long long cnt = 0;
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%lld%lld", &n, &k);
		cnt = 0;
		while (n)
		{
			if (n % k == 0)
			{
				n /= k, ++cnt;
			}
			else
			{
				cnt += n % k;
				n -= n % k;
			}
		}
		printf("%lld\n", cnt);
	}
//	system("pause");
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
struct node
{
	long long n, step;
	bool operator < (const node& other)const
	{
		if (step != other.step)
			return step > other.step;
		return n > other.n;
	}
} temp, kk;
int bfs(long long n, long long k)
{
	priority_queue<node> q;
	q.push(node{ n,0 });
	while (!q.empty())
	{
		kk = q.top();
		if (kk.n == 1)return kk.step + 1;
		if (kk.n == 0)return kk.step;
		q.pop();
		if (kk.n % k == 0)
		{
			q.push(node{ kk.n / k,kk.step + 1 });
			q.push(node{ kk.n - k,kk.step + k });
		}
		else
		{
			q.push(node{ kk.n - kk.n % k,kk.step + kk.n % k });
		}
	}
}
int main()
{
	long long n, k;
	long long cnt = 0;
	int t;
	scanf("%d", &t);
	while (t--)
	{
		cnt = 0;
		while (n)
		{
			if (n % k == 0)
			{
				n /= k, ++cnt;
			}
			else
			{
				cnt += n % k;
				n -= n % k;
			}
		}
		printf("%lld\n", cnt);
	}
//	system("pause");
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <cstdlib>
#include <bitset>
#define lowbit(x) (x&(-x))
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
using namespace std;
int main()
{
	long long n, k;
	int cnt = 0;
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%lld%lld", &n, &k);
		while (n)
		{
			if (n % k == 0)
				n /= k;
			else
				n -= 1;
			++cnt;
		}
		printf("%d\n", cnt);
	}
}


#include <iostream>
#include <math.h>
#include <algorithm>
#define ll long long
using namespace std;
ll n, a, b, c, d, m, z;
bool f = 0;
int main()
{
	cin >> m;

	while (m--)
	{
		cin >> n >> a;
		if(a==1){
		cout << n<< endl ;
	}
	else{
		while (n != 0)
		{
			if (n % a == 0)
			{
				n /= a;
				d++;
			}
			else
			{
				n--;
				d++;
			}
		}
		cout << d << endl;
		d = 0;
	}
	}
}


#include <iostream>
#include <math.h>
#include <algorithm>
#define ll long long
using namespace std;
ll n, a, b, c, d, m, z;
bool f = 0;
int main()
{
	cin >> m;

	while (m--)
	{
		cin >> n >> a;
		if(a>n)
		cout << n<< endl ;
		else{
		while (n != 0)
		{
			if (n % a == 0)
			{
				n /= a;
				d++;
			}
			else
			{
				n--;
				d++;
			}
		}
		cout << d << endl;
		d = 0;
	}
	}

}


#include <iostream>
#include <math.h>
#include <algorithm>
#define ll long long
using namespace std;
ll n, a, b, c, d, m, z, p ;
bool f = 0;
int main()
{
	cin >> m;

	while (m--)
	{
		cin >> n >> a;
		while (n!=0)
		{
			if (n % a == 0)
			{
				n /= a;
				d++;
			}
			else
			{
				c= n%a;
			     d+=c;
			     n-=c;
		}
		}
		cout << d << endl;
		d = 0;

	}

}


#include <iostream>
#include <math.h>
#include <algorithm>
#define ll long long
using namespace std;
ll n, a, b, c, d, m, z, p ;
bool f = 0;
int main()
{
	cin >> m;

	while (m--)
	{
		cin >> n >> a;
		while (n >= a)
		{
			if (n % a == 0)
			{
				n /= a;
				d++;
			}
			else
			{
				n--;
				d++;
			}
		}
		cout << d+n << endl;
		d = 0;

	}

}


#include <iostream>
#include <math.h>
#include <algorithm>
#define ll long long
using namespace std;
ll n, a, b, c, d, m, z;
bool f = 0;
int main()
{
	cin >> m;
	while (m--)
	{
		cin >> n >> a;
		while (n != 0)
		{
			if (n % a == 0)
			{
				n /= a;
				d++;
			}
			else
			{
				n--;
				d++;
			}
		}
		cout << d << endl;
		d = 0;
	}
}


#include <iostream>
#include <math.h>
#include <algorithm>
#define ll long long
using namespace std;
ll n, a, b, c, d, m, z;
bool f = 0;
int main()
{
	cin >> m;

	while (m--)
	{
		cin >> n >> a;
		while (n >= a)
		{
			if (n % a == 0)
			{
				n /= a;
				d++;
			}
			else
			{
				n--;
				d++;
			}
		}
		cout << d+n << endl;
		d = 0;
	}

}


#include <bits/stdc++.h>
using namespace std;

long long n, k, t;
int c = 0;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> t;
  long long results[t];
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    c = 0;
    while (n) {
      long long step = n % k;
      c += step;
      n -= step;
      if (n) {
        n /= k;
        c++;
      }
    }
    results[i] = c;
  }

  for (int i = 0; i < t; i++) {
    cout << results[i];
    if (i != t - 1)
      cout << '\n';
  }
}


#include <bits/stdc++.h>
using namespace std;

long long n, k, t;
int c = 0;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> t;
  long long results[t];
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    c = 0;
    while (n) {
      long long step = n % k;
      c += step;
      n -= step;
      if (n) {
        n /= k;
        c++;
      }
    }
    results[i] = c;
  }

  for (int i = 0; i < t; i++) {
    cout << results[i] << '\n';
  }
}


#include <bits/stdc++.h>
using namespace std;

unsigned long long n, k, t;
int c = 0;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> t;
  unsigned long long results[t];
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    c = 0;
    while (n > 0) {
      unsigned long long step = n % k;
      c += step;
      n -= step;
      if (n != 0) {
        n /= k;
        c++;
      }
    }
    results[i] = c;
  }

  for (int i = 0; i < t; i++) {
    cout << results[i];
    if (i != t - 1)
      cout << '\n';
  }
}


#include <bits/stdc++.h>
using namespace std;

unsigned long long n, k, t;
int c = 0;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> t;
  unsigned long long results[t];
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    c = 0;
    while (n) {
      unsigned long long step = n % k;
      c += step;
      n -= step;
      if (n) {
        n /= k;
        c++;
      }
    }
    results[i] = c;
  }

  for (int i = 0; i < t; i++) {
    cout << results[i];
    if (i != t - 1)
      cout << '\n';
  }
}


#include <bits/stdc++.h>
using namespace std;

unsigned long long n, k, t;
unsigned long long c = 0;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> t;
  unsigned long long results[t];
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    c = 0;
    while (n > 0) {
      if (k > n) {
        c = n;
        break;
      }
      unsigned long long step = n % k;
      c += step;
      n -= step;
      if (n != 0) {
        n /= k;
        c++;
      }
    }
    results[i] = c;
  }

  for (int i = 0; i < t; i++) {
    cout << results[i];
    if (i != t - 1)
      cout << '\n';
  }
  cin >> n;
}


#include <bits/stdc++.h>
using namespace std;

unsigned long long n, k, t;
int c = 0;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> t;
  unsigned long long results[t];
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    c = 0;
    while (n) {
      unsigned long long step = n % k;
      c += step;
      n -= step;
      if (n != 0) {
        n /= k;
        c++;
      }
    }
    results[i] = c;
  }

  for (int i = 0; i < t; i++) {
    cout << results[i];
    if (i != t - 1)
      cout << '\n';
  }
}


#include <bits/stdc++.h>
using namespace std;

long long n, k, t;
int c = 0;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> t;
  long long results[t];
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    c = 0;
    while (n) {
      int step = n % k;
      if (step == 0) {
        c++;
        n /= k;
      } else {
        n -= step;
        c += step;
      }
    }
    results[i] = c;
  }

  for (int i = 0; i < t; i++) {
    cout << results[i] << '\n';
  }
}


#include <bits/stdc++.h>
using namespace std;

unsigned long long n, k, t;
unsigned long long c = 0;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> t;
  unsigned long long results[t];
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    c = 0;
    if (k <= n) {
      while (n > 0) {
        unsigned long long step = n % k;
        c += step;
        n -= step;
        if (n != 0) {
          n /= k;
          c++;
        }
      }
    } else {
      c = n;
    }
    results[i] = c;
  }

  for (int i = 0; i < t; i++) {
    cout << results[i];
    if (i != t - 1)
      cout << '\n';
  }
  cin >> n;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    long long t;
    cin>>t;
    for(ll i=0;i<t;i++)
    {
        long long s=0;
        long long n,k,z;
        cin>>n>>k;
        if(n<k){cout<<n<<endl;}
        else
        {
             while(n>0)
        {
            if(n%k!=0)
            {

                s=s+(n%k);
                n=n-(n%k);
            }
            if(n%k==0 && n!=0)
            {
                n=n/k;
                s++;
            }

        }
        cout<<s<<endl;
            }
        }

    return 0;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        long s=0;
        long long n,k,z;
        cin>>n>>k;
        while(n>0)
        {
            if(n%k!=0)
            {

                s=s+(n%k);
                n=n-(n%k);
            }
            if(n%k==0)
            {
                n=n/k;
                s++;
            }

        }
        cout<<s-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        long s=0;
        long long n,k;
        cin>>n>>k;
        while(n!=0)
        {
            if(n%k==0)
            {
                n=n/k;
                s++;
            }
            else
            {
                n=n-1;
                s++;
            }

        }
        cout<<s<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        long s=0;
        long long n,k,z;
        cin>>n>>k;
        while(n!=0)
        {
            if(n%k==0)
            {
                n=n/k;
                s++;
            }
            else
            {
                 s=s+n%k;
                n=n-n%k;

            }

        }
        cout<<s<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        long s=0;
        long long n,k,z;
        cin>>n>>k;
        while(n!=0)
        {
            if(n%k==0)
            {
              while(n%k==0)
              {
                  n=n/k;
                  s++;
              }
            }
            else
            {
                n=n-1;
                s++;
            }

        }
        cout<<s<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        long s=0, flag=0;
        long long n,k,z;
        cin>>n>>k;
        if(n<k){cout<<n<<endl;}
        else
        {
             while(n>0)
        {
            flag=flag+(n%k);
           // if(n%k==0 && n!=0)
            //{
                n=n/k;
                s++;
            //}

        }
        s=s+flag-1;
        cout<<s<<endl;
            }
        }

    return 0;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        long s=0;
        long long n,k,z;
        cin>>n>>k;
        if(n<k){cout<<n<<endl;}
        else
        {
             while(n>0)
        {
            if(n%k!=0)
            {

                s=s+(n%k);
                n=n-(n%k);
            }
            if(n%k==0 && n!=0)
            {
                n=n/k;
                s++;
            }

        }
        cout<<s<<endl;
            }
        }

    return 0;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        long s=0;
        long long n,k,z;
        cin>>n>>k;
        while(n!=0)
        {
            if(n%k==0)
            {
                double a=log(n)/log(k);
                if(floor(a)==ceil(a))
                {
                    s=s+a;
                    n=1;
                }
              while(n%k==0)
              {
                  n=n/k;
                  s++;
              }
            }
            else
            {
                n=n-1;
                s++;
            }

        }
        cout<<s<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        long s=0;
        long long n,k,z;
        cin>>n>>k;
        while(n!=0)
        {
            if(n%k==0)
            {
                double a=log(n)/log(k);
                if(floor(a)==ceil(a))
                {
                    s=s+a;
                    n=1;
                }
                else{
              while(n%k==0)
              {
                  n=n/k;
                  s++;
              }}
            }
            else
            {
                n=n-1;
                s++;
            }

        }
        cout<<s<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#include<vector>
#define ll long long
using namespace std;

int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        long s=0;
        long long n,k,z;
        cin>>n>>k;
        while(n>0)
        {
            if(n%k!=0)
            {

                s=s+(n%k);
                n=n-(n%k);
            }
            if(n%k==0 && n!=0)
            {
                n=n/k;
                s++;
            }

        }
        cout<<s<<endl;
    }
    return 0;
}


#include <stdio.h>

int main(){
    int n;
    scanf("%d",&n);
    long long a,b;
    for(int i=0; i<n; i++){
        scanf("%lld%lld",&a,&b);
        long long cont = 0;
        long long al = 0;
        if(b>a/2 and b!=a){
            printf("%lld\n",a);
            al = 1;
        }
        while(a!=0 and al!=1){
            if(a%b==0){
                a=a/b;
                cont++;
            }
            else{
                a--;
                cont++;
            }
        }
        if(a == 0){
            printf("%lld\n",cont);
        }
    }
    return 0;
}


var t = parseInt(readline());
while(t-- > 0){
    var n = parseInt(readline());
    print(`1 ${n-1}`);
}


var test = readline(), x, b;
while(test--) {
  x = readline();
  b = x - 1;
  print('1 ' + b);
}


var test = readline(), x;
while(test--) {
  x = readline();
  print('1 ' + (x - 1));
}


#include <bits/stdc++.h>
using namespace std;

int main() {
ios::sync_with_stdio(0);
cin.tie(0);
	int caso;
	cin>>caso;
	while(caso--){
		int n;
		cin>>n;
		cout<<1<<" "<<n-1<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

int main() {
ios::sync_with_stdio(0);
cin.tie(0);
	int caso;
	cin>>caso;
	while(caso--){
		int n;
		cin>>n;
		bool p=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				long long a=__gcd(i,j);
				long long b=(i*j)/__gcd(i,j);
				if(a+b==n){
					cout<<a<<" "<<b;
					p=1;
					goto final;
				}
			}
		}
		final:
		if(p) cout<<'\n';

	}
}


#include <bits/stdc++.h>
using namespace std;

int main() {
ios::sync_with_stdio(0);
cin.tie(0);
	int caso;
	cin>>caso;
	while(caso--){
		int n;
		cin>>n;
		bool p=0;
		for(int i=1;i<=ceil(n/2);i++){
			for(int j=1;j<=ceil(n/2);j++){
				long long a=__gcd(i,j);
				long long b=(i*j)/__gcd(i,j);
				if(a+b==n){
					cout<<a<<" "<<b;
					p=1;
					goto final;
				}
			}
		}
		final:
		if(p) cout<<'\n';

	}
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	int caso;
	cin>>caso;
	while(caso--){
		int n;
		cin>>n;
		bool p=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int a=__gcd(i,j);
				int b=(i*j)/__gcd(i,j);
				if(a+b==n){
					cout<<a<<" "<<b;
					p=1;
					goto final;
				}
			}
		}
		final:
		if(p) cout<<'\n';

	}
}


// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <bits/stdc++.h>

using namespace std;

int main() {

	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {

		int x;
		cin >> x;
		printf("%d", x - 1);
	}
	return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file


// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <bits/stdc++.h>

using namespace std;

int main() {

	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int x;
		cin >> x;

		int a = 0, b = 0;
		if (x % 2 == 0) {
			a = x / 2;
			b = x / 2;

		}
		else {
			a = x - 1;
			b = 1;
		}
		cout << a << " " << b << endl;

	}
	return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file


int main() {

	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int x;
		cin >> x;
		printf("%d", x - 1);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {

	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int x;
		cin >> x;
		cout << 1 << "" << x - 1 << endl;

	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {

	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int x;
		cin >> x;

		int a = 0, b = 0;
		if (x % 2 == 0) {
			a = x / 2;
			b = x / 2;

		}
		else {
			a = x - 1;
			b = 1;
		}
		cout << a << " " << b << endl;

	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
        long long t,x;
        cin>>t;
        while(t--)
        {
                cin>>x;
                cout<<1<<" "<<x-1<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>

using namespace std;

long long gl(long long a,long long b)
{
     long long temp=0;
     while(b>0)
     {
             temp=b;
             b=a%b;
             a=temp;
     }
     return a;
}
long long gl1(long long a,long long b)
{
      long long c,d;
      c=max(a,b);
      while(1)
      {
              if(c%a==0 && c%b==0)
              {
                  d=c;
                  break;
              }
              c++;
      }
      return d;
}
int main()
{
    long long x,i,j,k,p;
    cin>>x;
    for(i=1; i<=x; i++)
    {
            for(j=1;j<=x;j++)
            {
                 k=gl(i,j);
                // cout<<k<<endl;
                 p=gl1(i,j);
                 //cout<<p<<endl;
                 if(p+k==x)
                 {
                         cout<<i<<" "<<j<<endl;
                 }
                 break;
            }
           // break;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

long long gl(long long a,long long b)
{
     long long temp=0;
     while(b>0)
     {
             temp=b;
             b=a%b;
             a=temp;
     }
     return a;
}
/*long long gl1(long long a,long long b)
{
      long long c,d;

      c=max(a,b);
      while(1)
      {
              if(c%a==0 && c%b==0)
              {
                  d=c;
                  break;
              }
              c++;
      }
      return d;
}*/
int main()
{
    long long x,i,j,k,p,t;
    cin>>t;
    while(t--){
    cin>>x;
    for(i=1; i<=x; i++)
    {
            for(j=1;j<=x;j++)
            {
                 k=gl(i,j);
                 p=(i*j)/k;
                // cout<<k<<endl;
                 //p=gl1(i,j);
                 //cout<<p<<endl;
                 if(p+k==x)
                 {
                         cout<<i<<" "<<j<<endl;
                 }
                 break;
            }
           // break;
    }
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

long long gl(long long a,long long b)
{
     long long temp=0;
     while(b>0)
     {
             temp=b;
             b=a%b;
             a=temp;
     }
     return a;
}
long long gl1(long long a,long long b)
{
      long long c,d;
      c=max(a,b);
      while(1)
      {
              if(c%a==0 && c%b==0)
              {
                  d=c;
                  break;
              }
              c++;
      }
      return d;
}
int main()
{
    long long x,i,j,k,p,t;
    cin>>t;
    while(t--){
    cin>>x;
    for(i=1; i<=x; i++)
    {
            for(j=1;j<=x;j++)
            {
                 k=gl(i,j);
                // cout<<k<<endl;
                 p=gl1(i,j);
                 //cout<<p<<endl;
                 if(p+k==x)
                 {
                         cout<<i<<" "<<j<<endl;
                 }
                 break;
            }
           // break;
    }
    }

    return 0;
}


#include <iostream>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
	int x;
	cin>>x;
	cout<<1<<" "<<x-1<<endl;
    }

	return 0;
}


#include <iostream>
#include <cstdio>
//#include<bits/stdhc++.h>
using namespace std;
int gcd(int x,int y)
{
    for(int i=x;i>=1;i--)
    if(x%i==0 && y%i==0)
    return i;
}
int lcm(int x,int y)
{
    for(int i=x;i<=x*y;i++)
    if(i%x==0 && i%y==0)
    return i;
}
int main() {
    int t;
    cin>>t;
    while(t--)
    {

        long long a=1,b=1,x;
        cin>>x;
        for(a=1;a<=x;a++)
        {
            for(b=1;b<=a;b++)
            {
                if((gcd(a,b)+lcm(a,b)==x) && (gcd(a,b)*lcm(a,b)==a*b))
                {
                    cout<<a<<" "<<b<<endl;
                    goto label;

                }
            }

        }
        label: a=1;

    }

    return 0;
}


#include <iostream>
#include <cstdio>
//#include<bits/stdhc++.h>
using namespace std;
int gcd(int x,int y)
{
    for(int i=x;i>=1;i--)
    if(x%i==0 && y%i==0)
    return i;
}
int lcm(int x,int y)
{
    for(int i=x;i<=x*y;i++)
    if(i%x==0 && i%y==0)
    return i;
}
int main() {
    int t;
    cin>>t;
    while(t--)
    {

        long long a=1,b=1,x;
        cin>>x;
        for(a=1;a<=x/2;a++)
        {
            for(b=1;b<=a;b++)
            {
                if((gcd(a,b)+lcm(a,b)==x) && (gcd(a,b)*lcm(a,b)==a*b))
                {
                    cout<<a<<" "<<b<<endl;
                    goto label;

                }
            }

        }
        label: a=1;

    }

    return 0;
}


#include <iostream>
#include <cstdio>
//#include<bits/stdhc++.h>
using namespace std;
int gcd(int x,int y)
{
    for(int i=x;i>=1;i--)
    if(x%i==0 && y%i==0)
    return i;
}
int lcm(int x,int y)
{
    for(int i=x;i<=x*y;i++)
    if(i%x==0 && i%y==0)
    return i;
}
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int flag=0;
        long long a=1,b=1,x;
        cin>>x;
        for(a=1;a<=x/2;a++)
        {
            for(b=1;b<=a;b++)
            {
                if(gcd(a,b)+lcm(a,b)==x)
                {break;flag=1;}
            }
            if(flag==1)
            {cout<<a<<" "<<b<<endl;
            break;
            }
        }
        cout<<a<<" "<<b<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
//#include<bits/stdhc++.h>
using namespace std;
int gcd(int x,int y)
{
    for(int i=x;i>=1;i--)
    if(x%i==0 && y%i==0)
    return i;
}
int lcm(int x,int y)
{
    for(int i=x;i<=x*y;i++)
    if(i%x==0 && i%y==0)
    return i;
}
int main() {
    int t;
    cin>>t;
    while(t--)
    {

        long long a=1,b=1,x;
        cin>>x;
        for(a=1;a<=x;a++,b=1)
        {
            do{if((gcd(a,b)+lcm(a,b)==x) && (gcd(a,b)*lcm(a,b)==a*b))
                {
                    cout<<a<<" "<<b<<endl;
                    goto label;

                }
                else b++;
            }while(b<=a);

        }
        label: a=1;

    }

    return 0;
}


#include <iostream
using namespace std;
int main()
{
    int t
    cin>>t
    for(int i=0;i<t;i++)
    {
        int n;
        cin<<n;
        cout<<"1"<<" "<<n-1<<endl;
    }
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;

    while(t>0)
    {
        int n;
        cin>>n;
        cout<<"1"<<" "<<n-1<<endl;
        t--;
    }


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;

    for(int i=0;i<t;i++)
    {
        int n;
        cin>>n;

        if(n%2==0)
        {
            for(int k=1;k<=(n/2);k++)
            {
                int gcd,lcd;
                int a,b;
                for(int m=n-k;m>=(n/2);m-- )
                {
                     a=k;
                     b=m;
                    int mini=min(a,b);

                    for(int y=1;y<=mini;y++)
                    {
                        if(a%y==0&&b%y==0)
                        {
                         gcd=y;
                        }
                    }
                    int maxi=max(a,b);
                    int i=maxi;

                    while(1)
                    {
                        if(i%a==0&&i%b==0)
                        {
                            lcd=i;
                            break;
                        }
                        i+=maxi;
                    }
                    break;

                }
                if((gcd+lcd)==n)
                {
                    cout<<a<<" "<<b<<endl;
                    break;
                }
            }
        }
    }
}


#include <iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;

    while(t>0)
    {
        int n;
        cin>>n;
        cout<<"1"<<" "<<n-1<<endl;
        t--;
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int a,b;
		if(n%2==0){
			a=n/2;
			b=n/2;
		}
		else{
			a=n-1;
			b=1;

		}
		cout<<a<<" "<<b;
		cout<<endl;
		a=0;
		b=0;

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int a,b;
		if(n%2==0){
			a=n/2;
			b=n/2;
		}
		else{
			a=n-1;
			b=n;

		}
		cout<<a<<" "<<b;
		cout<<endl;

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		int a,b;
		if(n%2==0){
			a=n/2;
			b=n/2;
		}
		else{
			a=n-1;
			b=n;

		}
		cout<<a<<" "<<b;
		cout<<endl;
		a=0;
		b=0;

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int gcd(int a,int b){
	if(b==0){
		return a;
	}
	return gcd(b,a%b);
}
int lcm(int a,int b){
	return (a/gcd(a,b))*b;
}

void solution(){
	int x;
	cin>>x;
	int a=x/2;
	int b=x-a;
	int g=0,l=0;
	do{
		g=gcd(a,b);
		l=lcm(a,b);
		a-=1;
		b+=1;
	}while((g+l)!=x);
	cout<<a+1<<" "<<b-1<<endl;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	for(int i=0;i<t;i++){
		solution();
	}
	return 0;
}


#include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <utility>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;

int T;
int n;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> T;
    while(T--) {
        cin >> n;
        int div = 2;
        while(1) {
            if(n%div==0) break;
            div++;
        }
        if(n==2) {
            cout << 1 << ' ' << 1 << '\n';
            continue;
        }
        for(int i=div; i<n; i+=div) {
            int a = i; int b = n-i;
            if(a<b) swap(a,b);
            if(a%b == 0) {
                cout << a << ' ' << b << '\n';
                break;
            }
        }
    }
    return 0;
};


#include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <utility>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;

int T;
int n;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> T;
    while(T--) {
        cin >> n;
        int div = 2;
        while(div++) {
            if(n%div==0) break;
        }

        if(n==2) {
            cout << 1 << ' ' << 1 << '\n';
            continue;
        }

        for(int i=div; i<n; i+=div) {
            int a = div; int b = n-div;
            if(a<b) swap(a,b);
            if(a%b == 0) {
                cout << a << ' ' << b << '\n';
                break;
            }
        }
    }
    return 0;
};


#include <iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    int T,n;
    cin >> T;
    while(T--)
    {
        cin >> n;

        cout << 1 << ' ' << n-1 << '\n';
    }
    return 0;
}


#include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <utility>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;

int T;
int n;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> T;
    while(T--) {
        cin >> n;
        int div = 2;
        while(1) {
            if(n%div==0) break;
            div++;
        }
        if(n==2) {
            cout << 1 << ' ' << 1 << '\n';
            continue;
        }
        for(int i=div; i<n; i+=div) {
            int a = div; int b = n-div;
            if(a<b) swap(a,b);
            if(a%b == 0) {
                cout << a << ' ' << b << '\n';
                break;
            }
        }
    }
    return 0;
};


#include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <utility>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;

int T;
int n;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> T;
    while(T--) {
        cin >> n;
        int div = 2;
        if(n%2==1) div++;

        if(n==2) {
            cout << 1 << ' ' << 1 << '\n';
            continue;
        }

        for(int i=div; i<n; i+=div) {
            int a = div; int b = n-div;
            if(a<b) swap(a,b);
            if(a%b == 0) {
                cout << a << ' ' << b << '\n';
                break;
            }
        }
    }
    return 0;
};


#include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <utility>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;

int T;
int n;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    cin >> T;
    while(T--) {
        cin >> n;
        int div = 2;
        if(n%2==1) div++;

        bool flag = false; int GCD = 1, LCM = 1;
        for(int i=div; i<n; i+=div) {
            int a = div; int b = n-div;
            if(a<b) swap(a,b);
            if(a%b == 0) {
                cout << a << ' ' << b << '\n';
                break;
            }
        }
    }
    return 0;
};


#include <iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    int T,n;

    while(T--)
    {
        cin >> n;

        cout << 1 << ' ' << n-1 << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int x;
        cin>>x;
        cout<<"1"<<" "<<x-1<<endl;

    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int x;
        cin>>x;
        cout<<"1"<<" "<<x<<"\n";
    }

}


#include <iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int x;
        cin>>x;
        cout<<"1"<<" "<<x<<"\n";
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int x;
        cin>>x;
        cout<<"1"<<" "<<x<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int x;
        cin>>x;
        cout<<"1"<<" "<<x-1<<endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define max1 10000
int main() {
    lli n,t;
    cin>>t;
    while(t--){
        cin>>n;
        cout<<1<<" "<<n-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef  long long int lli;
#define boost ios_base::sync_with_stdio(false); cin.tie(NULL);
#define inf (1000*1000*1000+5)

int main(){
  boost;
  int t;
  cin>>t;
  while(t--){
    cin>>n;
    cout<<1<<" "<<n-1<<endl;
  }
return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include<boost/multiprecision/cpp_int.hpp>
#include <algorithm>
using namespace std;
#define lli long long int
#define max1 10000
namespace mp=boost::multiprecision;

int main() {
    lli n,t;
    cin>>t;
    while(t--){
        cin>>n;
        cout<<1<<" "<<n-1<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef  long long int lli;
#define boost ios_base::sync_with_stdio(false); cin.tie(NULL);
#define inf (1000*1000*1000+5)

int main(){
  boost;
  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;
    cout<<1<<" "<<n-1<<endl;
  }
return 0;
}


#include<bits/stdc++.h>
using namespace std;
 int gcd(int ki, int ka)
{
    if(ka==0) return ki;
    else
    {
        gcd(ka,ki%ka);
    }
}
 int lcm( int a, int b)
{
    int k=gcd(a,b);
    int l=(a/k)*b;

}
int main()
{
   long long int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for( int i=1;i<=n;i++)
        {
            for( int j=1;j<=n;j++)
            {
                if(gcd(i,j)+lcm(i,j)==n)
                {
                    cout<<i<<" "<<j<<endl;
                    break;
                }
               /// break;
            }
            break;

        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int gcd(long long int ki,long long int ka)
{
    if(ka==0) return ki;
    else
    {
        gcd(ka,ki%ka);
    }
}
int lcm(int a,int b)
{
    int k=gcd(a,b);
    int l=(a/k)*b;

}
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(gcd(i,j)+lcm(i,j)==n)
                {
                    cout<<i<<" "<<j<<endl;
                    break;
                }
                break;
            }

        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
 int gcd(int ki, int ka)
{
    if(ka==0) return ki;
    else
    {
        gcd(ka,ki%ka);
    }
}
 int lcm( int a, int b)
{
  long long  int k=gcd(a,b);
   long long int l=(a/k)*b;

}
int main()
{
   long long int t,n,k;
    cin>>t;
    while(t--)
    {
        cin>>n;
        k=n/2;
        for( int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(gcd(i,j)+lcm(i,j)==n)
                {
                    cout<<i<<" "<<j<<endl;
                  /// break;

                }

            }
           break;

        }
       /// break;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        cout<<"1"<<" "<<n-1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long int gcd(long int ki, long int ka)
{
    if(ka==0) return ki;
    else
    {
        gcd(ka,ki%ka);
    }
}

int main()
{
   long long int t,n,k,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        k=n/2;
        for( int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {

                x=gcd(i,j);///gcd of i and j;
                y=(i/x)*j;///lcm of i and j;

                if((x+y)==n)
                {
                    cout<<i<<" "<<j<<"\n";/// if i r j er gcd and lcm equal input value hy
                    ////break;
            }
                ///break;

        }
         break;

        }

    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        if(n%2==0)
        {
            cout<<(n/2)<<" "<<(n/2)<<endl;
        }
        else
        {
            cout<<"1"<<" "<<(n-1)<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x;
        cin>>x;
        int gcd=2;
        while(x%gcd!=0)
            gcd++;
        int a=gcd;
        int b=(x-gcd)/gcd;
        b=b/gcd;
        cout<<a<<" "<<b;
    }
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x;
        cin>>x;
        int gcd=2;
        while(x%gcd!=0)
            gcd++;
        x-=gcd;
        cout<<gcd<<" "<<gcd*x<<endl;
    }
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int mcd(int x)
{
    for(int i=2;i<=sqrt(x+1);i++)
    {
        if(x%i==0)
            return i;
    }
    return -1;
}
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x;
        cin>>x;
        int ch=mcd(x);
        if(ch==-1||ch==x)
            cout<<1<<" "<<x-1<<endl;
        else
            cout<<ch<<" "<<x-ch<<endl;
    }
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x;
        cin>>x;
        int gcd=2;
        while(x%gcd!=0)
            gcd++;
        if(x==2)
            cout<<1<<" "<<1<<endl;
        else
            cout<<gcd<<" "<<x-gcd<<endl;
    }
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x;
        cin>>x;
        int gcd=2;
        while(x%gcd!=0)
            gcd++;
        if(gcd==x)
            cout<<1<<" "<<x-1<<endl;
        else
            cout<<gcd<<" "<<x-gcd<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

int main(){

long long n;
cin>>n;

cout<<n-1<<' '<<1;
return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
 long long n;
 for(int i=0;i<n;i++){
  cin>>n;
  cout<<n-1<<' '<<1;
 {
 return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
 long long n, x;
 for(int i=0;i<n;i++){
  cin>>x;
  cout<<x-1<<' '<<1<<'\n';
 }
 return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
 long long n;
 for(int i=0;i<n;i++){
  cin>>n;
  cout<<n-1<<' '<<1;
 }
 return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
 long long n;
 for(int i=0;i<n;i++){
  cin>>n;
  cout<<n-1<<' '<<1<<'\n';
 }
 return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
 int t;
 cin >> t;
 long long x;
 while(t--){
  cin >> x;
  cout << x-1 << ' ' << 1 << '\n';
 }
 return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
 long long n;
 while(n--){
  cin>>n;
  cout<<n-1<<' '<<1<<'\n';
 }
 return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){
 int n;
 long long x;
 while(n--){
  cin>>x;
  cout<<x-1<<' '<<1<<'\n';
 }
 return 0;
}


#include<bits/stdc++.h>
using namespace std;
{
          int t;
          cin>>t;
          while(t--)
          {
                    int a,b;
                    a=1;
                    b=n-1;
                    cout<<a<<" "<<b<<"\n";
          }
          cout<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
          int t;
          cin>>t;
          while(t--)
          {
                    int a,b,n;
                    cin>>n;
                    a=1;
                    b=n-1;
                    cout<<a<<" "<<b<<"\n";
          }
          return 0;

}


#include<bits/stdc++.h>
using namespace std;
{
          int t;
          cin>>t;
          while(t--)
          {
                    int a,b;
                    a=1;
                    b=n-1;
                    cout<<a<<" "<<b<<"\n";
          }
          return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
          int t;
          cin>>t;
          while(t--)
          {
                    int a,b,n;
                    cin>>n
                    a=1;
                    b=n-1;
                    cout<<a<<" "<<b<<"\n";
          }
          return 0;

}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int lcm(int a,int b,int g)
{
int l;
l=(a*b)/g;
return g;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x,l,g,sum;
	//ll sum;
        cin>>x;
    if(x%2==0)
      {
	for(int i=0,j=x;i!=j;i++,j--)
	{
	g=__gcd(i,j);
	l=lcm(i,j,g);
	sum=l+g;
	if(sum==x)
	{
	cout<<i<<" "<<j<<"\n";
	break;
	}
	}
     }

else
   {for(int i=0,j=x;i>j;i++,j--)
	{
	g=__gcd(i,j);
	l=lcm(i,j,g);
	sum=l+g;
	if(sum==x)
	{
	cout<<i<<" "<<j<<"\n";
	break;
	}
	}
     }
break;
}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
          int t;
          cin>>t;
          while(t--)
          {
                    int a,b;
                    a=1;
                    b=n-1;
                    cout<<a<<" "<<b<<"\n";
          }
          return 0;

}


#include <iostream>
using namespace std;
int main(void){int t;cin>>t;while(t--){int x;cin>>x;cout<<1<<" "<<x-1<<'\n';return 0;}}


#include <iostream>
using namespace std;
int main(void){int x;cin>>x;cout<<1<<" "<<x-1<<'\n';return 0;}


#include <iostream>
using namespace std;
int main(void){int x;xin>>x;cout<<1<<" "<<x-1<<'\n';return 0;}


#include <iostream>
int main(void){int x;xin>>x;cout<<1<<" "<<x-1<<'\n';return 0;}


#include <iostream>
using namespace std;
int main(void){int t;
    cin>>t;
    while(t--){
        int x;cin>>x;cout<<1<<" "<<x-1<<'\n';
    }
    return 0;
}