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

main(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    int n,m,t;
    cin>>n>>m>>t;
    vector<vector<int>>v(n+2,vector<int>(m+2,1e18+1)),vv(n+2,vector<int>(m+2,-1));
    vector<vector<int>> vis(n+1,vector<int> (m+1,0));

    for(int i=1;i<n+1;i++){
      string s;cin>>s;
      for(int j=1;j<m+1;j++){
            vv[i][j]=s[j-1]-'0';
      }
    }
    int c=0;int pk=0;
    int ik=-1,jk=-1;
     for(int i=1;i<n+1;i++){
      for(int j=1;j<m+1;j++){
        if(vv[i][j]!=-1){
        if(vv[i][j]==vv[i-1][j]){ if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i-1][j]=0;}
        if(vv[i][j]==vv[i][j-1]){if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i][j-1]=0;}
        if(vv[i][j]==vv[i+1][j]){if(pk==0){ik=i;jk=j;v[i][j]=0;pk++;}v[i+1][j]=0;}
        if(vv[i][j]==vv[i][j+1]){if(pk==0){ik=i;jk=j;v[i][j]=0;pk++;}v[i][j+1]=0;}
        }
      }
     }
     //  for(int i=1;i<=n;i++){
     // for(int j=1;j<=m;j++)
     //  cout<<v[i][j]<<" ";
     // cout<<endl;
     //}

     if(ik==-1){
      while(t--){
        int i,j,p;
        cin>>i>>j>>p;
        cout<<vv[i][j]<<endl;
      }
      return 0;
     }
     queue<pair<int,int>>q;
     q.push({ik,jk});
        while(!q.empty()){
        pair<int, int> p = q.front();
        q.pop();
        for(int k=0;k<4;k++){
            int xx = p.first + dir[k][0];
            int yy = p.second + dir[k][1];
            if(xx>0&&xx<=n&&yy>0&&yy<=m&&vis[xx][yy]==0){
                vis[xx][yy] = 1;
                v[xx][yy] = min(1 + v[p.first][p.second],v[xx][yy]);
                q.push({xx, yy});
            }
        }

    }
     //cout<<ik<<" "<<jk<<endl;
     //cout<<endl<<endl;
     //  for(int i=1;i<=n;i++){
      //for(int j=1;j<=m;j++)
      //  cout<<v[i][j]<<" ";
      //cout<<endl;
     //}
     while(t--){
      int i,j,p;
      cin>>i>>j>>p;
      int kc=v[i][j];
      if(kc>=p){cout<<vv[i][j]<<endl;}
      else{
        kc-=p;
        if(kc%2==0)cout<<vv[i][j]<<endl;
        else cout<<1-vv[i][j]<<endl;
      }
     }
}


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

main(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    int n,m,t;
    cin>>n>>m>>t;
    vector<vector<int>>v(n+2,vector<int>(m+2,1e18+1)),vv(n+2,vector<int>(m+2,-1));
    vector<vector<int>> vis(n+1,vector<int> (m+1,0));

    for(int i=1;i<n+1;i++){
      string s;cin>>s;
      for(int j=1;j<m+1;j++){
            vv[i][j]=s[j-1]-'0';
      }
    }
    int c=0;int pk=0;
    int ik=-1,jk=-1;
     for(int i=1;i<n+1;i++){
      for(int j=1;j<m+1;j++){
        if(vv[i][j]!=-1){
        if(vv[i][j]==vv[i-1][j]){ if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i-1][j]=0;}
        if(vv[i][j]==vv[i][j-1]){if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i][j-1]=0;}
        if(vv[i][j]==vv[i+1][j]){if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i+1][j]=0;}
        if(vv[i][j]==vv[i][j+1]){if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;pk++;}v[i][j+1]=0;}
        }
      }
     }
     //  for(int i=1;i<=n;i++){
     // for(int j=1;j<=m;j++)
     //  cout<<v[i][j]<<" ";
     // cout<<endl;
     //}

     if(ik==-1){
      while(t--){
        int i,j,p;
        cin>>i>>j>>p;
        cout<<vv[i][j]<<endl;
      }
      return 0;
     }
     queue<pair<int,int>>q;
     q.push({ik,jk});
        while(!q.empty()){
        pair<int, int> p = q.front();
        q.pop();
        for(int k=0;k<4;k++){
            int xx = p.first + dir[k][0];
            int yy = p.second + dir[k][1];
            if(xx>0&&xx<=n&&yy>0&&yy<=m&&vis[xx][yy]==0){
                vis[xx][yy] = 1;
                v[xx][yy] = min(1 + v[p.first][p.second],v[xx][yy]);
                q.push({xx, yy});
            }
        }

    }
     //cout<<ik<<" "<<jk<<endl;
     //cout<<endl<<endl;
     //  for(int i=1;i<=n;i++){
      //for(int j=1;j<=m;j++)
      //  cout<<v[i][j]<<" ";
      //cout<<endl;
     //}
     while(t--){
      int i,j,p;
      cin>>i>>j>>p;
      int kc=v[i][j];
      if(kc>=p){cout<<vv[i][j]<<endl;}
      else{
        kc-=p;
        if(kc%2==0)cout<<vv[i][j]<<endl;
        else cout<<1-vv[i][j]<<endl;
      }
     }
}


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

main(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    int n,m,t;
    cin>>n>>m>>t;
    vector<vector<int>>v(n+2,vector<int>(m+2,10)),vv(n+2,vector<int>(m+2,-1));
    vector<vector<int>> vis(n+1,vector<int> (m+1,0));

    for(int i=1;i<n+1;i++){
      string s;cin>>s;
      for(int j=1;j<m+1;j++){
            vv[i][j]=s[j-1]-'0';
      }
    }
    int c=0;int pk=0;
    int ik=-1,jk=-1;
     for(int i=1;i<n+1;i++){
      for(int j=1;j<m+1;j++){
        if(vv[i][j]!=-1){
        if(vv[i][j]==vv[i-1][j]){ if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i-1][j]=0;}
        if(vv[i][j]==vv[i][j-1]){if(pk==0){ik=i;jk=j;pk++;}v[i][j]=0;v[i][j-1]=0;}
        if(vv[i][j]==vv[i+1][j]){if(pk==0){ik=i;jk=j;v[i][j]=0;pk++;}v[i+1][j]=0;}
        if(vv[i][j]==vv[i][j+1]){if(pk==0){ik=i;jk=j;v[i][j]=0;pk++;}v[i][j+1]=0;}
        }
      }
     }
    //   for(int i=1;i<=n;i++){
    //  for(int j=1;j<=m;j++)
    //    cout<<v[i][j]<<" ";
     // cout<<endl;
     //}

     if(ik==-1){
      while(t--){
        int i,j,p;
        cin>>i>>j>>p;
        cout<<vv[i][j]<<endl;
      }
      return 0;
     }
     queue<pair<int,int>>q;
     q.push({ik,jk});
        while(!q.empty()){
        pair<int, int> p = q.front();
        q.pop();
        for(int k=0;k<4;k++){
            int xx = p.first + dir[k][0];
            int yy = p.second + dir[k][1];
            if(xx>0&&xx<=n&&yy>0&&yy<=m&&vis[xx][yy]==0){
                vis[xx][yy] = 1;
                v[xx][yy] = 1 + v[p.first][p.second];
                q.push({xx, yy});
            }
        }

    }
    // cout<<ik<<" "<<jk<<endl;
    // cout<<endl<<endl;
    //   for(int i=1;i<=n;i++){
    //  for(int j=1;j<=m;j++)
    //    cout<<v[i][j]<<" ";
    //  cout<<endl;
    // }
     while(t--){
      int i,j,p;
      cin>>i>>j>>p;
      int kc=v[i][j];
      if(kc>=p){cout<<vv[i][j]<<endl;}
      else{
        kc-=p;
        if(kc%2==0)cout<<vv[i][j]<<endl;
        else cout<<1-vv[i][j]<<endl;
      }
     }
}


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

main(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int n,m,t;
    cin>>n>>m>>t;
    vector<vector<int>>v(n+2,vector<int>(m+2,1e18+1)),vv(n+2,vector<int>(m+2,-1));
    map<pai<int,int>>vis;
    for(int i=1;i<n+1;i++){
      string s;cin>>s;
      for(int j=1;j<m+1;j++){
            vv[i][j]=s[j-1]-'0';
      }
    }
    int c=0;
    int ik=-1,jk=-1;
     for(int i=1;i<n+1;i++){
      for(int j=1;j<m+1;j++){
        if(vv[i][j]!=-1){
        if(vv[i][j]==vv[i-1][j]){ik=i;jk=j;v[i][j]=0;v[i-1][j]=0;}
        if(vv[i][j]==vv[i][j-1]){ik=i;jk=j;v[i][j]=0;v[i][j-1]=0;}
        if(vv[i][j]==vv[i+1][j]){ik=i;jk=j;v[i][j]=0;v[i+1][j]=0;}
        if(vv[i][j]==vv[i][j+1]){ik=i;jk=j;v[i][j]=0;v[i][j+1]=0;}
        }
      }
     }
     if(ik==-1){
      while(t--){
        int i,j,p;
        cin>>i>>j>>p;
        cout<<vv[i][j]<<endl;
      }
      return 0;
     }
     queue<pair<int,int>>q;
     q.push({ik,ij});
     while(!q.empty()){
      pair<int,int> top=q.front();
      q.pop();
        int i=top.first;
        int j=top.second;
        vector<pair<int,int>>temp;
        temp.push_back({i,j-1});
        temp.push_back({i,j+1});
       temp.push_back({i+1,j});
       temp.push_back({i-1,j});
        for(auto cc:temp){
          if(vis[{cc.first,cc.second}]>0){
            v[cc.first][cc.second]=min( v[cc.first][cc.second],1+v[i][j]);
          }
          else{
            vis[{cc.first,cc.second}]++;
             v[cc.first][cc.second]= v[cc.first][cc.second],1+v[i][j];
             if(cc.first<=n && cc.first>=1 && cc.second>=1 && cc.second<=m)q.push({cc.first,cc.second});
          }
        }
     }
     while(t--){
      int i,j,p;
      cin>>i>>j>>p;
      int kc=v[i][j];
      if(kc>=p){cout<<vv[i][j]<<endl;}
      else{
        kc-=p;
        if(kc%2==0)cout<<vv[i][j]<<endl;
        else cout<<1-vv[i][j]<<endl;
      }
     }
}


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

main(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int n,m,t;
    cin>>n>>m>>t;
    vector<vector<int>>v(n+2,vector<int>(m+2,1e18+1)),vv(n+2,vector<int>(m+2,-1));
    map<pai<int,int>,int>vis;
    for(int i=1;i<n+1;i++){
      string s;cin>>s;
      for(int j=1;j<m+1;j++){
            vv[i][j]=s[j-1]-'0';
      }
    }
    int c=0;
    int ik=-1,jk=-1;
     for(int i=1;i<n+1;i++){
      for(int j=1;j<m+1;j++){
        if(vv[i][j]!=-1){
        if(vv[i][j]==vv[i-1][j]){ik=i;jk=j;v[i][j]=0;v[i-1][j]=0;}
        if(vv[i][j]==vv[i][j-1]){ik=i;jk=j;v[i][j]=0;v[i][j-1]=0;}
        if(vv[i][j]==vv[i+1][j]){ik=i;jk=j;v[i][j]=0;v[i+1][j]=0;}
        if(vv[i][j]==vv[i][j+1]){ik=i;jk=j;v[i][j]=0;v[i][j+1]=0;}
        }
      }
     }
     if(ik==-1){
      while(t--){
        int i,j,p;
        cin>>i>>j>>p;
        cout<<vv[i][j]<<endl;
      }
      return 0;
     }
     queue<pair<int,int>>q;
     q.push({ik,jk});
     while(!q.empty()){
      pair<int,int> top=q.front();
      q.pop();
        int i=top.first;
        int j=top.second;
        vector<pair<int,int>>temp;
        temp.push_back({i,j-1});
        temp.push_back({i,j+1});
       temp.push_back({i+1,j});
       temp.push_back({i-1,j});
        for(auto cc:temp){
          if(vis[{cc.first,cc.second}]>0){
            v[cc.first][cc.second]=min( v[cc.first][cc.second],1+v[i][j]);
          }
          else{
            vis[{cc.first,cc.second}]++;
             v[cc.first][cc.second]= v[cc.first][cc.second],1+v[i][j];
             if(cc.first<=n && cc.first>=1 && cc.second>=1 && cc.second<=m)q.push({cc.first,cc.second});
          }
        }
     }
     while(t--){
      int i,j,p;
      cin>>i>>j>>p;
      int kc=v[i][j];
      if(kc>=p){cout<<vv[i][j]<<endl;}
      else{
        kc-=p;
        if(kc%2==0)cout<<vv[i][j]<<endl;
        else cout<<1-vv[i][j]<<endl;
      }
     }
}


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

