#include <bits/stdtr1c++.h>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef complex<ld> pt;

int main() {
	ios::sync_with_stdio(0);
	int t; cin >> t;
	while (t--) {
		int ca; cin >> ca;
		cout << ca << " ";
		int b; string s; cin >> b >> s;
		int sum = 0;
		for (auto c : s) {
			int d = c-'0';
			sum += d;
		}
		cout << (sum%(b-1)) << endl;
	}
	return 0;
}