#include<bits/stdc++.h>
using namespace std;
/*==================!!-Code-Starts-From-Here-!!==================*/
const int N = 512,B=300;
int dp[33][65][B][N]={}; // dp[k][m][b] -> Preprocessed A^(K)'s , m'th group , b'th index
int AK[33][N][N]={}; // AK[k] -> K-th power of A
int A[N][N]; // A -> original matrix
int ans[N]; // Single row to calculate answer
int temp[N];
int n,m,t; // n -> matrix dimension, m -> number of groups, t -> number of row's/coloumn's per group
vector<int> bits; // -> selection of subsets [1 at a time, 2 at a time ,... t at a time]
bool cmp(int a,int b) { // sort by number of set bits accending
if(__builtin_popcount(a) == __builtin_popcount(b))
return a < b;
return __builtin_popcount(a) < __builtin_popcount(b);
}
void preProcessAK(int k) { // Preprocess A^(K) K={0,1,2,4,8,....}
for(int g=0;g<m;g++) {
for(int b=0;b<bits.size();b++) {
int rr;
for(rr=0;(bits[b]&(1<<rr))==0;rr++); // find least significant bit
for(int cc=0;cc<n;cc++) {
dp[k][g][bits[b]][cc] = dp[k][g][bits[b]][cc] || dp[k][g][bits[b]^(1<<rr)][cc] || AK[k][(g*t)+rr][cc];
// dp[bit-index][col] = dp[bit-index ^ lsb][col] || A[lsb + offset of group][col]
}
}
}
int left=n%t; // group with less than t-rows
for(int b=0;b<bits.size();b++) {
int msb = 32 - __builtin_clz(bits[b]);
if(msb > left) continue;
int rr;
for(rr=0;(bits[b]&(1<<rr))==0;rr++); // find least significant bit
for(int cc=0;cc<n;cc++) {
dp[k][m][bits[b]][cc] = dp[k][m][bits[b]][cc] || dp[k][m][bits[b]^(1<<rr)][cc] || AK[k][(m*t)+rr][cc];
}
}
return;
}
void Init(int p) { // Initialization to calculate all A^(k)'s and preprocess them
t = log2(n); // division of 'n' into 'm' groups of size 't'
m = n/t;
for(int i=1;i<(1<<t);i++) { // find all 2^(t)-1 combinations and sort by N(set-bits)
bits.push_back(i);
}
sort(bits.begin(),bits.end(),cmp);
for(int i=0;i<n;i++) { // Preprocess Initial Matrix A^(0) = A
for(int j=0;j<n;j++) {
AK[0][i][j] = A[i][j];
}
}
preProcessAK(0);
for(int k=1;k<p;k++) { // Power - k
for(int rr=0;rr<n;rr++) { // Row - rr
for(int g=0;g<m;g++) { // Group - g
int b=0;
for(int cc=0;cc<t;cc++) { // bit-index - b
b = b | (AK[k-1][rr][(g*t)+cc]<<cc);
}
for(int cc=0;cc<n;cc++) {
AK[k][rr][cc] = AK[k][rr][cc] || dp[k-1][g][b][cc];
// A^(k) [row][col] = A^(k) [row][col] or DP(A^(k-1))[group][bit-index][col];
}
}
int left = n%t; // group with less than 't' bits
int b=0;
for(int cc=0;cc<left;cc++) {
b = b | (AK[k-1][rr][(m*t)+cc]<<cc);
}
for(int cc=0;cc<n;cc++) {
AK[k][rr][cc] = AK[k][rr][cc] || dp[k-1][m][b][cc];
}
}
preProcessAK(k);
}
}
void solve(int rr,int p) { // row-rr , power-p of A needed
memset(ans,0,sizeof(ans));
int k=0; // power-k of A used now {0,1,2,4,8,....}
bool ok=false;
while(p) {
if((p&1)) { // current power-k is needed
if(!ok) { // first - needed ans[col] = A^(K)[row][col]
for(int cc=0;cc<n;cc++) {
ans[cc]=AK[k][rr][cc];
}
k++;
p=p>>1;
ok=true;
continue;
}
for(int cc=0;cc<n;cc++) { // from second need of A^(k)
temp[cc]=ans[cc];
ans[cc]=0;
}
for(int g=0;g<m;g++) { // group - g
int b=0;
for(int cc=0;cc<t;cc++) { // bit-index - b
b = b | (temp[(g*t)+cc]<<cc);
}
for(int cc=0;cc<n;cc++) {
ans[cc] = ans[cc] || dp[k][g][b][cc];
// ans[col] = ans[col] or DP(A^(K))[group][bit-index][col]
}
}
int left = n%t; // group with less than t - rows/cols
int b=0;
for(int cc=0;cc<left;cc++) {
b = b | (temp[(m*t)+cc]<<cc);
}
for(int cc=0;cc<n;cc++) {
ans[cc] = ans[cc] || dp[k][m][b][cc];
}
}
p=p>>1;
k++;
}
}
int main() {
scanf("%d",&n); // get - dimension of square matrix
for(int rr=0;rr<n;rr++) { // and also matrix
for(int cc=0;cc<n;cc++) {
scanf("%d",&A[rr][cc]);
}
}
if(n==1) { // special case dimension - 1x1
int test,k,rr;
scanf("%d",&test);
while(test--) {
scanf("%d",&k);scanf("%d",&rr);
if(k==0 || (A[0][0]==1)) {
puts("1\n1");
} else {
puts("0\n-1");
}
}
return 0;
}
Init(32); // Precompute - all DP[A^(K)] k:{0,1,2,...31}
int test;
scanf("%d",&test);
while(test--) {
int k,rr;
scanf("%d",&k);scanf("%d",&rr);
rr--;
if(k==0) { // Base-case
printf("1\n%d\n",rr+1);
continue;
}
solve(rr,k); // solve for row -rr of A^(k)
vector<int> val;
for(int cc=0;cc<n;cc++) {
if(ans[cc]==1) val.push_back(cc+1);
}
printf("%d\n",(int)val.size());
if(val.size()==0) printf("-1");
for(int cc=0;cc<val.size();cc++) {
printf("%d ",val[cc]);
}
printf("\n");
}
return 0;
}
/*=======================!!-End-Of-Code-!!=======================*/