main(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    int n,m,t;
    cin>>n>>m>>t;
    vector<vector<int>>v(n+2,vector<int>(m+2,1e18+1)),vv(n+2,vector<int>(m+2,-1));
    vector<vector<int>> vis(n+1,vector<int> (m+1,0));
queue<pair<int,int>>q;
    for(int i=1;i<n+1;i++){
      string s;cin>>s;
      for(int j=1;j<m+1;j++){
            vv[i][j]=s[j-1]-'0';
      }
    }
    int c=0;int pk=0;
    int ik=-1,jk=-1;
     for(int i=1;i<n+1;i++){
      for(int j=1;j<m+1;j++){
        if(vv[i][j]!=-1){
        if(vv[i][j]==vv[i-1][j]){ ik=i;jk=j;pk++;
 q.push({ik,jk});vis[ik][jk]=1;  v[i][j]=0;v[i-1][j]=0;}
        if(vv[i][j]==vv[i][j-1]){ik=i;jk=j;pk++;
 q.push({ik,jk});vis[ik][jk]=1; v[i][j]=0;v[i][j-1]=0;}
        if(vv[i][j]==vv[i+1][j]){ik=i;jk=j;pk++;
 q.push({ik,jk});vis[ik][jk]=1; v[i][j]=0;v[i+1][j]=0;}
        if(vv[i][j]==vv[i][j+1]){ik=i;jk=j;pk++;
 q.push({ik,jk});vis[ik][jk]=1; v[i][j]=0;v[i][j+1]=0;}
        }
      }
     }
     //  for(int i=1;i<=n;i++){
     // for(int j=1;j<=m;j++)
     //  cout<<v[i][j]<<" ";
     // cout<<endl;
     //}

     if(ik==-1){
      while(t--){
        int i,j,p;
        cin>>i>>j>>p;
        cout<<vv[i][j]<<endl;
      }
      return 0;
     }

    // q.push({ik,jk});vis[ik][jk]=1
        while(!q.empty()){
        pair<int, int> p = q.front();
        q.pop();
        for(int k=0;k<4;k++){
            int xx = p.first + dir[k][0];
            int yy = p.second + dir[k][1];
            if(xx>0&&xx<=n&&yy>0&&yy<=m&&vis[xx][yy]==0){
                vis[xx][yy] = 1;
                v[xx][yy] = min(1 + v[p.first][p.second],v[xx][yy]);
                q.push({xx, yy});
            }
        }

    }
     //cout<<ik<<" "<<jk<<endl;
     //cout<<endl<<endl;
     //  for(int i=1;i<=n;i++){
      //for(int j=1;j<=m;j++)
      //  cout<<v[i][j]<<" ";
      //cout<<endl;
     //}
     while(t--){
      int i,j,p;
      cin>>i>>j>>p;
      int kc=v[i][j];
      if(kc>=p){cout<<vv[i][j]<<endl;}
      else{
        kc-=p;
        if(kc%2==0)cout<<vv[i][j]<<endl;
        else cout<<1-vv[i][j]<<endl;
      }
     }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 2e5+5;
ll expo(ll a, ll b){
    if(b == 0)
		return 1;
    ll x = expo(a, b/2);
    return (b & 1 ? ((x*x)*a) : (x*x) );
}

int x[] = {1, -1, 0, 0}, l, r, q;
int y[] = {0, 0, 1, -1};

bool used[1005][1005];
char m[1005][1005];
int dist[1005][1005];

vector<pii> check(int a, int b) {
	vector<pii> bfs;
	queue<pii> ths;
	ths.push({a, b});

	while(!ths.empty()) {
		pii kappa = ths.front();
		ths.pop();
		a = kappa.f, b = kappa.s;

		bool pxd = 0;

		for(int i = 0; i < 4; i++) {

			int na = a + x[i], nb = b + y[i];

			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;

			if(!used[na][nb]) {
				ths.push({na, nb});
				used[na][nb] = 1;
			}

			if(m[na][nb] == m[a][b]  && !pxd) {
				bfs.pb({a, b});
				pxd = 1;
			}

		}

	}
	return bfs;
}

void solve() {

	vector<pii> vet = check(0, 0);

	for(int i = 0; i < 1005; i++)
		for(int j = 0; j < 1005; j++)
			dist[i][j] = INF;

	queue<pii> fila;
	for(int i = 0; i < vet.size(); i++) {
		dist[vet[i].f][vet[i].s] = 0;
		fila.push({vet[i].f, vet[i].s});
	}

	while(!fila.empty()) {

		pii kappa = fila.front();
		int a = kappa.f, b = kappa.s;
		fila.pop();

		for(int i = 0; i < 4; i++) {
			int na = a + x[i], nb = b + y[i];
			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;
			if(dist[a][b] + 1 < dist[na][nb]) {
				dist[na][nb] = dist[a][b] + 1;
				fila.push({na, nb});
			}
		}

	}

}

int32_t main() {
	cin >> l >> r >> q;
	for(int i = 0; i < l; i++)
		for(int j = 0; j < r; j++)
			cin >> m[i][j];

	solve();
	/*for(int i = 0; i < l; cout << endl, i++)
		for(int j = 0; j < r; j++)
			cout << dist[i][j] << " ";
	*/
	while(q--) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		if(dist[a][b] > c)
			cout << m[a][b] << endl;
		else {

			if( (c-dist[a][b]) % 2 == 0) cout << m[a][b] << endl;
			else {
				if(m[a][b] == '1') cout << 0 << endl;
				else cout << 1 << endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define int long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 2e5+5;
ll expo(ll a, ll b){
    if(b == 0)
		return 1;
    ll x = expo(a, b/2);
    return (b & 1 ? ((x*x)*a) : (x*x) );
}

int x[] = {1, -1, 0, 0}, l, r, q;
int y[] = {0, 0, 1, -1};

bool used[1005][1005];
char m[1005][1005];
int dist[1005][1005];

vector<pii> check(int a, int b) {
	vector<pii> bfs;
	queue<pii> ths;
	ths.push({a, b});

	while(!ths.empty()) {
		pii kappa = ths.front();
		ths.pop();
		a = kappa.f, b = kappa.s;

		bool pxd = 0;

		for(int i = 0; i < 4; i++) {

			int na = a + x[i], nb = b + y[i];

			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;

			if(!used[na][nb]) {
				ths.push({na, nb});
				used[na][nb] = 1;
			}

			if(m[na][nb] == m[a][b]  && !pxd) {
				bfs.pb({a, b});
				pxd = 1;
			}

		}

	}
	return bfs;
}

void solve() {

	vector<pii> vet = check(0, 0);

	for(int i = 0; i < 1005; i++)
		for(int j = 0; j < 1005; j++)
			dist[i][j] = LONG_LONG_MAX;

	queue<pii> fila;
	for(int i = 0; i < vet.size(); i++) {
		dist[vet[i].f][vet[i].s] = 0;
		fila.push({vet[i].f, vet[i].s});
	}

	while(!fila.empty()) {

		pii kappa = fila.front();
		int a = kappa.f, b = kappa.s;
		fila.pop();

		for(int i = 0; i < 4; i++) {
			int na = a + x[i], nb = b + y[i];
			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;
			if(dist[a][b] + 1 < dist[na][nb]) {
				dist[na][nb] = dist[a][b] + 1;
				fila.push({na, nb});
			}
		}

	}

}

int32_t main() {
	cin >> l >> r >> q;
	for(int i = 0; i < l; i++)
		for(int j = 0; j < r; j++)
			cin >> m[i][j];

	solve();
	//for(int i = 0; i < l; cout << endl, i++)
	//	for(int j = 0; j < r; j++)
	//		if(dist[i][j] != LONG_MAX) cout << "NONNON" << " ";

	while(q--) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		if(dist[a][b] > c)
			cout << m[a][b] << endl;
		else {

			if( (c-dist[a][b]) % 2 == 0) cout << m[a][b] << endl;
			else {
				if(m[a][b] == '1') cout << 0 << endl;
				else cout << 1 << endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define int long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 2e5+5;
ll expo(ll a, ll b){
    if(b == 0)
		return 1;
    ll x = expo(a, b/2);
    return (b & 1 ? ((x*x)*a) : (x*x) );
}

int x[] = {1, -1, 0, 0}, l, r, q;
int y[] = {0, 0, 1, -1};

bool used[1005][1005];
char m[1005][1005];
int dist[1005][1005];

vector<pii> check(int a, int b) {
	vector<pii> bfs;
	queue<pii> ths;
	ths.push({a, b});

	while(!ths.empty()) {
		pii kappa = ths.front();
		ths.pop();
		a = kappa.f, b = kappa.s;

		bool pxd = 0;

		for(int i = 0; i < 4; i++) {

			int na = a + x[i], nb = b + y[i];

			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;

			if(!used[na][nb]) {
				ths.push({na, nb});
				used[na][nb] = 1;
			}

			if(m[na][nb] == m[a][b]  && !pxd) {
				bfs.pb({a, b});
				pxd = 1;
			}

		}

	}
	return bfs;
}

void solve() {

	vector<pii> vet = check(0, 0);

	for(int i = 0; i < 1005; i++)
		for(int j = 0; j < 1005; j++)
			dist[i][j] = INF;

	queue<pii> fila;
	for(int i = 0; i < vet.size(); i++) {
		dist[vet[i].f][vet[i].s] = 0;
		fila.push({vet[i].f, vet[i].s});
	}

	while(!fila.empty()) {

		pii kappa = fila.front();
		int a = kappa.f, b = kappa.s;
		fila.pop();

		for(int i = 0; i < 4; i++) {
			int na = a + x[i], nb = b + y[i];
			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;
			if(dist[a][b] + 1 < dist[na][nb]) {
				dist[na][nb] = dist[a][b] + 1;
				fila.push({na, nb});
			}
		}

	}

}

int32_t main() {
	cin >> l >> r >> q;
	for(int i = 0; i < l; i++)
		for(int j = 0; j < r; j++)
			cin >> m[i][j];

	solve();
	/*for(int i = 0; i < l; cout << endl, i++)
		for(int j = 0; j < r; j++)
			cout << dist[i][j] << " ";
	*/
	while(q--) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		if(dist[a][b] > c)
			cout << m[a][b] << endl;
		else {

			if( (c-dist[a][b]) % 2 == 0) cout << m[a][b] << endl;
			else {
				if(m[a][b] == '1') cout << 0 << endl;
				else cout << 1 << endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define int long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 2e5+5;
ll expo(ll a, ll b){
    if(b == 0)
		return 1;
    ll x = expo(a, b/2);
    return (b & 1 ? ((x*x)*a) : (x*x) );
}

int x[] = {1, -1, 0, 0}, l, r, q;
int y[] = {0, 0, 1, -1};

bool used[1005][1005];
char m[1005][1005];
int dist[1005][1005];

vector<pii> check(int a, int b) {
	vector<pii> bfs;
	queue<pii> ths;
	ths.push({a, b});

	while(!ths.empty()) {
		pii kappa = ths.front();
		ths.pop();
		a = kappa.f, b = kappa.s;

		bool pxd = 0;

		for(int i = 0; i < 4; i++) {

			int na = a + x[i], nb = b + y[i];

			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;

			if(!used[na][nb]) {
				ths.push({na, nb});
				used[na][nb] = 1;
			}

			if(m[na][nb] == m[a][b]  && !pxd) {
				bfs.pb({a, b});
				pxd = 1;
			}

		}

	}
	return bfs;
}

void solve() {

	vector<pii> vet = check(0, 0);

	for(int i = 0; i < 1005; i++)
		for(int j = 0; j < 1005; j++)
			dist[i][j] = LONG_MAX;

	queue<pii> fila;
	for(int i = 0; i < vet.size(); i++) {
		dist[vet[i].f][vet[i].s] = 0;
		fila.push({vet[i].f, vet[i].s});
	}

	while(!fila.empty()) {

		pii kappa = fila.front();
		int a = kappa.f, b = kappa.s;
		fila.pop();

		for(int i = 0; i < 4; i++) {
			int na = a + x[i], nb = b + y[i];
			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;
			if(dist[a][b] + 1 < dist[na][nb]) {
				dist[na][nb] = dist[a][b] + 1;
				fila.push({na, nb});
			}
		}

	}

}

int32_t main() {
	cin >> l >> r >> q;
	for(int i = 0; i < l; i++)
		for(int j = 0; j < r; j++)
			cin >> m[i][j];

	solve();
	for(int i = 0; i < l; cout << endl, i++)
		for(int j = 0; j < r; j++)
			cout << "NONNON" << " ";

	while(q--) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		if(dist[a][b] > c)
			cout << m[a][b] << endl;
		else {

			if( (c-dist[a][b]) % 2 == 0) cout << m[a][b] << endl;
			else {
				if(m[a][b] == '1') cout << 0 << endl;
				else cout << 1 << endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define int long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 2e5+5;
ll expo(ll a, ll b){
    if(b == 0)
		return 1;
    ll x = expo(a, b/2);
    return (b & 1 ? ((x*x)*a) : (x*x) );
}

int x[] = {1, -1, 0, 0}, l, r, q;
int y[] = {0, 0, 1, -1};

bool used[1005][1005];
char m[1005][1005];
int dist[1005][1005];

vector<pii> check(int a, int b) {
	vector<pii> bfs;
	queue<pii> ths;
	ths.push({a, b});

	while(!ths.empty()) {
		pii kappa = ths.front();
		ths.pop();
		a = kappa.f, b = kappa.s;

		bool pxd = 0;

		for(int i = 0; i < 4; i++) {

			int na = a + x[i], nb = b + y[i];

			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;

			if(!used[na][nb]) {
				ths.push({na, nb});
				used[na][nb] = 1;
			}

			if(m[na][nb] == m[a][b]  && !pxd) {
				bfs.pb({a, b});
				pxd = 1;
			}

		}

	}
	return bfs;
}

void solve() {

	vector<pii> vet = check(0, 0);

	for(int i = 0; i < 1005; i++)
		for(int j = 0; j < 1005; j++)
			dist[i][j] = LONG_MAX;

	queue<pii> fila;
	for(int i = 0; i < vet.size(); i++) {
		dist[vet[i].f][vet[i].s] = 0;
		fila.push({vet[i].f, vet[i].s});
	}

	while(!fila.empty()) {

		pii kappa = fila.front();
		int a = kappa.f, b = kappa.s;
		fila.pop();

		for(int i = 0; i < 4; i++) {
			int na = a + x[i], nb = b + y[i];
			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;
			if(dist[a][b] + 1 < dist[na][nb]) {
				dist[na][nb] = dist[a][b] + 1;
				fila.push({na, nb});
			}
		}

	}

}

int32_t main() {
	cin >> l >> r >> q;
	for(int i = 0; i < l; i++)
		for(int j = 0; j < r; j++)
			cin >> m[i][j];

	solve();
	//for(int i = 0; i < l; cout << endl, i++)
	//	for(int j = 0; j < r; j++)
	//		if(dist[i][j] != LONG_MAX) cout << "NONNON" << " ";

	while(q--) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		if(dist[a][b] > c)
			cout << m[a][b] << endl;
		else {

			if( (c-dist[a][b]) % 2 == 0) cout << m[a][b] << endl;
			else {
				if(m[a][b] == '1') cout << 0 << endl;
				else cout << 1 << endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define int long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 2e5+5;
ll expo(ll a, ll b){
    if(b == 0)
		return 1;
    ll x = expo(a, b/2);
    return (b & 1 ? ((x*x)*a) : (x*x) );
}

int x[] = {1, -1, 0, 0}, l, r, q;
int y[] = {0, 0, 1, -1};

bool used[1005][1005];
char m[1005][1005];
int dist[1005][1005];

vector<pii> check(int a, int b) {
	vector<pii> bfs;
	queue<pii> ths;
	ths.push({a, b});

	while(!ths.empty()) {
		pii kappa = ths.front();
		ths.pop();
		a = kappa.f, b = kappa.s;

		bool pxd = 0;

		for(int i = 0; i < 4; i++) {

			int na = a + x[i], nb = b + y[i];

			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;

			if(!used[na][nb]) {
				ths.push({na, nb});
				used[na][nb] = 1;
			}

			if(m[na][nb] == m[a][b]  && !pxd) {
				bfs.pb({a, b});
				pxd = 1;
			}

		}

	}
	return bfs;
}

void solve() {

	vector<pii> vet = check(0, 0);

	for(int i = 0; i < 1005; i++)
		for(int j = 0; j < 1005; j++)
			dist[i][j] = LONG_MAX;

	queue<pii> fila;
	for(int i = 0; i < vet.size(); i++) {
		dist[vet[i].f][vet[i].s] = 0;
		fila.push({vet[i].f, vet[i].s});
	}

	while(!fila.empty()) {

		pii kappa = fila.front();
		int a = kappa.f, b = kappa.s;
		fila.pop();

		for(int i = 0; i < 4; i++) {
			int na = a + x[i], nb = b + y[i];
			if(na < 0 or na >= l or nb < 0 or nb >= r) continue;
			if(dist[a][b] + 1 < dist[na][nb]) {
				dist[na][nb] = dist[a][b] + 1;
				fila.push({na, nb});
			}
		}

	}

}

int32_t main() {
	cin >> l >> r >> q;
	for(int i = 0; i < l; i++)
		for(int j = 0; j < r; j++)
			cin >> m[i][j];

	solve();
	/*for(int i = 0; i < l; cout << endl, i++)
		for(int j = 0; j < r; j++)
			cout << "NONNON" << " ";
	*/
	while(q--) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		if(dist[a][b] > c)
			cout << m[a][b] << endl;
		else {

			if( (c-dist[a][b]) % 2 == 0) cout << m[a][b] << endl;
			else {
				if(m[a][b] == '1') cout << 0 << endl;
				else cout << 1 << endl;
			}
		}
	}
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>

using namespace std;

using ll =  long long;
using Pii = pair<int, int>;

constexpr ll LINF = 1LL << 60;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

int n, m; ll t;

bool check(int y, int x) {
    return (0 <= y) && (y < n) && (0 <= x) && (x < m);
}

int main() {
    cin >> n >> m >> t;
    vector<vector<ll>> a(n, vector<ll>(m));
    for(int i=0;i<n;++i) {
        string s;
        cin >> s;
        for(int j=0;j<m;++j) {
            a[i][j] = (s[j] - '0');
        }
    }

    vector<vector<ll>> dist(n, vector<ll>(m, LINF));
    queue<Pii> que;
    for(int i=0;i<n;++i) {
        for(int j=0;j<m;++j) {
            for(int k=0;k<4;++k) {
                int y = i + dyx[k][0], x = j + dyx[k][1];
                if(check(y, x) && a[i][j] == a[y][x]) {
                    dist[i][j] = 0;
                    que.emplace(i, j);
                    break;
                }
            }
        }
    }

    while(!que.empty()) {
        Pii p = que.front(); que.pop();
        for(int k=0;k<4;++k) {
            int y = p.first + dyx[k][0], x = p.second + dyx[k][1];
            if(check(y, x) && dist[y][x] == LINF) {
                dist[y][x] = dist[p.first][p.second] + 1;
                que.emplace(y, x);
            }
        }
    }

    int y, x; ll p;
    for(int i=0;i<t;++i) {
        cin >> y >> x >> p;
        --y; --x;
        cout << (a[y][x] + max(0LL, p-dist[y][x])) % 2 << "\n";
    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

int n, m; ll t;

bool check(int y, int x) {
    return (0 <= y) && (y < n) && (0 <= x) && (x < m);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m >> t;
    vector<vector<int>> a(n, vector<int>(m));
    for(int i=0;i<n;++i) {
        string s;
        cin >> s;
        for(int j=0;j<m;++j) {
            a[i][j] = (s[j] - '0');
        }
    }

    vector<vector<int>> dist(n, vector<int>(m, LINF));
    queue<Pii> que;
    for(int i=0;i<n;++i) {
        for(int j=0;j<m;++j) {
            for(int k=0;k<4;++k) {
                int y = i + dyx[k][0], x = j + dyx[k][1];
                if(check(y, x) && a[i][j] == a[y][x]) {
                    dist[i][j] = 0;
                    que.emplace(i, j);
                    break;
                }
            }
        }
    }

    while(!que.empty()) {
        Pii p = que.front(); que.pop();
        for(int k=0;k<4;++k) {
            int y = p.first + dyx[k][0], x = p.second + dyx[k][1];
            if(check(y, x) && dist[y][x] == INF) {
                dist[y][x] = dist[p.first][p.second] + 1;
                que.emplace(y, x);
            }
        }
    }

    int y, x; ll p;
    for(int i=0;i<t;++i) {
        cin >> y >> x >> p;
        --y; --x;
        cout << (a[y][x] + max(0LL, p-dist[y][x])) % 2 << "\n";
    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>

using namespace std;

using ll =  long long;

constexpr ll LINF = 1LL << 60;

int n, m; ll t;

bool check(int y, int x) {
    return (0 <= y) && (y < n) && (0 <= x) && (x < m);
}

int main() {
    cin >> n >> m >> t;
    vector<vector<ll>> a(n, vector<ll>(m));
    for(int i=0;i<n;++i) {
        string s;
        cin >> s;
        for(int j=0;j<m;++j) {
            a[i][j] = (s[j] - '0');
        }
    }

    vector<vector<ll>> dist(n, vector<ll>(m, LINF));
    queue<Pii> que;
    for(int i=0;i<n;++i) {
        for(int j=0;j<m;++j) {
            for(int k=0;k<4;++k) {
                int y = i + dyx[k][0], x = j + dyx[k][1];
                if(check(y, x) && a[i][j] == a[y][x]) {
                    dist[i][j] = 0;
                    que.emplace(i, j);
                    break;
                }
            }
        }
    }

    while(!que.empty()) {
        Pii p = que.front(); que.pop();
        for(int k=0;k<4;++k) {
            int y = p.first + dyx[k][0], x = p.second + dyx[k][1];
            if(check(y, x) && dist[y][x] == LINF) {
                dist[y][x] = dist[p.first][p.second] + 1;
                que.emplace(y, x);
            }
        }
    }

    int y, x; ll p;
    for(int i=0;i<t;++i) {
        cin >> y >> x >> p;
        --y; --x;
        cout << (a[y][x] + max(0LL, p-dist[y][x])) % 2 << "\n";
    }

}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>

using namespace std;

using ll =  long long;
using Pii = pair<int, int>;

constexpr ll LINF = 1LL << 60;

int n, m; ll t;

bool check(int y, int x) {
    return (0 <= y) && (y < n) && (0 <= x) && (x < m);
}

int main() {
    cin >> n >> m >> t;
    vector<vector<ll>> a(n, vector<ll>(m));
    for(int i=0;i<n;++i) {
        string s;
        cin >> s;
        for(int j=0;j<m;++j) {
            a[i][j] = (s[j] - '0');
        }
    }

    vector<vector<ll>> dist(n, vector<ll>(m, LINF));
    queue<Pii> que;
    for(int i=0;i<n;++i) {
        for(int j=0;j<m;++j) {
            for(int k=0;k<4;++k) {
                int y = i + dyx[k][0], x = j + dyx[k][1];
                if(check(y, x) && a[i][j] == a[y][x]) {
                    dist[i][j] = 0;
                    que.emplace(i, j);
                    break;
                }
            }
        }
    }

    while(!que.empty()) {
        Pii p = que.front(); que.pop();
        for(int k=0;k<4;++k) {
            int y = p.first + dyx[k][0], x = p.second + dyx[k][1];
            if(check(y, x) && dist[y][x] == LINF) {
                dist[y][x] = dist[p.first][p.second] + 1;
                que.emplace(y, x);
            }
        }
    }

    int y, x; ll p;
    for(int i=0;i<t;++i) {
        cin >> y >> x >> p;
        --y; --x;
        cout << (a[y][x] + max(0LL, p-dist[y][x])) % 2 << "\n";
    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

int n, m; ll t;

bool check(int y, int x) {
    return (0 <= y) && (y < n) && (0 <= x) && (x < m);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m >> t;
    vector<vector<int>> a(n, vector<int>(m));
    for(int i=0;i<n;++i) {
        string s;
        cin >> s;
        for(int j=0;j<m;++j) {
            a[i][j] = (s[j] - '0');
        }
    }

    vector<vector<int>> dist(n, vector<int>(m, INF));
    queue<Pii> que;
    for(int i=0;i<n;++i) {
        for(int j=0;j<m;++j) {
            for(int k=0;k<4;++k) {
                int y = i + dyx[k][0], x = j + dyx[k][1];
                if(check(y, x) && a[i][j] == a[y][x]) {
                    dist[i][j] = 0;
                    que.emplace(i, j);
                    break;
                }
            }
        }
    }

    while(!que.empty()) {
        Pii p = que.front(); que.pop();
        for(int k=0;k<4;++k) {
            int y = p.first + dyx[k][0], x = p.second + dyx[k][1];
            if(check(y, x) && dist[y][x] == INF) {
                dist[y][x] = dist[p.first][p.second] + 1;
                que.emplace(y, x);
            }
        }
    }

    int y, x; ll p;
    for(int i=0;i<t;++i) {
        cin >> y >> x >> p;
        --y; --x;
        cout << (a[y][x] + max(0LL, p-dist[y][x])) % 2 << "\n";
    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

int n, m; ll t;

bool check(int y, int x) {
    return (0 <= y) && (y < n) && (0 <= x) && (x < m);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m >> t;
    vector<vector<int>> a(n, vector<int>(m));
    for(int i=0;i<n;++i) {
        string s;
        cin >> s;
        for(int j=0;j<m;++j) {
            a[i][j] = (s[j] - '0');
        }
    }

    vector<vector<ll>> dist(n, vector<ll>(m, LINF));
    queue<Pii> que;
    for(int i=0;i<n;++i) {
        for(int j=0;j<m;++j) {
            for(int k=0;k<4;++k) {
                int y = i + dyx[k][0], x = j + dyx[k][1];
                if(check(y, x) && a[i][j] == a[y][x]) {
                    dist[i][j] = 0;
                    que.emplace(i, j);
                    break;
                }
            }
        }
    }

    while(!que.empty()) {
        Pii p = que.front(); que.pop();
        for(int k=0;k<4;++k) {
            int y = p.first + dyx[k][0], x = p.second + dyx[k][1];
            if(check(y, x) && dist[y][x] == INF) {
                dist[y][x] = dist[p.first][p.second] + 1;
                que.emplace(y, x);
            }
        }
    }

    int y, x; ll p;
    for(int i=0;i<t;++i) {
        cin >> y >> x >> p;
        --y; --x;
        cout << (a[y][x] + max(0LL, p-dist[y][x])) % 2 << "\n";
    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

int n, m; ll t;

bool check(int y, int x) {
    return (0 <= y) && (y < n) && (0 <= x) && (x < m);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m >> t;
    vector<vector<ll>> a(n, vector<ll>(m));
    for(int i=0;i<n;++i) {
        string s;
        cin >> s;
        for(int j=0;j<m;++j) {
            a[i][j] = (s[j] - '0');
        }
    }

    vector<vector<ll>> dist(n, vector<ll>(m, LINF));
    queue<Pii> que;
    for(int i=0;i<n;++i) {
        for(int j=0;j<m;++j) {
            for(int k=0;k<4;++k) {
                int y = i + dyx[k][0], x = j + dyx[k][1];
                if(check(y, x) && a[i][j] == a[y][x]) {
                    dist[i][j] = 0;
                    que.emplace(i, j);
                    break;
                }
            }
        }
    }

    while(!que.empty()) {
        Pii p = que.front(); que.pop();
        for(int k=0;k<4;++k) {
            int y = p.first + dyx[k][0], x = p.second + dyx[k][1];
            if(check(y, x) && dist[y][x] == LINF) {
                dist[y][x] = dist[p.first][p.second] + 1;
                que.emplace(y, x);
            }
        }
    }

    int y, x; ll p;
    for(int i=0;i<t;++i) {
        cin >> y >> x >> p;
        --y; --x;
        cout << (a[y][x] + max(0LL, p-dist[y][x])) % 2 << "\n";
    }

}


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

int n,m;
bool valid(int x,int y)
{
    if(x>=0 && x<n && y>=0 && y<m)
        return true;
    return false;
}
int main()
{
    int t;
    cin>>n>>m>>t;
    string s[n];

    int badiya[n][m]={0},dist[n][m],kitnetimeme[n][m];
    memset(dist,-1,sizeof(dist));
    memset(badiya,0,sizeof(badiya));

    queue<pair<int,int>> q; ;

    for(int i=0;i<n;i++)
        cin>>s[i];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(valid(i-1,j) && s[i-1][j]==s[i][j])
                badiya[i][j]=1;

            if(valid(i+1,j) && s[i+1][j]==s[i][j])
                badiya[i][j]=1;

            if(valid(i,j-1) && s[i][j-1]==s[i][j])
                badiya[i][j]=1;

            if(valid(i,j+1) && s[i][j+1]==s[i][j])
                badiya[i][j]=1;

            if(badiya[i][j])
            {
                kitnetimeme[i][j]=0;
                q.push({i,j});
            }
            else
                kitnetimeme[i][j]=INT_MAX;

        }
    }

    while(!q.empty())
    {
        pair<int,int> p=q.front();
        q.pop();
        int i= p.first;
        int j= p.second;

        if(valid(i-1,j) && kitnetimeme[i-1][j]==INT_MAX)
        {
                kitnetimeme[i-1][j]=kitnetimeme[i][j]+1;
                q.push({i-1,j});
        }
        if(valid(i+1,j) && kitnetimeme[i+1][j]==INT_MAX)
        {
                kitnetimeme[i+1][j]=kitnetimeme[i][j]+1;
                q.push({i+1,j});
        }
        if(valid(i,j-1) && kitnetimeme[i][j-1]==INT_MAX)
        {
                kitnetimeme[i][j-1]=kitnetimeme[i][j]+1;
                q.push({i,j-1});
        }
        if(valid(i,j+1) && kitnetimeme[i][j+1]==INT_MAX)
        {
                kitnetimeme[i][j+1]=kitnetimeme[i][j]+1;
                q.push({i,j+1});
        }
    }

    while(t--)
    {
        int i,j;
        ll p;
        cin>>i>>j>>p;

        if(p<kitnetimeme[i-1][j-1])
            cout<<s[i-1][j-1]<<endl;
        else
        {
            ll diff= p- kitnetimeme[i-1][j-1];
            if(diff&1)
            {
                if(s[i-1][j-1]=='0')
                    cout<<1<<endl;
                else
                    cout<<0<<endl;
            }
            else
                cout<<s[i-1][j-1]<<endl;
        }

    }
    return 0;
}


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

int main()
{
    int n,m,t;
    cin>>n>>m>>t;
    char arr[n+2][m+2],temp[n+2][m+2],save[n+1][m+1];
    int toggle[n+1][m+1]={0};
    vector<int> adj[n+1][m+1];
    for(int i=0;i<=m+1;i++)
    {
        arr[0][i]='2';
        arr[n+1][i]='2';
        temp[0][i]='2';
        temp[n+1][i]='2';
    }
    for(int i=0;i<=n+1;i++)
    {
        arr[i][0]='2';
        arr[i][m+1]='2';
        temp[i][0]='2';
        temp[i][m+1]='2';
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>arr[i][j];
            save[i][j]=arr[i][j];
            temp[i][j]=arr[i][j];
        }
    }
    int rowsame=1,colsame=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=2;j<=m;j++)
        {
            if(arr[i][j]!=arr[i][j-1])
                rowsame=0;
        }
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(arr[i][j]!=arr[i-1][j])
                colsame=0;
        }
    }
    int iteration=0,filled,alwaysame;
    if(rowsame==0 && colsame==0)
    {
        while(1)
        {
            iteration++;
            alwaysame=1;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    int same=0;
                    if(arr[i][j]==arr[i-1][j])
                        same=1;
                    if(arr[i][j]==arr[i+1][j])
                        same=1;
                    if(arr[i][j]==arr[i][j-1])
                        same=1;
                    if(arr[i][j]==arr[i][j+1])
                        same=1;
                    if(same)
                    {
                        toggle[i][j]=1-toggle[i][j];
                        alwaysame=0;
                        if(arr[i][j]=='1')
                            temp[i][j]='0';
                        else
                            temp[i][j]='1';
                    }
                    adj[i][j].push_back(toggle[i][j]);
                }
            }
            if(alwaysame)
                break;
            int zero=0,one=0;
            for(int i=0;i<=n+1;i++)
            {
                for(int j=0;j<=m+1;j++)
                {
                    arr[i][j]=temp[i][j];

                    if(arr[i][j]=='0')
                        zero++;
                    else if(arr[i][j]=='1')
                        one++;
                }
            }
            if(zero==0)
            {
                filled=1;
                break;
            }
            else if(one==0)
            {
                filled=0;
                break;
            }
        }
    }
    while(t--)
    {
        int i,j;
        ll p;
        cin>>i>>j>>p;
        if(rowsame || colsame)
        {
            if( !(p&1) )
                cout<<save[i][j]<<endl;
            else
            {
                if(save[i][j]=='0')
                    cout<<1<<endl;
                else
                    cout<<0<<endl;
            }

        }
        else if(alwaysame)
            cout<<save[i][j]<<endl;
        else
        {
            if(p<iteration)
            {
                if(adj[i][j][p-1])
                {
                    if(save[i][j]=='1')
                        cout<<0<<endl;
                    else
                        cout<<1<<endl;
                }
                else
                    cout<<save[i][j]<<endl;
            }
            else
            {
                if( (p- iteration) &1 )
                    cout<<1-filled<<endl;
                else
                    cout<<filled<<endl;
            }
        }
    }
    return 0;
}


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

int n,m;
bool valid(int x,int y)
{
    if(x>=0 && x<n && y>=0 && y<m)
        return true;
    return false;
}
int main()
{
    int t;
    cin>>n>>m>>t;
    string s[n];

    int badiya[n][m]={0},dist[n][m],kitnetimeme[n][m];
    memset(dist,-1,sizeof(dist));
    memset(badiya,0,sizeof(badiya));

    queue<pair<int,int>> q; ;

    for(int i=0;i<n;i++)
        cin>>s[i];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(valid(i-1,j) && s[i-1][j]==s[i][j])
                badiya[i][j]=1;
            //cout<<badiya[i][j]<<" ";

            if(valid(i+1,j) && s[i+1][j]==s[i][j])
                badiya[i][j]=1;
            //cout<<badiya[i][j]<<" ";

            if(valid(i,j-1) && s[i][j-1]==s[i][j])
                badiya[i][j]=1;
            //cout<<badiya[i][j]<<" ";

            if(valid(i,j+1) && s[i][j+1]==s[i][j])
                badiya[i][j]=1;
            //cout<<badiya[i][j]<<endl;

            if(badiya[i][j])
            {
                kitnetimeme[i][j]=0;
                q.push({i,j});
            }
            else
                kitnetimeme[i][j]=INT_MAX;

            //cout<<i<<" "<<j<<" "<<badiya[i][j]<<" "<<kitnetimeme[i][j]<<endl;

        }
    }

    while(!q.empty())
    {
        pair<int,int> p=q.front();
        q.pop();
        int i= p.first;
        int j= p.second;

        if(valid(i-1,j) && kitnetimeme[i-1][j]==INT_MAX)
        {
                kitnetimeme[i-1][j]=kitnetimeme[i][j]+1;
                q.push({i-1,j});
        }
        if(valid(i+1,j) && kitnetimeme[i+1][j]==INT_MAX)
        {
                kitnetimeme[i+1][j]=kitnetimeme[i][j]+1;
                q.push({i+1,j});
        }
        if(valid(i,j-1) && kitnetimeme[i][j-1]==INT_MAX)
        {
                kitnetimeme[i][j-1]=kitnetimeme[i][j]+1;
                q.push({i,j-1});
        }
        if(valid(i,j+1) && kitnetimeme[i][j+1]==INT_MAX)
        {
                kitnetimeme[i][j+1]=kitnetimeme[i][j]+1;
                q.push({i,j+1});
        }
    }

    while(t--)
    {
        int i,j;
        ll p;
        cin>>i>>j>>p;

        //cout<<kitnetimeme[i-1][j-1]<<" ";

        if(p<kitnetimeme[i-1][j-1])
            cout<<s[i-1][j-1]<<endl;
        else
        {
            int diff= p- kitnetimeme[i-1][j-1];
            if(diff&1)
            {
                if(s[i-1][j-1]=='0')
                    cout<<1<<endl;
                else
                    cout<<0<<endl;
            }
            else
                cout<<s[i-1][j]<<endl;
        }

    }
    return 0;
}


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

int main()
{
    int n,m,t;
    cin>>n>>m>>t;
    char arr[n+2][m+2],temp[n+2][m+2],save[n+1][m+1];
    int toggle[n+1][m+1]={0};
    vector<int> adj[n+1][m+1];
    for(int i=0;i<=m+1;i++)
    {
        arr[0][i]='2';
        arr[n+1][i]='2';
        temp[0][i]='2';
        temp[n+1][i]='2';
    }
    for(int i=0;i<=n+1;i++)
    {
        arr[i][0]='2';
        arr[i][m+1]='2';
        temp[i][0]='2';
        temp[i][m+1]='2';
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>arr[i][j];
            save[i][j]=arr[i][j];
            temp[i][j]=arr[i][j];
        }
    }
    int rowsame=1,colsame=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=2;j<=m;j++)
        {
            if(arr[i][j]!=arr[i][j-1])
                rowsame=0;
        }
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(arr[i][j]!=arr[i-1][j])
                colsame=0;
        }
    }
    int iteration=0,filled,alwaysame;
    if(rowsame==0 && colsame==0)
    {
        while(1)
        {
            iteration++;
            alwaysame=1;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    int same=0;
                    if(arr[i][j]==arr[i-1][j])
                        same=1;
                    if(arr[i][j]==arr[i+1][j])
                        same=1;
                    if(arr[i][j]==arr[i][j-1])
                        same=1;
                    if(arr[i][j]==arr[i][j+1])
                        same=1;
                    if(same)
                    {
                        toggle[i][j]=1-toggle[i][j];
                        alwaysame=0;
                        if(arr[i][j]=='1')
                            temp[i][j]='0';
                        else
                            temp[i][j]='1';
                    }
                    adj[i][j].push_back(toggle[i][j]);
                }
            }
            if(alwaysame)
                break;
            int zero=0,one=0;
            for(int i=0;i<=n+1;i++)
            {
                for(int j=0;j<=m+1;j++)
                {
                    arr[i][j]=temp[i][j];

                    if(arr[i][j]=='0')
                        zero++;
                    else if(arr[i][j]=='1')
                        one++;
                }
            }
            if(zero==0)
            {
                filled=1;
                break;
            }
            else if(one==0)
            {
                filled=0;
                break;
            }
        }
    }
    while(t--)
    {
        int i,j;
        ll p;
        cin>>i>>j>>p;
        if(n==1)
        {
            if(save[i][j]!=save[i][j-1]  && save[i][j]!=save[i][j+1])
                cout<<save[i][j]<<endl;
            else
            {
                if(p&1)
                {
                    if(save[i][j]=='0')
                        cout<<1<<endl;
                    else
                        cout<<0<<endl;
                }
                else
                    cout<<save[i][j]<<endl;
            }
        }
        else if(m==1)
        {
            if(save[i][j]!=save[i-1][j]  && save[i][j]!=save[i+1][j])
                cout<<save[i][j]<<endl;
            else
            {
                if(p&1)
                {
                    if(save[i][j]=='0')
                        cout<<1<<endl;
                    else
                        cout<<0<<endl;
                }
                else
                    cout<<save[i][j]<<endl;
            }
        }
        else if(rowsame || colsame)
        {
            if( !(p&1) )
                cout<<save[i][j]<<endl;
            else
            {
                if(save[i][j]=='0')
                    cout<<1<<endl;
                else
                    cout<<0<<endl;
            }

        }
        else if(alwaysame)
            cout<<save[i][j]<<endl;
        else
        {
            if(p<iteration)
            {
                if(adj[i][j][p-1])
                {
                    if(save[i][j]=='1')
                        cout<<0<<endl;
                    else
                        cout<<1<<endl;
                }
                else
                    cout<<save[i][j]<<endl;
            }
            else
            {
                if( (p- iteration) &1 )
                    cout<<1-filled<<endl;
                else
                    cout<<filled<<endl;
            }
        }
    }
    return 0;
}


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

int main()
{
    int n,m,t;
    cin>>n>>m>>t;
    char arr[n+2][m+2],temp[n+2][m+2],save[n+1][m+1];
    int toggle[n+1][m+1]={0};
    vector<int> adj[n+1][m+1];
    for(int i=0;i<=m+1;i++)
    {
        arr[0][i]='2';
        arr[n+1][i]='2';
        temp[0][i]='2';
        temp[n+1][i]='2';
    }
    for(int i=0;i<=n+1;i++)
    {
        arr[i][0]='2';
        arr[i][m+1]='2';
        temp[i][0]='2';
        temp[i][m+1]='2';
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>arr[i][j];
            save[i][j]=arr[i][j];
            temp[i][j]=arr[i][j];
        }
    }
    int rowsame=1,colsame=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=2;j<=m;j++)
        {
            if(arr[i][j]!=arr[i][j-1])
                rowsame=0;
        }
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(arr[i][j]!=arr[i-1][j])
                colsame=0;
        }
    }
    int iteration=0,filled,alwaysame;
    if(rowsame==0 && colsame==0)
    {
        while(1)
        {
            iteration++;
            alwaysame=1;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    int same=0;
                    if(arr[i][j]==arr[i-1][j])
                        same=1;
                    if(arr[i][j]==arr[i+1][j])
                        same=1;
                    if(arr[i][j]==arr[i][j-1])
                        same=1;
                    if(arr[i][j]==arr[i][j+1])
                        same=1;
                    if(same)
                    {
                        toggle[i][j]=1-toggle[i][j];
                        alwaysame=0;
                        if(arr[i][j]=='1')
                            temp[i][j]='0';
                        else
                            temp[i][j]='1';
                    }
                    adj[i][j].push_back(toggle[i][j]);
                }
            }
            if(alwaysame)
                break;
            int zero=0,one=0;
            for(int i=0;i<=n+1;i++)
            {
                for(int j=0;j<=m+1;j++)
                {
                    arr[i][j]=temp[i][j];

                    if(arr[i][j]=='0')
                        zero++;
                    else if(arr[i][j]=='1')
                        one++;
                }
            }
            if(zero==0)
            {
                filled=1;
                break;
            }
            else if(one==0)
            {
                filled=0;
                break;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cout<<save[i][j];
        }
        cout<<endl;
    }
    while(t--)
    {
        int i,j;
        ll p;
        cin>>i>>j>>p;
        if(rowsame || colsame)
        {
            if( !(p&1) )
                cout<<save[i][j]<<endl;
            else
            {
                if(save[i][j]=='0')
                    cout<<1<<endl;
                else
                    cout<<0<<endl;
            }

        }
        else if(alwaysame)
            cout<<save[i][j]<<endl;
        else
        {
            if(p<iteration)
            {
                if(adj[i][j][p-1])
                {
                    if(save[i][j]=='1')
                        cout<<0<<endl;
                    else
                        cout<<1<<endl;
                }
                else
                    cout<<save[i][j]<<endl;
            }
            else
            {
                if( (p- iteration) &1 )
                    cout<<1-filled<<endl;
                else
                    cout<<filled<<endl;
            }
        }
    }
    return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;

// C. Make It Good
// https://codeforces.com/problemset/problem/1385/C
//

void solve(int n, vector<int>& a)
{
	bool inc = true, dec = true;
	for (int i=0; i<n-1; i++)
	{
		if (a[i] > a[i+1])
			inc = false;
		if (a[i] < a[i+1])
			dec = false;
	}
	if (inc || dec)
	{
		cout << 0 << endl;
		return;
	}
	int i = n-1;
	while (i-1>=0 && a[i-1]>=a[i])
	{
		i--;
	}
	const int peak = i;
	i = peak;
	while (i-1>=0 && a[i-1]<=a[i])
	{
		i--;
	}
	const int valley = i;
	int ans = valley;
	cout << ans << endl;
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
	std::ifstream in("inputC.txt");
	std::cin.rdbuf(in.rdbuf()); //redirect std::cin to input.txt!
	std::ofstream out("outC.txt");
	std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
#endif
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<int> a(n);
		for (int i=0; i<n; i++)
			cin >> a[i];
		solve(n, a);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include <vector>
#include<algorithm>
#include<cstring>
#include<map>
#include <queue>
using namespace std;
int a[20005];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n,center;
		cin >> n;
		center = n;
		for (int i = 1; i <= n; i++)
		{
			scanf_s("%d", &a[i]);
		}
		for (int i = n; i >= 1; i--)
		{
			if (a[i] > a[i - 1])
			{
				center = i;
				break;
			}
		}
		int end = 0;
		for (int i = center; i >=2; i--)
		{
			if (a[i] < a[i - 1])
			{
				end = i;
				break;
			}
		}
		end--;
		if (end == -1)
			end++;
		cout << end << endl;
	}
}


#include<iostream>
#include<cstdio>
#include<string>
#include <vector>
#include<algorithm>
#include<cstring>
#include<map>
#include <queue>
using namespace std;
queue < int > q;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n,center;
		int a[20005];
		cin >> n;
		center = n;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
		}
		for (int i = n; i >= 1; i--)
		{
			if (a[i] > a[i - 1])
			{
				center = i;
				break;
			}
		}
		int end = 0;
		for (int i = center; i >=2; i--)
		{
			if (a[i] < a[i - 1])
			{
				end = i;
				break;
			}
		}
		end--;
		if (end == -1)
			end++;
		cout << end << endl;
	}
}


#include<iostream>
#include<cstdio>
#include<string>
#include <vector>
#include<algorithm>
#include<cstring>
#include<map>
#include <queue>
using namespace std;
int a[200005];
int main()
{
	int t;
	scanf_s("%d", &t);
	while (t--)
	{
		int n,center;
		int f1=0, f2=0;
		int s1, s2;
		cin >> n;
		center = n;
		scanf_s("%d", &a[1]);
		for (int i = 2; i <= n; i++)
		{
			scanf_s("%d", &a[i]);
		}
		for (int i = n; i >= 1; i--)
		{
			if (a[i] > a[i - 1])
			{
				center = i;
				break;
			}
		}
		int end = 0;
		for (int i = center; i >=2; i--)
		{
			if (a[i] < a[i - 1])
			{
				end = i;
				break;
			}
		}
		end--;
		if (end == -1)
			end++;
		printf("%d\n", end);
	}
}


#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <string>
using namespace std;

int main()
{
	int64_t q;
	cin >> q;

	for (int64_t x = 0; x < q; x++)
	{
		int64_t n;
		cin >> n;

		vector<int64_t> v(n);
		for (int64_t i = 0; i < n; i++)
		{
			cin >> v[i];
		}

		int64_t ans = 0;
		bool flag = true;
		for (int64_t i = v.size() - 2; i >= 0; i--)
		{
			if (flag)
			{
				if (v[i] < v[i + 1])
				{
					flag = false;
				}
			}
			else
			{
				if (v[i] > v[i + 1])
				{
					ans = i + 1;
					break;
				}
			}
		}

		cout << ans << endl;
	}
}


#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <string>
using namespace std;

int main()
{
	int64_t q;
	cin >> q;

	for (int64_t x = 0; x < q; x++)
	{
		int64_t n;
		cin >> n;
		if (n == 1)
		{
			cin >> n;
			cout << 0 << endl;
			continue;
		}

		vector<int64_t> v(n);
		for (int64_t i = 0; i < n; i++)
		{
			cin >> v[i];
		}

		int64_t ans = 0;
		bool flag = true;
		for (int64_t i = v.size() - 2; i >= 0; i--)
		{
			if (flag)
			{
				if (v[i] < v[i + 1])
				{
					flag = false;
				}
			}
			else
			{
				if (v[i] > v[i + 1])
				{
					ans = i + 1;
					break;
				}
			}
		}

		cout << ans << endl;
	}
}


//#include<bits/stdc++.h>
//#define ll long long
//#define INF 0x3f3f3f3f
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
	std::ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		int n, a[200005];
		cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		int flag = 0, i;
		for (i = n - 1; i >= 0; i--) {
			if (a[i]<a[i+1] && flag == 0)flag = 1;
			if (a[i] > a[i + 1]&&flag==1)break;
		}
		if (i == -1)i = 0;
		cout << i << endl;
	}
	return 0;
}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <numeric>
#include <iomanip>
#include <map>
#include <stack>
#include <sstream>
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
using namespace std;
const int MAX = 1e6;
long long max(long long a, long long b) { if (a > b)return a; else return b; }
long long min(long long a, long long b) { if (a < b)return a; else return b; }
const int MAXN = 1000000 + 30;
int a[MAXN];
int main()
{

	int t; cin >> t;
	while (t--)
	{
		int n; cin >> n;
		vector<int> v(n);
		for (int i = 0; i < n; i++)
		{
			cin >> v[i];
		}

		bool tf1 = false, tf2 = false;
		int ans = 0;
		for (int i = n - 1; i; i--)
		{
			if (v[i - 1] == v[i])
			{
				continue;
			}
			if (v[i - 1] > v[i])
			{
				if (tf1)
				{
					ans = i;
					break;
				}
				tf2 = true;
			}
			else
			{
				tf1 = true;
			}
		}
		cout << ans << '\n';
	}

	return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int T, N;
vector<int> ans;
int arr[200000];
int dp[200000];
int dir[200000];
int same[200000];

int main()
{
	int i, t;
	cin >> T;
	for (t = 0; t < T; t++) {
		cin >> N;

		for (i = 0; i < N; i++) {
			dp[i] = 1; dir[i] = 0; same[i] = 1;
		}

		for (i = 0; i < N; i++) {
			cin >> arr[i];
		}
		for (i = 1; i < N; i++) {
			if (arr[i] > arr[i - 1]) {
				if (dir[i - 1] == -1) {
					dp[i] = same[i - 1] + 1;
				}
				else {
					dp[i] = dp[i - 1] + 1;
				}
			}
			else if (arr[i] == arr[i - 1]) {
				dp[i] = dp[i - 1] + 1;
				dir[i] = dir[i - 1];
				same[i] = same[i - 1] + 1;
			}
			else {
				dp[i] = dp[i - 1] + 1;
				dir[i] = -1;
			}
		}
		ans.push_back(N-dp[N - 1]);
	}

	for (int i : ans) {
		cout << i << "\n";
	}
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int T, N;
vector<int> ans;
int arr[200000];
int dp[200000];
int dir[200000];

int main()
{
	int i, t;
	cin >> T;
	for (t = 0; t < T; t++) {
		cin >> N;

		for (i = 0; i < N; i++) {
			dp[i] = 1; dir[i] = 0;
		}

		for (i = 0; i < N; i++) {
			cin >> arr[i];
		}
		for (i = 1; i < N; i++) {
			if (arr[i] > arr[i - 1]) {
				if (dir[i - 1] == -1) {
					dp[i] = 2;
				}
				else {
					dp[i] = dp[i - 1] + 1;
				}
			}
			else if (arr[i] == arr[i - 1]) {
				dp[i] = dp[i - 1] + 1;
				dir[i] = dir[i - 1];
			}
			else {
				dp[i] = dp[i - 1] + 1;
				dir[i] = -1;
			}
		}
		ans.push_back(N-dp[N - 1]);
	}

	for (int i : ans) {
		cout << i << "\n";
	}
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;
int T, N;
vector<int> ans;
int arr[200000];
int dp[200000];
int dir[200000];

int main()
{
	int i, t;
	cin >> T;
	for (t = 0; t < T; t++) {
		cin >> N;

		for (i = 0; i < N; i++) {
			dp[i] = 0; dir[i] = 0;
		}

		for (i = 0; i < N; i++) {
			cin >> arr[i];
		}

		dp[0] = 1;
		for (i = 1; i < N; i++) {
			if (arr[i] > arr[i - 1]) { //����
				if (dir[i - 1] == -1) {
					dp[i] = 2;
				}
				else {
					dp[i] = dp[i - 1] + 1;
					dir[i] = 1;
				}
			}
			else if (arr[i] == arr[i - 1]) {
				dp[i] = dp[i - 1] + 1;
				dir[i] = dir[i - 1];
			}
			else {
				dp[i] = dp[i - 1] + 1;
				dir[i] = -1;
			}
		}
		ans.push_back(N-dp[N - 1]);
	}

	for (int i : ans) {
		cout << i << "\n";
	}
	return 0;
}


#define _CRT_SECURE_NO_WARINGS
#include <iostream>
#include <iomanip>
#include <map>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <fstream>
#include <map>
#include <unordered_map>
using namespace std;
typedef int me;
typedef long long ll;
typedef unsigned long long ull;
#define andrew               \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0)
#define endl '\n'
#define sz(v) (int)(v.size())
#define all(v) v.begin(), v.end()
#define PI 3.1415926535
#define sin(i) (long double)sin(i *PI / 180.0)
#define cos(i) (long double)cos(i * PI/ 180.0)
ll gcd(ll x, ll y)
{
    return (y == 0) ? x : gcd(y, x % y);
}
ull lcm(ll x, ll y) { return x / gcd(x, y) * y; }
int dy8[] = { 0, 0, 1, -1, 1, -1, -1, 1 };
int dx8[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
int id8[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
int n, m;
int main()
{
    andrew;
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int>v(n);
        for (auto& it : v)
            cin >> it;

        int indx = n - 1;
        while (indx > 0 && v[indx - 1] >= v[indx]) {
            indx--;
        }
        while (indx > 0 && v[indx - 1] <= v[indx]){
            indx--;
        }

        cout << indx << endl;
    }

}
/*
3
0 6
6 0
3 4
*/


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

using namespace std;
typedef int32_t i;
#define endl '\n'

int main()
{
    i t;
    cin >> t;
    while (t--)
    {
        i n;
        cin >> n;

        vector<i> a(n);
        for (auto &k : a)
            cin >> k;
            if (n == 1)
        {
            cout << 0 << endl;
            continue;
        }
        bool is = 0;
        i ans = 0;
        for (i f = n - 2; f >= 0; f--)
        {
            if (is)
            {
                if (a[f] > a[f + 1])
                {
                    ans = f+1;
                    break;
                }
            }
            else if (a[f] < a[f + 1])
                is = 1;
        }
        cout << ans << endl;
    }
    return 0;
}


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

using namespace std;
typedef int32_t i;
#define endl '\n'

int main()
{
    i t;
    cin >> t;
    while (t--)
    {
        i n;
        cin >> n;
        if (n == 1)
        {
            cout << 0 << endl;
            continue;
        }
        vector<i> a(n);
        for (auto &k : a)
            cin >> k;
        bool is = 0;
        i ans = 0;
        for (i f = n - 2; f >= 0; f--)
        {
            if (is)
            {
                if (a[f] > a[f + 1])
                {
                    ans = f+1;
                    break;
                }
            }
            else if (a[f] < a[f + 1])
                is = 1;
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <map>

using namespace std;

int n, pos;
inline void solve() {

	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	pos = n - 1;
	while (pos > 0 && a[pos - 1] >= a[pos])
		pos--;
	while (pos > 0 && a[pos - 1] <= a[pos])
		pos--;
	cout << pos << endl;
}

int main() {
	int t;
	cin >> t;
	while (t--) {
		solve();
	}

	int u;
	//cin >> u;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            int b;
            cin >> b;
            a[i] = b;
        }
        int x = 0, y = 0;
        for (int i = n - 1; i > 0; i--) {
            if (a[i] > a[i - 1]) {
                for (int j = i - 1; j > 0; j--) {
                    if (a[j] < a[j - 1]) {
                        x = j;
                        break;
                    }
                }
                break;
            }
        }
        for (int i = n - 1; i > 0; i--) {
            if (a[i] < a[i - 1]) {
                y = i;
                break;
            }
        }
        cout << min(x, y);
        cout << endl;

    }
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            int b;
            cin >> b;
            a[i] = b;
        }
        int x = n, y = n;
        for (int i = n - 1; i > 0; i--) {
            if (a[i] > a[i - 1]) {
                for (int j = i - 1; j > 0; j--) {
                    if (a[j] < a[j - 1]) {
                        x = j;
                        break;
                    }
                }
                break;
            }
        }
        for (int i = n - 1; i > 0; i--) {
            if (a[i] < a[i - 1]) {
                y = i;
                break;
            }
        }
        cout << min(x, y);
        cout << endl;

    }
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            int b;
            cin >> b;
            a[i] = b;
        }
        for (int i = 0; i < n; i++) {
            bool up = true, down = true, up_down = true;
            for(int j = i; j < n - 1; j++){
                if (a[j] > a[j + 1]) {
                    up = false;
                    for (int g = j + 1; g < n - 1; g++) {
                        if (a[g] < a[g + 1]) {
                            up_down = false;
                            break;
                        }
                    }
                    break;
                }
            }
            for (int j = i; j < n - 1; j++) {
                if (a[j] < a[j + 1]) {
                    down = false;
                    break;
                }
            }
            if (up || down || up_down) {
                cout << i << endl;
                break;
            }
        }
        cout << endl;

    }
}


#include<iostream>

using namespace std;

int t, n;
int a[200001];

int main()
{
	cin >> t;
	while (t--)
	{
		int zt = 0, w = 1;
		cin >> n;
		for (int i = 1; i <= n; i++)
		cin >> a[i];
		for (int i = n; i > 1; i--)
		if (a[i] > a[i - 1])
		{
			zt = 1;
			break;
		}
		else
		if (a[i] < a[i - 1])
		{
			zt = 2;
			break;
		}
		if (zt == 0)
		{
			printf("0\n");
		}
		else
		{
			for (int i = n - 1; i > 1; i--)
			{
				if (a[i - 1] <= a[i] && a[i] > a[i + 1])zt--;
				if (a[i - 1] > a[i] && a[i] <= a[i + 1])zt--;
				if (zt == 0)
				{
					w = i;
					break;
				}
			}
			printf("%d\n", w - 1);
		}
	}
}


#include<iostream>

using namespace std;

int t, n;
int a[200005];

int main()
{
	cin >> t;
	while (t--)
	{
		int zt = 0, w = 1, jl;
		cin >> n;
		for (int i = 1; i <= n; i++)
		cin >> a[i];
		for (int i = n; i > 1; i--)
		if (a[i] > a[i - 1])
		{
			zt = 1;
			break;
		}
		else
		if (a[i] < a[i - 1])
		{
			zt = 2;
			break;
		}
		if (zt == 0)
		{
			printf("0\n");
		}
		else
		{
			int lst = n - 1, bj = 0;
			if (n > 1)
			for (int i = n - 1; i > 1; i--)
			if (a[i] != a[i - 1])
			{
				if (a[i - 1] < a[i] && a[i] > a[lst + 1])zt--;
				if (a[i - 1] > a[i] && a[i] < a[lst + 1])zt--;
				if (zt == 0)
				{
					w = i;
					break;
				}
				lst = i - 1;
				bj = 0;
			}
			else
			if (bj == 0) lst = i, bj = 1;
			printf("%d\n", w - 1);
		}
	}
	return 0;
}


#include<iostream>

using namespace std;

int t, n;
int a[200001];

int main()
{
	cin >> t;
	while (t--)
	{
		int zt = 0, w = 1;
		cin >> n;
		for (int i = 1; i <= n; i++)
		cin >> a[i];
		for (int i = n; i > 1; i--)
		if (a[i] > a[i - 1])
		{
			zt = 1;
			break;
		}
		else
		if (a[i] < a[i - 1])
		{
			zt = 2;
			break;
		}
		if (zt == 0)
		{
			printf("0\n");
		}
		else
		{
			for (int i = n - 1; i > 1; i--)
			{
				if (a[i - 1] < a[i] && a[i] > a[i + 1])zt--;
				if (a[i - 1] > a[i] && a[i] < a[i + 1])zt--;
				if (zt == 0)
				{
					w = i;
					break;
				}
			}
			printf("%d\n", w - 1);
		}
	}
}


#include<iostream>

using namespace std;

int t, n;
int a[200001];

int main()
{
	cin >> t;
	while (t--)
	{
		int zt = 0, w = 1;
		cin >> n;
		for (int i = 1; i <= n; i++)
		cin >> a[i];
		for (int i = n; i > 1; i--)
		if (a[i] > a[i - 1])
		{
			zt = 1;
			break;
		}
		else
		if (a[i] < a[i - 1])
		{
			zt = 2;
			break;
		}
		if (zt == 0)
		{
			printf("0\n");
		}
		else
		{
			if (n > 1)
			for (int i = n - 1; i > 1; i--)
			if (a[i] != a[i - 1])
			{
				if (a[i - 1] < a[i] && a[i] > a[i + 1])zt--;
				if (a[i - 1] > a[i] && a[i] < a[i + 1])zt--;
				if (zt == 0)
				{
					w = i;
					break;
				}
			}
			printf("%d\n", w - 1);
		}
	}
	return 0;
}


#include<iostream>

using namespace std;

int t, n;
int a[200001];

int main()
{
	cin >> t;
	while (t--)
	{
		int zt = 0, w = 1;
		cin >> n;
		for (int i = 1; i <= n; i++)
		cin >> a[i];
		for (int i = n; i > 1; i--)
		if (a[i] > a[i - 1])
		{
			zt = 1;
			break;
		}
		else
		if (a[i] < a[i - 1])
		{
			zt = 2;
			break;
		}
		if (zt == 0)
		{
			printf("0\n");
		}
		else
		{
			for (int i = n - 1; i > 1; i--)
			{
				if (a[i - 1] <= a[i] && a[i] > a[i + 1])zt--;
				if (a[i - 1] > a[i] && a[i] <= a[i + 1])zt--;
				if (zt == 0)
				{
					w = i;
					break;
				}
			}
			printf("%d\n", w - 1);
		}
	}
	return 0;
}


#include<iostream>

using namespace std;

int t, n;
int a[200001];

int main()
{
	cin >> t;
	while (t--)
	{
		int zt = 0, w = 1;
		cin >> n;
		for (int i = 1; i <= n; i++)
		cin >> a[i];
		for (int i = n; i > 1; i--)
		if (a[i] > a[i - 1])
		{
			zt = 1;
			break;
		}
		else
		if (a[i] < a[i - 1])
		{
			zt = 2;
			break;
		}
		if (zt == 0)
		{
			printf("0\n");
		}
		else
		{
			if (n > 1)
			for (int i = n - 1; i > 1; i--)
			{
				if (a[i - 1] <= a[i] && a[i] > a[i + 1])zt--;
				if (a[i - 1] > a[i] && a[i] <= a[i + 1])zt--;
				if (zt == 0)
				{
					w = i;
					break;
				}
			}
			printf("%d\n", w - 1);
		}
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <bitset>

int T, N;
int a[2 * 100000 + 1];

int main() {
	std::cin >> T;
	for (int t = 1; t <= T; ++t) {
		std::cin >> N;
		for (int n = 0; n < N; ++n)
			std::cin >> a[n];
		int lastDescent = 0;
		int out = 0;
		for (int n = 0; n < (N-1); ++n) {
			if (a[n] > a[n + 1])
				lastDescent = n+1;
			else if (a[n] < a[n + 1])
				out = lastDescent;
		}
		std::cout << out << std::endl;
	}
}


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <bitset>

int T, N;
int a[2 * 100000 + 1];

int main() {
	std::cin >> T;
	for (int t = 1; t <= T; ++t) {
		std::cin >> N;
		for (int n = 0; n < N; ++n)
			std::cin >> a[n];
		int lastDescent = 0;
		int out = 0;
		for (int n = 0; n < (N-1); ++n) {
			if (a[n] > a[n + 1])
				lastDescent = n+1;
			else if (a[n] < a[n + 1])
				out = lastDescent;
		}
		std::cout << out;
	}
}


#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <unordered_set>
#define ll long long
#define ld long double
#define inf 2147483647
using namespace std;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
int main()
{
    fast();
    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        vector<ll>v(n);
        bool f = 0;
        if (n == 1 || n == 2)
        {
            f = 1;
        }
        for (ll i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        if (f == 1)
        {
            cout << 0 << endl;
            continue;
        }
        ll j;
        for ( j = n - 2; j > 0; j--)
        {
            if (v[j] < v[j + 1] && v[j] < v[j - 1])
                break;
        }
        cout << j <<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <unordered_set>
#define ll long long
#define ld long double
#define inf 2147483647
using namespace std;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
int main()
{
    fast();
    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        vector<ll>v(n);
        bool f = 0;
        if (n == 1 || n == 2)
        {
            f = 1;
        }
        for (ll i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        if (f == 1)
        {
            cout << 0 << endl;
            continue;
        }
        ll j;
        bool u=0;
        for ( j = n - 2; j > 0; j--)
        {
            bool z=0;
            if (v[j] < v[j + 1] && v[j] < v[j - 1])
                break;
            if(v[j+1]>v[j] && v[j-1]==v[j])
            {
                  j--;
                  while(j>0)
                  {
                        if(v[j]<v[j-1])
                        {
                              u=1;
                              break;
                        }
                        else if(v[j]>v[j-1])
                        {
                              break;
                        }
                        else
                        {

                              j--;
                        }
                  }
            }
            if(u) break;
            if(j==0) break;
        }
        cout << j <<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <unordered_set>
#define ll long long
#define ld long double
#define inf 2147483647
using namespace std;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
int main()
{
    fast();
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int>v(n);
        for (int i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        int i;
        for ( i = n - 2; i > 0; i--)
        {
            if (v[i] < v[i + 1] && v[i] < v[i - 1])
                break;
        }
        cout << i<<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <unordered_set>
#define ll long long
#define ld long double
#define inf 2147483647
using namespace std;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
int main()
{
    fast();
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int>v(n);
        if (n == 1 || n == 2)
        {
            cout << 0 << endl;
            continue;
        }
        for (int i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        int j;
        for ( j = n - 2; j > 0; j--)
        {
            if (v[j] < v[j + 1] && v[j] < v[j - 1])
                break;
        }
        cout << j <<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <unordered_set>
#define ll long long
#define ld long double
#define inf 2147483647
using namespace std;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
int main()
{
    fast();
    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        vector<ll>v(n);
        bool f = 0;
        if (n == 1 || n == 2)
        {
            f = 1;
        }
        for (ll i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        if (f == 1)
        {
            cout << 0 << endl;
            continue;
        }
        ll j;
        bool u=0;
        for ( j = n - 2; j > 0; j--)
        {
            bool z=0;
            if (v[j] < v[j + 1] && v[j] < v[j - 1])
                break;
            if(v[j+1]>v[j] && v[j-1]==v[j])
            {
                  j--;
                  while(j>0)
                  {
                        if(v[j]<v[j-1])
                        {
                              u=1;
                              break;
                        }
                        else if(v[j]>v[j-1])
                        {
                              break;
                        }
                        else j--;
                  }
            }
            if(u) break;
        }
        cout << j <<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <unordered_set>
#define ll long long
#define ld long double
#define inf 2147483647
using namespace std;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
int main()
{
    fast();
    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        vector<ll>v(n);
        if (n == 1 || n == 2)
        {
            cout << 0 << endl;
            continue;
        }
        for (ll i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        ll j;
        for ( j = n - 2; j > 0; j--)
        {
            if (v[j] < v[j + 1] && v[j] < v[j - 1])
                break;
        }
        cout << j <<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <unordered_set>
#define ll long long
#define ld long double
#define inf 2147483647
using namespace std;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
int main()
{
    fast();
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int>v(n);
        for (int i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        int j;
        for ( j = n - 2; j > 0; j--)
        {
            if (v[j] < v[j + 1] && v[j] < v[j - 1])
                break;
        }
        cout << j <<endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <unordered_set>
#define ll long long
#define ld long double
#define inf 2147483647
using namespace std;
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
int main()
{
    fast();
    int t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        vector<ll>v(n);
        bool f = 0;
        if (n == 1 || n == 2)
        {
            f = 1;
        }
        for (ll i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        if (f == 1)
        {
            cout << 0 << endl;
            continue;
        }
        ll j;
        bool u=0;
        for ( j = n - 2; j > 0; j--)
        {
            bool z=0;
            if (v[j] < v[j + 1] && v[j] < v[j - 1])
                break;
            if(v[j+1]>v[j] && v[j-1]==v[j])
            {
                  j--;
                  while(j>0)
                  {
                        if(v[j]<v[j-1])
                        {
                              u=1;
                              break;
                        }
                  }
            }
            if(u) break;
        }
        cout << j <<endl;
    }
    return 0;
}


#include <iostream>
#include<string>
#include <cstring>
#include<vector>
#include<set>
#include<list>
#include<map>
#include<algorithm>
#include<cmath>
#include <numeric>
#include <fstream>
#include<iomanip>

#define DEBUG true
#define deb(x)   \
    if (DEBUG) \
        cout << #x << " " << x << '\n';
#define debarr(arrayName, n)               \
    if (DEBUG)                           \
    {                                    \
        cout << #arrayName << " ";       \
        for (ll i = 0; i < n; i++)       \
            cout << arrayName[i] << " "; \
        cout << '\n';                    \
    }
#define debv(vec)               \
    if (DEBUG)                \
    {                         \
        cout << #vec << " ";  \
        for (auto x : vec)    \
            cout << x << " "; \
        cout << '\n';         \
    }

using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

const double pi = (double)2 * acos(0.0);
#define rep(i,n) for(auto i=0;i<n;i++)
#define fi first
#define se second

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        ll* a = new ll[ n];
        rep(i, n) {
            cin >> a[i];
        }
        int mx = 0;
        for (int i = 1; i < n - 1; i++) {
            if (a[i] < a[i - 1] && a[i] < a[i + 1]) {
                mx = max(mx, i);
            }
        }
        cout << mx << endl;
    }
}


#include <iostream>
#include<string>
#include <cstring>
#include<vector>
#include<set>
#include<list>
#include<map>
#include<algorithm>
#include<cmath>
#include <numeric>
#include <fstream>
#include<iomanip>

#define DEBUG true
#define deb(x)   \
    if (DEBUG) \
        cout << #x << " " << x << '\n';
#define debarr(arrayName, n)               \
    if (DEBUG)                           \
    {                                    \
        cout << #arrayName << " ";       \
        for (ll i = 0; i < n; i++)       \
            cout << arrayName[i] << " "; \
        cout << '\n';                    \
    }
#define debv(vec)               \
    if (DEBUG)                \
    {                         \
        cout << #vec << " ";  \
        for (auto x : vec)    \
            cout << x << " "; \
        cout << '\n';         \
    }

using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

const double pi = (double)2 * acos(0.0);
#define rep(i,n) for(auto i=0;i<n;i++)
#define fi first
#define se second

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        ll* a = new ll[ n];
        rep(i, n) {
            cin >> a[i];
        }
        ll mx = 0;
        int flag = 0;
        ll index = 0;
        ll mi = 10000000;
        for (int i = 1; i < n; i++) {
            if (flag == 0) {
                if (a[i] < a[i - 1]) {
                    flag = 1;
                    index = i;
                    mi = a[i];
                }
            }
            else {
                if (a[i] > mi) {
                    mx = max(mx, index);
                    flag = 0;
                }else{
                    if (a[i] < mi) {
                        mi = a[i];
                        index = i;
                    }
                }
            }
        }
        cout << mx << endl;
    }
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<string>
#include<cstdlib>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
#include<algorithm>
#define Inf 0x3f3f3f3f3f3f3f
typedef long long ll;
using namespace std;
const int N=2e5+10;
int t;
int n;
int a[N];

int main()
{
	scanf("%d",&t);
	a[0]=N;
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		bool flag=1;
		int ans=0;
		if(a[n]>a[n-1])
			flag=0;

		for(int i=n;i>=1;i--)
		{
			if(flag==1)
			{
				if(a[i-1]<a[i])
				{
					flag=0;
				}
			}
			else
			{
				if(a[i-1]>a[i])
				{
					ans=i-1;
					break;
				}
			}
		}
		printf("%d\n",ans);
	}

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<string>
#include<cstdlib>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
#include<algorithm>
#define Inf 0x3f3f3f3f3f3f3f
typedef long long ll;
using namespace std;
const int N=2e5+10;
int t;
int n;
char s[N];
int mp[27];
int k=1;
int ans=0;
int solve(int l,int r,char c)
{
	if(l==r)
	{
		if(s[l]==c)
			return 0;
		else
			return 1;
	}
	int mid=(l+r)/2;//1-mid  mid+1-r
	int a=0,b=0;
	for(int i=l;i<=mid;i++)
		if(s[i]!=c)
			a++;
	for(int i=mid+1;i<=r;i++)
		if(s[i]!=c)
			b++;
	a+=solve(mid+1,r,c+1);
	b+=solve(l,mid,c+1);
	return min(a,b);

}
int main()
{
	scanf("%d",&t);

	while(t--)
	{
		scanf("%d",&n);
		memset(mp,0,sizeof(mp));
		int maxx=-Inf;
		for(int i=1;i<=n;i++)
		{
			scanf(" %c",&s[i]);
			mp[s[i]-'a']++;
		}
		int kk=1;
		for(k=1;kk<n;k++)
		{
			kk*=2;
			if(kk==n)
			break;
		}
		ans=n-mp[0];
		ans=min(ans,solve(1,n,'a'));
		cout<<ans<<endl;
	}

	return 0;
}


#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2")
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
#include<unordered_map>
#include<assert.h>
#include<time.h>
#include<stdlib.h>
#include<random>
#include<queue>
using namespace std;
using lld = long long int;
using pll = pair<lld, lld>;
using lf = double;
using pff = pair<lf, lf>;
using pii = pair<int, int>;
using pli = pair<lld, int>;
using pil = pair<int, lld>;
#define INF 1000000009
int n;
int a[200009];
int main() {
	int i, j, k, l;
	int t = 1, tv = 0;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (i = 0; i < n; i++) {
			scanf("%d", &a[i]);
		}
		int l = 0;
		int r = n;
		int last = -1;
		while (l < r) {
			//printf("%d %d %d\n", l, r, last);
			if (last <= a[l] && last <= a[r - 1]) {
				if (a[l] <= a[r - 1]) {
					last = a[l];
					l++;
				}
				else {
					last = a[r - 1];
					r--;
				}
			}
			else if (last <= a[l]) {
					last = a[l];
					l++;
			}
			else if (last <= a[r - 1]) {
				last = a[r - 1];
				r--;
			}
			else {
				break;
			}
		}
		printf("%d\n", r - l);
	}
	return 0;
}


#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2")
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
#include<unordered_map>
#include<assert.h>
#include<time.h>
#include<stdlib.h>
#include<random>
#include<queue>
using namespace std;
using lld = long long int;
using pll = pair<lld, lld>;
using lf = double;
using pff = pair<lf, lf>;
using pii = pair<int, int>;
using pli = pair<lld, int>;
using pil = pair<int, lld>;
#define INF 1000000009
int n;
int a[200009];
int main() {
	int i, j, k, l;
	int t = 1, tv = 0;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (i = 0; i < n; i++) {
			scanf("%d", &a[i]);
		}
		int state = 0;
		int last = -1;
		for (i = n - 1; i >= 0; i--) {
			if (state == 0) {
				if (a[i] < last) {
					state = 1;
				}
				else{
					last = a[i];
				}
			}
			if (state == 1) {
				if (a[i] > last) {
					state = 2;
					break;
				}
				else {
					last = a[i];
				}
			}
		}
		printf("%d\n", i + 1);
	}
	return 0;
}


#include <iostream>
using namespace std;

void main() {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		int* a = new int[n];
		for (int i = 0; i < n; i++) cin >> a[i];
		int pos = n - 1;
		while (pos > 0 && a[pos - 1] >= a[pos]) --pos;
		while (pos > 0 && a[pos - 1] <= a[pos]) --pos;
		cout << pos << endl;
	}
}


#include <iostream>
#include <vector>

using namespace std;

bool is_good(const vector<int>& dat, int from, int end, int now)
{
	if (end - from == 0)
	{
		return dat[from] >= now;
	}
	else
	{
		bool ans = false;
		if (dat[from] >= now)
			ans |= is_good(dat, from + 1, end, dat[from]);
		if (dat[end] >= now)
			ans |= is_good(dat, from, end - 1, dat[end]);
		return ans;
	}
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<int> dat(n, 0);
		for (int i = 0; i < n; i++)
			cin >> dat[i];

		bool has = false;
		int ans = 0;
		if (n >= 3)
		{
			if (dat[n - 2] < dat[n - 1])
				has = true;
			for (int i = n - 3; i >= 0; i--)
			{
				if (!has)
				{
					if (dat[i] == dat[i + 1] || (dat[i] - dat[i + 1] > 0))
					{
						continue;
					}
					else
					{
						has = true;
					}
				}
				else if (dat[i] - dat[i + 1] > 0)
				{
					ans = i + 1;
					break;
				}
			}
			cout << ans << endl;
		}
		else
		{
			cout << n << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

bool is_good(const vector<int>& dat, int from, int end, int now)
{
	if (end - from == 0)
	{
		return dat[from] >= now;
	}
	else
	{
		bool ans = false;
		if (dat[from] >= now)
			ans |= is_good(dat, from + 1, end, dat[from]);
		if (dat[end] >= now)
			ans |= is_good(dat, from, end - 1, dat[end]);
		return ans;
	}
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<int> dat(n, 0);
		for (int i = 0; i < n; i++)
			cin >> dat[i];

		bool has = false;
		int ans = 0;
		if (n >= 3)
		{
			for (int i = n - 2; i >= 0; i--)
			{
				if (!has)
				{
					if (dat[i] - dat[i + 1] < 0)
					{
						has = true;
					}
				}
				else if (dat[i] - dat[i + 1] > 0)
				{
					ans = i + 1;
					break;
				}
			}
			cout << ans << endl;
		}
		else
		{
			cout << 0 << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

bool is_good(const vector<int>& dat, int from, int end, int now)
{
	if (end - from == 0)
	{
		return dat[from] >= now;
	}
	else
	{
		bool ans = false;
		if (dat[from] >= now)
			ans |= is_good(dat, from + 1, end, dat[from]);
		if (dat[end] >= now)
			ans |= is_good(dat, from, end - 1, dat[end]);
		return ans;
	}
}

int find_ans(const vector<int>& dat, int l, int r)
{
	if (l == r)
		return l;
	else
	{
		int mid = (l + r) / 2;
		if (is_good(dat, mid + 1, dat.size() - 1, dat[mid]) || is_good(dat, mid, dat.size() - 2, dat[dat.size() - 1]))
		{
			return find_ans(dat, l, mid);
		}
		else
		{
			return find_ans(dat, mid + 1, r);
		}
	}
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<int> dat(n, 0);
		for (int i = 0; i < n; i++)
			cin >> dat[i];

		cout << find_ans(dat, 0, n - 2) << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <math.h>

#include <iostream>
#include <algorithm>

using namespace std;

#define MAX_ARR 1000000
#define lld long long
long long N, M;

int arr[200000] = { 0, };
int main() {

	int nTestCase = 0;
	cin >> nTestCase;
	for (int iTestCas = 0; iTestCas < nTestCase; iTestCas++)
	{
		cin >> N;

		for (int i = 1; i <= N; i++)
			cin >> arr[i];

		int front = 1;
		int back = N;
		int cursor = -1;
		int answer = N;
		while (1) {
			if (!(arr[front] >= cursor || arr[back] >= cursor))
				break;

			if (arr[front] > arr[back] ) {
				if (arr[back] >= cursor) {
					cursor = arr[back];
					back--;
				}
				else {
					cursor = arr[front];
					front++;
				}
			}
			else {
				if (arr[front] >= cursor) {
					cursor = arr[front];
					front++;
				}
				else {
					cursor = arr[back];
					back--;
				}
			}
			answer--;

			if (answer == 0) break;

		}

		cout << answer << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <math.h>

#include <iostream>
#include <algorithm>

using namespace std;

#define MAX_ARR 1000000
#define lld long long
long long N, M;

int arr[200000] = { 0, };
int main() {

	int nTestCase = 0;
	cin >> nTestCase;
	for (int iTestCas = 0; iTestCas < nTestCase; iTestCas++)
	{
		cin >> N;

		for (int i = 1; i <= N; i++)
			cin >> arr[i];

		int cursor = arr[N];
		int dir = 0;
		int answer = 0;
		for (int i = N-1; i > 0; i--) {

			if (dir == 0) {
				if (arr[i] >= cursor) {
					cursor = arr[i];
				}
				else {
					cursor = arr[i];
					dir = 1;
				}
			}
			else if (dir == 1) {
				if (arr[i] <= cursor) {
					cursor = arr[i];
				}
				else {
					answer = i;
					break;
				}
			}
		}
		cout << answer << endl;

	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <math.h>

#include <iostream>
#include <algorithm>

using namespace std;

#define MAX_ARR 1000000
#define lld long long
long long N, M;

int arr[200000] = { 0, };
int main() {

	int nTestCase = 0;
	cin >> nTestCase;
	for (int iTestCas = 0; iTestCas < nTestCase; iTestCas++)
	{
		cin >> N;

		for (int i = 1; i <= N; i++)
			cin >> arr[i];

		int front = 1;
		int back = N;
		int cursor = -1;
		int answer = N;
		while (1) {
			if ((arr[front] >= cursor && arr[back] >= cursor)) {
				if (arr[front] <= arr[back]) {
					cursor = arr[front];
					front++;
				}
				else {
					cursor = arr[back];
					back--;
				}
			}
			else if (arr[front] >= cursor) {
				cursor = arr[front];
				front++;
			}
			else if (arr[back] >= cursor) {
				cursor = arr[back];
				back--;
			}
			else {
				break;
			}
			answer--;

			if (answer == 0) break;

		}

		cout << answer << endl;
	}
	return 0;
}