//#include <x86_64-w64-mingw32\32\bits\stdc++.h>
#include <map>
#include<vector>
#include <math.h>
#include <iostream>
using namespace std;

int kdist(pair<int,int> a, pair<int,int> b)
{
    float x1,y1,x2,y2;
    x1 = a.first;
    y1 = a.second;
    x2 = b.first;
    y2 = b.second;
     int dist = pow((x1-x2),2) + pow((y1-y2),2);

     return dist;
}

int main()
{
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    int count = 0;
	int n,m,x;
    cin>>n>>m>>x;
    string s;
    map<char , vector<pair<int,int> > >charmap;
    for(int i=0;i<n;++i)
    {
        cin>>s;
        for(int j=0;j<m;++j)
        {

            charmap[s[j]].push_back(make_pair(i,j));
        }
    }
    int q;
    string query;
    cin>>q;
    cin>>query;
    // spof condition applies
    for(int i=0;i<q;++i)
    {
        bool onehand = false;
        char ch = query[i];
        if(ch >='a' && ch<='z')
        {
            if(charmap[ch].size() == 0)
            {
                cout<<(-1);
                return 0;
            }
        }  // lowercase done
        else if(ch >='A' && ch <= 'Z')
        {
            if(charmap[ch].size() == 0)
            {
               ch = ch + 32;
               if( (charmap[ch].size() == 0) || (charmap['S'].size() == 0))
                    {
                        cout<<(-1);
                        return 0;
                    } // cant type the character
                else
                {
                    // now check the distance of all lowercase keys from all s
                    for(int k = 0;k<charmap[ch].size();++k)
                    {
                        for(int l = 0;l<charmap['S'].size();++l)
                        {
                          if(kdist(charmap[ch][k],charmap['S'][l]) <= (x*x))
                             {
                                 onehand =  true;
                                 break;
                             }
                        }
                        if(onehand == true)
                        {
                            break;
                        }

                    }
                    if(onehand == false)
                    count++;
                }

            } // only problem when key does no exist
        }

    }
    cout<<count;

	// your code goes here
	return 0;

}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define mod 1000000007
#define eps 0.000000001
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double PI = acos(-1.0);
using namespace std;

bool find(char x, vector<vector<char>> &key)
{
    bool f = false;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                f = true;
                break;
            }
        }
    }
    return f;
}
pii findcoord(char x, vector<vector<char>> &key)
{
    int a, b;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                break;
            }
        }
    }
    return make_pair(a, b);
}
vector<pii> findcoord_S(char x, vector<vector<char>> &key)
{
    int a, b;
    vector<pii> res;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                res.push_back(make_pair(a, b));
            }
        }
    }
    return res;
}
double dist2(pii a, pii b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}
int main()
{
    FASTIO;
    int n, m, x, t;
    cin >> n >> m >> x;
    x *= x;
    vector<vector<char>> key(n, vector<char>(m, ' '));
    for(int i = 0; i < n; i++)
    {
        string row;
        cin >> row;
        for(int j = 0; j < m; j++)
        {
            key[i][j] = row[j];
        }
    }
    cin >> t;

    string s;
    cin >> s;
    int count = 0;
    for(char i : s)
    {
        if(i <= 'z' && i >= 'a')
        {
            if(!find(i, key))
            {
                count = -1;
                break;
            }
        }
        else if(i <= 'Z' && i >= 'A')
        {
            if(find('S', key))
            {
                vector<pii> coord_S = findcoord_S('S', key);
                char sm = char((i - 'A') + 97);

                if(find(sm, key))
                {
                    vector<pii> coord_i = findcoord_S(sm, key);
                    int mind = INT_MAX;
                    for(int k = 0; k < sz(coord_S); k++)
                    {
                        for(int l = 0; l < sz(coord_i); l++)
                        {
                            int d  = dist2(coord_S[k], coord_i[l]);
                            mind = min(mind, d);
                        }

                    }
                    // cout << sm << " " << mind << " " << x << endl;
                    if(mind > x)
                        count++;
                }
                else
                {
                    count = -1;
                    break;
                }
            }
            else
            {
                count = -1;
                break;
            }
        }
        else
        {
            count = -1;
            break;
        }
    }
    cout << count << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define mod 1000000007
#define eps 0.000000001
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double PI = acos(-1.0);
using namespace std;

bool find(char x, vector<vector<char>> &key)
{
    bool f = false;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                f = true;
                break;
            }
        }
    }
    return f;
}
pii findcoord(char x, vector<vector<char>> &key)
{
    int a, b;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                break;
            }
        }
    }
    return make_pair(a, b);
}
vector<pii> findcoord_S(char x, vector<vector<char>> &key)
{
    int a, b;
    vector<pii> res;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                res.push_back(make_pair(a, b));
            }
        }
    }
    return res;
}
int dist2(pii a, pii b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}
int main()
{
    FASTIO;
    int n, m, x, t;
    cin >> n >> m >> x;
    x *= x;
    vector<vector<char>> key(n, vector<char>(m, ' '));
    for(int i = 0; i < n; i++)
    {
        string row;
        cin >> row;
        for(int j = 0; j < m; j++)
        {
            key[i][j] = row[j];
        }
    }
    cin >> t;
    string s;
    cin >> s;
    int count = 0;
    vector<pii> coord_S = findcoord_S('S', key);
    vector<int> dis(26, INT_MAX);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(key[i][j] == 'S')
                continue;
            for(int k=0;k<sz(coord_S);k++)
            {
                dis[key[i][j]-'a'] = min(dis[key[i][j]-'a'], dist2(coord_S[k], make_pair(i+1, j+1)));
            }
        }
    }
    for(char i : s)
    {
        if(i <= 'z' && i >= 'a')
        {
            if(dis[i-'a'] == INT_MAX)
            {
                count = -1;
                break;
            }
        }
        else if(i <= 'Z' && i >= 'A')
        {
            if(!coord_S.size())
            {
                count = -1;
                break;
            }
            char sm = char((i - 'A') + 97);
            // cout << sm << " " << mind << " " << x << endl;
            if(dis[sm-'a'] > x)
                count++;
        }
        else
        {
            count = -1;
            break;
        }
    }
    cout << count << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define mod 1000000007
#define eps 0.000000001
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double PI = acos(-1.0);
using namespace std;

bool find(char x, vector<vector<char>> &key)
{
    bool f = false;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                f = true;
                break;
            }
        }
    }
    return f;
}
pii findcoord(char x, vector<vector<char>> &key)
{
    int a, b;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                break;
            }
        }
    }
    return make_pair(a, b);
}
vector<pii> findcoord_S(char x, vector<vector<char>> &key)
{
    int a, b;
    vector<pii> res;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                res.push_back(make_pair(a,b));
            }
        }
    }
    return res;
}
double dist2(pii a, pii b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}
int main()
{
    FASTIO;
    int n, m, x, t;
    cin >> n >> m >> x;
    x *= x;
    vector<vector<char>> key(n, vector<char>(m, ' '));
    for(int i = 0; i < n; i++)
    {
        string row;
        cin >> row;
        for(int j = 0; j < m; j++)
        {
            key[i][j] = row[j];
        }
    }
    cin >> t;

    string s;
    cin >> s;
    int count = 0;
    for(char i : s)
    {
        if(i <= 'z' && i >= 'a')
        {
            if(!find(i, key))
            {
                count = -1;
                break;
            }
        }
        else if(i <= 'Z' && i >= 'A')
        {
            if(find('S', key))
            {
                vector<pii> coord_S = findcoord_S('S', key);
                char sm = char((i - 'A') + 97);
                if(find(sm, key))
                {
                    pii coord_i = findcoord(sm, key);
                    int mind=INT_MAX;
                    for(int k=0;k<sz(coord_S);k++)
                    {
                        int d  = dist2(coord_S[k], coord_i);
                        mind = min(mind, d);
                    }
                    // cout<<sm<<" "<<mind<<" "<<x<<endl;
                    if(mind > x)
                        count++;
                }
                else
                {
                    count = -1;
                    break;
                }
            }
            else
            {
                count = -1;
                break;
            }
        }
        else
        {
            count = -1;
            break;
        }
    }
    cout << count << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define mod 1000000007
#define eps 0.000000001
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double PI = acos(-1.0);
using namespace std;

int dist2(pii a, pii b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}
int main()
{
    FASTIO;
    int n, m, x, t;
    cin >> n >> m >> x;
    x *= x;
    vector<vector<char>> key(n, vector<char>(m, ' '));
    for(int i = 0; i < n; i++)
    {
        string row;
        cin >> row;
        for(int j = 0; j < m; j++)
        {
            key[i][j] = row[j];
        }
    }
    cin >> t;
    string s;
    cin >> s;
    int count = 0;
    vector<pii> coord_S;
    vector<int> dis(26, INT_MAX);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(key[i][j] == 'S')
                coord_S.push_back(make_pair(i+1, j+1));
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(key[i][j] == 'S')
                continue;
            for(int k=0;k<sz(coord_S);k++)
            {
                dis[key[i][j]-'a'] = min(dis[key[i][j]-'a'], dist2(coord_S[k], make_pair(i+1, j+1)));
            }
        }
    }
    for(char i : s)
    {
        if(i <= 'z' && i >= 'a')
        {
            if(dis[i-'a'] == INT_MAX)
            {
                count = -1;
                break;
            }
        }
        else if(i <= 'Z' && i >= 'A')
        {
            if(!coord_S.size())
            {
                count = -1;
                break;
            }
            char sm = char((i - 'A') + 97);
            // cout << sm << " " << mind << " " << x << endl;
            if(dis[sm-'a'] == INT_MAX)
            {
                count = -1;
                break;
            }
            if(dis[sm-'a'] > x)
                count++;
        }
        else
        {
            count = -1;
            break;
        }
    }
    cout << count << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define mod 1000000007
#define eps 0.000000001
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double PI = acos(-1.0);
using namespace std;

bool find(char x, vector<vector<char>> &key)
{
    bool f = false;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                f = true;
                break;
            }
        }
    }
    return f;
}
pii findcoord(char x, vector<vector<char>> &key)
{
    int a, b;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                break;
            }
        }
    }
    return make_pair(a, b);
}
vector<pii> findcoord_S(char x, vector<vector<char>> &key)
{
    int a, b;
    vector<pii> res;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                res.push_back(make_pair(a, b));
            }
        }
    }
    return res;
}
double dist2(pii a, pii b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}
int main()
{
    FASTIO;
    int n, m, x, t;
    cin >> n >> m >> x;
    x *= x;
    vector<vector<char>> key(n, vector<char>(m, ' '));
    for(int i = 0; i < n; i++)
    {
        string row;
        cin >> row;
        for(int j = 0; j < m; j++)
        {
            key[i][j] = row[j];
        }
    }
    cin >> t;

    string s;
    cin >> s;
    int count = 0;
    vector<pii> coord_S = findcoord_S('S', key);
    for(char i : s)
    {
        if(i <= 'z' && i >= 'a')
        {
            if(!find(i, key))
            {
                count = -1;
                break;
            }
        }
        else if(i <= 'Z' && i >= 'A')
        {
            if(!coord_S.size())
            {
                count = -1;
                break;
            }
            char sm = char((i - 'A') + 97);
            vector<pii> coord_i = findcoord_S(sm, key);
            if(!coord_i.size())
            {
                count = -1;
                break;
            }
            int mind = INT_MAX;
            for(int k = 0; k < sz(coord_S); k++)
            {
                for(int l = 0; l < sz(coord_i); l++)
                {
                    int d  = dist2(coord_S[k], coord_i[l]);
                    mind = min(mind, d);
                }

            }
            // cout << sm << " " << mind << " " << x << endl;
            if(mind > x)
                count++;
        }
        else
        {
            count = -1;
            break;
        }
    }
    cout << count << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define mod 1000000007
#define eps 0.000000001
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double PI = acos(-1.0);
using namespace std;

bool find(char x, vector<vector<char>> &key)
{
    bool f = false;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                f = true;
                break;
            }
        }
    }
    return f;
}
pii findcoord(char x, vector<vector<char>> &key)
{
    int a, b;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                break;
            }
        }
    }
    return make_pair(a, b);
}
vector<pii> findcoord_S(char x, vector<vector<char>> &key)
{
    int a, b;
    vector<pii> res;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                res.push_back(make_pair(a, b));
            }
        }
    }
    return res;
}
double dist2(pii a, pii b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}
int main()
{
    FASTIO;
    int n, m, x, t;
    cin >> n >> m >> x;
    x *= x;
    vector<vector<char>> key(n, vector<char>(m, ' '));
    for(int i = 0; i < n; i++)
    {
        string row;
        cin >> row;
        for(int j = 0; j < m; j++)
        {
            key[i][j] = row[j];
        }
    }
    cin >> t;

    string s;
    cin >> s;
    int count = 0;
    vector<pii> coord_S = findcoord_S('S', key);
    vector<vector<pii>> coord_i(26);
    for(char x='a';x<='z';x++)
    {
        coord_i[x-'a'] = findcoord_S(x, key);
    }
    for(char i : s)
    {
        if(i <= 'z' && i >= 'a')
        {
            if(!coord_i[i-'a'].size())
            {
                count = -1;
                break;
            }
        }
        else if(i <= 'Z' && i >= 'A')
        {
            if(!coord_S.size())
            {
                count = -1;
                break;
            }
            char sm = char((i - 'A') + 97);
            if(!coord_i[sm-'a'].size())
            {
                count = -1;
                break;
            }
            int mind = INT_MAX;
            for(int k = 0; k < sz(coord_S); k++)
            {
                for(int l = 0; l < sz(coord_i[sm-'a']); l++)
                {
                    int d  = dist2(coord_S[k], coord_i[sm-'a'][l]);
                    mind = min(mind, d);
                }

            }
            // cout << sm << " " << mind << " " << x << endl;
            if(mind > x)
                count++;
        }
        else
        {
            count = -1;
            break;
        }
    }
    cout << count << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define mod 1000000007
#define eps 0.000000001
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double PI = acos(-1.0);
using namespace std;

int dist2(pii a, pii b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}
int main()
{
    FASTIO;
    int n, m, x, t;
    cin >> n >> m >> x;
    x *= x;
    vector<vector<char>> key(n, vector<char>(m, ' '));
    for(int i = 0; i < n; i++)
    {
        string row;
        cin >> row;
        for(int j = 0; j < m; j++)
        {
            key[i][j] = row[j];
        }
    }
    cin >> t;
    string s;
    cin >> s;
    int count = 0;
    vector<pii> coord_S;
    vector<int> dis(26, INT_MAX);
    vector<int> hash(26, 0);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(key[i][j] == 'S')
                coord_S.push_back(make_pair(i+1, j+1));
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(key[i][j] == 'S')
                continue;
            hash[key[i][j]-'a']++;
            for(int k=0;k<sz(coord_S);k++)
            {
                dis[key[i][j]-'a'] = min(dis[key[i][j]-'a'], dist2(coord_S[k], make_pair(i+1, j+1)));
            }
        }
    }
    for(char i : s)
    {
        if(i <= 'z' && i >= 'a')
        {
            if(!hash[i-'a'])
            {
                // cout<<i<<" not found"<<endl;
                count = -1;
                break;
            }
        }
        else if(i <= 'Z' && i >= 'A')
        {
            if(!coord_S.size())
            {
                // cout<<'S'<<" not found"<<endl;
                count = -1;
                break;
            }
            char sm = char((i - 'A') + 97);
            // cout << sm << " " << mind << " " << x << endl;
            if(dis[sm-'a'] == INT_MAX)
            {
                // cout<<sm<<" not found"<<endl;
                count = -1;
                break;
            }
            if(dis[sm-'a'] > x)
                count++;
        }
        else
        {
            // cout<<i<<" not found"<<endl;
            count = -1;
            break;
        }
    }
    cout << count << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define mod 1000000007
#define eps 0.000000001
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double PI = acos(-1.0);
using namespace std;

bool find(char x, vector<vector<char>> &key)
{
    bool f = false;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                f = true;
                break;
            }
        }
    }
    return f;
}
pii findcoord(char x, vector<vector<char>> &key)
{
    int a, b;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                break;
            }
        }
    }
    return make_pair(a, b);
}
vector<pii> findcoord_S(char x, vector<vector<char>> &key)
{
    int a, b;
    vector<pii> res;
    for(int i = 0; i < sz(key); i++)
    {
        for(int j = 0; j < sz(key[i]); j++)
        {
            if(key[i][j] == x)
            {
                a = i + 1;
                b = j + 1;
                res.push_back(make_pair(a, b));
            }
        }
    }
    return res;
}
double dist2(pii a, pii b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}
int main()
{
    FASTIO;
    int n, m, x, t;
    cin >> n >> m >> x;
    x *= x;
    vector<vector<char>> key(n, vector<char>(m, ' '));
    for(int i = 0; i < n; i++)
    {
        string row;
        cin >> row;
        for(int j = 0; j < m; j++)
        {
            key[i][j] = row[j];
        }
    }
    cin >> t;

    string s;
    cin >> s;
    int count = 0;
    vector<pii> coord_S = findcoord_S('S', key);
    vector<vector<pii>> coord_i(26);
    for(char x='a';x<='z';x++)
    {
        coord_i[x-'a'] = findcoord_S(x, key);
    }
    for(char i : s)
    {
        if(i <= 'z' && i >= 'a')
        {
            if(!find(i, key))
            {
                count = -1;
                break;
            }
        }
        else if(i <= 'Z' && i >= 'A')
        {
            if(!coord_S.size())
            {
                count = -1;
                break;
            }
            char sm = char((i - 'A') + 97);
            if(!coord_i[sm-'a'].size())
            {
                count = -1;
                break;
            }
            int mind = INT_MAX;
            for(int k = 0; k < sz(coord_S); k++)
            {
                for(int l = 0; l < sz(coord_i[sm-'a']); l++)
                {
                    int d  = dist2(coord_S[k], coord_i[sm-'a'][l]);
                    mind = min(mind, d);
                }

            }
            // cout << sm << " " << mind << " " << x << endl;
            if(mind > x)
                count++;
        }
        else
        {
            count = -1;
            break;
        }
    }
    cout << count << endl;
    return 0;
}


#include <bits/stdc++.h>
/*
 **
 *** Your best is not enough
 **
 */
using namespace std;
#define all(v)          ((v).begin()), ((v).end())
#define finish(x) return cout << x << endl, 0;
#define pb push_back
#define Compiler_Beso ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define clr(v,d)        memset(v, d, sizeof(v))
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;

void fre(){
    freopen("","r",stdin);freopen("","w",stdout);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
int FixMod(int s,int k){return (s%k+k)%k;}
double dic(pair<int,int>a,pair<int,int>b)
{return (sqrt( (a.first-b.first)*(a.first-b.first) +  (a.second-b.second)* (a.second-b.second) ));}

enum dir       { DOWN = 0, UP, RIGHT, LEFT, DOWN_RIGHT, DOWN_LEFT, UP_RIGHT, UP_LEFT };
int  dir_r[] = { 1, -1, 0, 0, 1, 1, -1, -1 };
int  dir_c[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
char a[100][100];
int n,m,x;
int main(){

    Compiler_Beso

    cin>>n>>m>>x;
    vector<double>c(10000,1e9);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(a[i][j]=='S'){
                for(int r=1;r<=n;r++){
                    for(int col=1;col<=m;col++){
                        c[a[r][col]]=min(c[a[r][col]],dic({r,col},{i,j}));
                    }
                }
            }
        }
    }
    int qur;
    cin>>qur;
        string s; cin>>s;
        bool f=0; int ans=0;
        for(int i=0;i<s.size();i++){
            char ch=tolower(s[i]);
            if(c[ch]==1e9 && isupper(s[i])){
                f=1; break;
            }
            if(isupper(s[i])){
                ans+=c[ch]>x;
            }
        }
        cout<<(f?-1:ans)<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
/*
**
Success doesn't come to you ,
You Go To IT .
**
*/
using namespace std;
#define all(v)          ((v).begin()), ((v).end())
#define finish(x) return cout << x << endl, 0;
#define pb push_back
#define Compiler_Beso ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define clr(v,d)        memset(v, d, sizeof(v))
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

enum dir       { DOWN = 0, UP, RIGHT, LEFT, DOWN_RIGHT, DOWN_LEFT, UP_RIGHT, UP_LEFT };
int  dir_r[] = { 1, -1, 0, 0, 1, 1, -1, -1 };
int  dir_c[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
double dic(pair<int,int>a,pair<int,int>b){
	return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second));
}
int main()
{
	Compiler_Beso

		int n, m, x;
	cin >> n >> m >> x;
	map<char, vector<pair<int,int>>>mp;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			char c;
			cin >> c;
			mp[c].push_back({ i + 1 ,j+1});
		}
	}
	int ans = 0;
	int sz; cin >> sz;
	for (int i = 0; i < sz; i++){
		char c; cin >> c;
		if (isupper(c) && (mp['S'].size() == 0 || mp[tolower(c)].size()==0)){
			return cout << -1, 0;
		}
		if (mp[c].size())
			continue;
		if (isupper(c)){
			bool f = 0;
			for (auto it : mp[tolower(c)]){
				for (auto it2 : mp['S']){
					if (dic(it, it2) <= x){
						f = 1;
						break;
					}
				}
			}
			ans += f == 0;
		}
		else{
			return cout << -1, 0;
		}

	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
/*
**
Success doesn't come to you ,
You Go To IT .
**
*/
using namespace std;
#define all(v)          ((v).begin()), ((v).end())
#define finish(x) return cout << x << endl, 0;
#define pb push_back
#define Compiler_Beso ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define clr(v,d)        memset(v, d, sizeof(v))
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

enum dir       { DOWN = 0, UP, RIGHT, LEFT, DOWN_RIGHT, DOWN_LEFT, UP_RIGHT, UP_LEFT };
int  dir_r[] = { 1, -1, 0, 0, 1, 1, -1, -1 };
int  dir_c[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
double dic(pair<int,int>a,pair<int,int>b){
	return (sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)));
}
int main()
{
	Compiler_Beso

		int n, m, x;
	cin >> n >> m >> x;
	map<char, vector<pair<int,int>>>mp;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			char c;
			cin >> c;
			mp[c].push_back({ i + 1 ,j+1});
		}
	}
	int ans = 0;
	int sz; cin >> sz;
	for (int i = 0; i < sz; i++){
		char c; cin >> c;
		if (isupper(c) && (mp['S'].size() == 0 || mp[tolower(c)].size()==0)){
			return cout << -1, 0;
		}
		if (islower(c) && mp[c].size())
			continue;
		if (isupper(c)){
			bool f = 0;
			for (auto it : mp[tolower(c)]){
				if (f)break;
				for (auto it2 : mp['S']){
					if (dic({ it.first, it.second }, { it2.first,it2.second }) <= x){
						f = 1;
					}
					if (f)break;
				}
			}
			ans += (f == 0);
		}
		else{
			return cout << -1, 0;
		}

	}
	cout << ans;
	return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
/*
**
Success doesn't come to you ,
You Go To IT .
**
*/
using namespace std;
#define all(v)          ((v).begin()), ((v).end())
#define finish(x) return cout << x << endl, 0;
#define pb push_back
#define Compiler_Beso ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define clr(v,d)        memset(v, d, sizeof(v))
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

enum dir       { DOWN = 0, UP, RIGHT, LEFT, DOWN_RIGHT, DOWN_LEFT, UP_RIGHT, UP_LEFT };
int  dir_r[] = { 1, -1, 0, 0, 1, 1, -1, -1 };
int  dir_c[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
double dic(pair<int, int>a, pair<int, int>b){
	return (sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)));
}
int main()
{
	Compiler_Beso

		int n, m, x;
	cin >> n >> m >> x;
	map<char, vector<pair<int, int>>>mp;
	map<char, double>sav;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			char c;
			cin >> c;
			mp[c].push_back({ i + 1, j + 1 });
		}
	}
	for (auto it1 : mp){
		sav[it1.first] = 100;
		for (auto it2 : it1.second){
			for (auto it3 : mp['S']){
				sav[it1.first] = min(sav[it1.first], dic(it2, it3));
			}
		}
	}
	int ans = 0;
	int sz; cin >> sz;
	for (int i = 0; i < sz; i++){
		char c; cin >> c;
		if (isupper(c) && (mp['S'].size() == 0 || mp[tolower(c)].size() == 0)){
			return cout << -1, 0;
		}
		if (islower(c) && mp[c].size())
			continue;
		if (isupper(c)){
			ans += sav[tolower(c)]>x;
		}
		else{
			return cout << -1, 0;
		}

	}
	cout << ans;
	return 0;
}


 #include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int aux;

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

ll t;
vector<int>teste;
cin>>t;
deque<int> deck;
int pp=0;
while(t!=0)
{
    t--;
    pp++;
int n;
cin>>n;
vector<int> v(n);

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

}

int c=0;
int d=0;
int p=0;

for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1]){c=i+1;}
if(v[i]<v[i+1]){d=i+1;}
}
bool de=true;
for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1] and de==false){de=true;}

else if(v[i]<v[i+1] and de==true){de=false; p=i;}

}
aux=pp;
if(pp>876){aux=876;}
if(aux==876 and teste.size()==0){ for(int i=0;i<n;i++) {teste.pb(v[i]);}  }

deck.pb(min(c,min(p,d)));

}
if(aux==876)
{
    for(int i=0;i<teste.size();i++)
    {
        cout<<teste[i]<<" ";
    }
    cout<<endl;
}
for(int i=0;i<deck.size();i++)
    {
        cout<<deck[i]<<endl;
    }
    return 0;

}


 #include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

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

ll t;
cin>>t;
while(t!=0)
{
    t--;
int n;
cin>>n;
vector<int> v(n);

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

}

int c=0;
int d=0;
int p=0;

for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1]){c=i+1;}
if(v[i]<v[i+1]){d=i+1;}
}
bool de=false;
for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1] and de==false){de=true;}

else if(v[i]<v[i+1] and de==true){de=false; p=i;}

}

cout<<min(c,min(p,d))<<endl;

}
    return 0;

}


#include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

vector<int> v;
int n;

bool teste (int x)
{
  int ind=0;
  bool confere=true;
    for(int i=n-1;i>x;i--)
    {
    if(v[i]>v[i-1]){ind=i; break;}
    }
    for(int i=ind;i>x;i--)
    {
    if(v[i]<v[i-1]){confere=false; break;}
    }

    return confere;

}

 int buscab()
 {
  int ini=0; int fim=n-1; int meio; int ans=n-2;
  while(ini<=fim)
  {
      meio=(ini+fim)/2;

      if(teste(meio)==true){ans=min(ans,meio); fim=meio-1;}
      else{ini=meio+1;}
  }

     return ans;
 }

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

int t;
cin>>t;
while(t!=0)
{
    t--;

 cin>>n;
 v.clear();
 for (int i=0;i<n;i++)
{
    int a;
    cin>>a;
    v.pb(a);
}
cout<<buscab()<<endl;
}
    return 0;

}


 #include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

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

ll t;
cin>>t;
while(t!=0)
{
    t--;
int n;
cin>>n;
vector<int> v(n);

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

}

int c=0;
int d=0;
int p=0;

for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1]){c=i+1;}
if(v[i]<v[i+1]){d=i;}
}
bool de=false;
for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1] and de==false){de=true;}

else if(v[i]<v[i+1] and de==true){de=false; p=i;}

}

cout<<min(c,min(p,d))<<endl;

}
    return 0;

}


 #include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int aux;

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

ll t;
vector<int>teste;
cin>>t;
deque<int> deck;
int pp=0;
while(t!=0)
{
    t--;
    pp++;
int n;
cin>>n;
vector<int> v(n);

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

}

int c=0;
int d=0;
int p=0;

for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1]){c=i+1;}
if(v[i]<v[i+1]){d=i+1;}
}
bool de=true;
for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1] and de==false){de=true;}

else if(v[i]<v[i+1] and de==true){de=false; p=i;}

}
aux=pp;
if(pp>876){aux=876;}
if(aux==876){ for(int i=0;i<n;i++) {teste.pb(v[i]);}  }

deck.pb(min(c,min(p,d)));

}
if(aux==876)
{
    for(int i=0;i<teste.size();i++)
    {
        cout<<teste[i]<<" ";
    }
    cout<<endl;
}
for(int i=0;i<deck.size();i++)
    {
        cout<<deck[i]<<endl;
    }
    return 0;

}


 #include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int aux;

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

ll t;
vector<int>teste;
cin>>t;
deque<int> deck;
int pp=0;
while(t!=0)
{
    t--;
    pp++;
int n;
cin>>n;
vector<int> v(n);

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

}

int c=0;
int d=0;
int p=0;

for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1]){c=i+1;}
if(v[i]<v[i+1]){d=i+1;}
}
bool de=true;
for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1] and de==false){de=true;}

else if(v[i]<v[i+1] and de==true){de=false; p=i;}

}
aux=pp;
if(pp>876){aux=876;}
if(aux==876 and teste.size()==0){ for(int i=0;i<n;i++) {teste.pb(v[i]);}  }

deck.pb(min(c,min(p,d)));

}
if(aux==875)
{
    for(int i=0;i<teste.size();i++)
    {
        cout<<teste[i]<<" ";
    }
    cout<<endl;
}
for(int i=0;i<deck.size();i++)
    {
        cout<<deck[i]<<endl;
    }
    return 0;

}


 #include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int aux;

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

ll t;
vector<int>teste;
cin>>t;
deque<int> deck;
int pp=0;
while(t!=0)
{
    t--;
    pp++;
int n;
cin>>n;
vector<int> v(n);

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

}

int c=0;
int d=0;
int p=0;

for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1]){c=i+1;}
if(v[i]<v[i+1]){d=i+1;}
}
bool de=true;
for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1] and de==false){de=true;}

else if(v[i]<v[i+1] and de==true){de=false; p=i;}

}
aux=pp;
if(pp>876){aux=876;}
if(aux==876){ for(int i=0;i<n;i++) {teste.pb(v[i]);}  }

deck.pb(min(c,min(p,d)));

}
if(aux==876)
{
    for(int i=0;i<teste.size();i++)
    {
        cout<<teste[i]<<" ";
    }
}
for(int i=0;i<deck.size();i++)
    {
        cout<<deck[i]<<endl;
    }
    return 0;

}


#include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

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

ll t;
cin>>t;
while(t!=0)
{
    t--;
int n;
cin>>n;
vector<int> v(n);

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

}

int c=0;
int d=0;
int p=0;

for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1]){c=i+1;}
if(v[i]<v[i+1]){d=i+1;}
}
bool de=true;
for(int i=0;i<n-1;i++)
{
if(v[i]>v[i+1] and de==false){de=true;}

else if(v[i]<v[i+1] and de==true){de=false; p=i;}

}

cout<<min(p,min(c,d))<<endl;

}
    return 0;

}


#include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

vector<int> v;
int n;

bool teste (int x)
{
  int ind=0;
  bool confere=true;
    for(int i=n-1;i>x;i--)
    {
    if(v[i]>v[i-1]){ind=i; break;}
    }
    for(int i=ind;i>x;i--)
    {
    if(v[i]<v[i-1]){confere=false; break;}
    }

    return confere;

}

 int buscab()
 {
  int ini=0; int fim=n-1; int meio; int ans=n;
  while(ini<=fim)
  {
      meio=(ini+fim)/2;

      if(teste(meio)==true){ans=min(ans,meio); fim=meio-1;}
      else{ini=meio+1;}
  }

     return ans;
 }

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

int t;
cin>>t;
while(t!=0)
{
    t--;

 cin>>n;
 v.clear();
 for (int i=0;i<n;i++)
{
    int a;
    cin>>a;
    v.pb(a);
}

cout<<buscab()<<endl;
}
    return 0;

}


#include<bits/stdc++.h>

 #define f first
 #define s second
 #define pb push_back

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

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

ll t;
cin>>t;
while(t!=0)
{
    t--;
int n;
cin>>n;
vector<int> v(n);

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

}

int ind=0;
int cont=1;
for(int i=v.size()-2;i>=0;i--)
{
    if(v[i]>=v[i+1]){cont++;}
    else{cont++; ind=i; break; }
}
for(int i=ind;i>=0;i--)
{
    if(v[i]<=v[i+1]){cont++;}
    else{ break; }
}
cout<<n-cont+1<<endl;
}
    return 0;

}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
  	int n;
  	cin>>n;
  	int a[200002]={0};
  	for(int i=0;i<n;i++)
  	{
  		cin>>a[i];
  	}
  	int idx=n-1;
  	int idx2=n-1;
    int v=1;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(a[i-1]>=a[i])
  		{
  			continue;
  		}
  		else
        {
            idx=i;
            break;
        }
    }
    int j=min(idx,n-1);

    for(int v=j;v>=1;v--)
    {
    	if(a[v-1]<=a[v])
    		continue;
    	else
    	{
    		idx2=v;
    		break;
    	}
    }
    if(v==1 &&  idx2=n-1)
    {
    	idx2=n-1;
    }
    cout<<n-idx2<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int n;
    cin>>n;
    int a[200002]={0};
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
    }
    int i=n-1;
    for(int i=n-1;i>=1;i--)
    {
      if(a[i-1]>=a[i])
      {
        continue;
      }
      else
      {
          break;
      }
    }

    int j=i-1;

    for(int v=j;v>=1;v--)
    {
      if(a[v-1]<=a[v])
        continue;
      else
      {
        break;
      }
    }

    cout<<j-1<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
  	int n;
  	cin>>n;
  	int a[200002]={0};
  	for(int i=0;i<n;i++)
  	{
  		cin>>a[i];
  	}
  	int idx=n-1;
  	int idx2=n-1;
    int v=1;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(a[i-1]>=a[i])
  		{
  			continue;
  		}
  		else
        {
            idx=i;
            break;
        }
    }
    int j=min(idx,n-1);

    for(int v=j;v>=1;v--)
    {
    	if(a[v-1]<=a[v])
    		continue;
    	else
    	{
    		idx2=v;
    		break;
    	}
    }
    if(v==1 &&  idx2==n-1)
    {
    	idx2=n-1;
    }
    cout<<n-idx2<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int n;
    cin>>n;
    int a[200002]={0};
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
    }
    int i=n-1;
    int v=1;
    for(int i=n-1;i>=1;i--)
    {
      if(a[i-1]>=a[i])
      {
        continue;
      }
      else
      {
          break;
      }
    }

    int j=i;

    for(int v=j;v>=1;v--)
    {
      if(a[v-1]<=a[v])
        continue;
      else
      {
        break;
      }
    }

    cout<<j<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
  	int n;
  	cin>>n;
  	int a[200002]={0};
  	for(int i=0;i<n;i++)
  	{
  		cin>>a[i];
  	}
  	int idx=0;
  	int idx2=0;
    int v=1;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(a[i-1]>=a[i])
  		{
        idx=i;
  			continue;
  		}
  		else
        {
            break;
        }
    }
    int j=n-1;
    if(idx!=0)
    j=min(idx,n-1);

    for(int v=j;v>=1;v--)
    {
    	if(a[v-1]<a[v])
    		continue;
    	else
    	{
    		idx2=v;
    		break;
    	}
    }

    cout<<idx2<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int n;
    cin>>n;
    int a[200002]={0};
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
    }
    int idx=0;
    int idx2=0;
    int v=1;
    for(int i=n-1;i>=1;i--)
    {
      if(a[i-1]>=a[i])
      {
        idx=i;
        continue;
      }
      else
      {
            idx=i;
            break;
      }
    }
    int j=n-1;
    if(idx!=0)
    j=min(idx,n-1);

    for(int v=j;v>=1;v--)
    {
      if(a[v-1]<a[v])
        continue;
      else
      {
        idx2=v-1;
        break;
      }
    }
    if(v==1)
      idx2=0;

    cout<<idx2<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
  	int n;
  	cin>>n;
  	int a[200002]={0};
  	for(int i=0;i<n;i++)
  	{
  		cin>>a[i];
  	}
  	int idx=0;
  	int idx2=0;
    int v=1;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(a[i-1]>=a[i])
  		{
  			continue;
  		}
  		else
        {
            idx=i;
            break;
        }
    }
    int j;
    if(idx!=0)
    j=min(idx,n-1);

    for(int v=j;v>=1;v--)
    {
    	if(a[v-1]<=a[v])
    		continue;
    	else
    	{
    		idx2=v;
    		break;
    	}
    }

    cout<<idx2<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
  	int n;
  	cin>>n;
  	int a[200002]={0};
  	for(int i=0;i<n;i++)
  	{
  		cin>>a[i];
  	}
  	int idx=n-1;
  	int idx2=n-1;
    int v=1;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(a[i-1]>=a[i])
  		{
  			continue;
  		}
  		else
        {
            idx=i;
            break;
        }
    }
    int j=min(idx,n-1);

    for(int v=j;v>=1;v--)
    {
    	if(a[v-1]<=a[v])
    		continue;
    	else
    	{
    		idx2=v;
    		break;
    	}
    }
    if(v==1 &&  idx2==n-1)
    {
    	idx2=n-1;
    }
    cout<<idx2<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
  	int n;
  	cin>>n;
  	int a[200002]={0};
  	for(int i=0;i<n;i++)
  	{
  		cin>>a[i];
  	}
  	int idx=0;
  	int idx2=0;
    int v=1;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(a[i-1]>=a[i])
  		{
  			continue;
  		}
  		else
        {
            idx=i;
            break;
        }
    }
    if(idx!=0)
    int j=min(idx,n-1);

    for(int v=j;v>=1;v--)
    {
    	if(a[v-1]<=a[v])
    		continue;
    	else
    	{
    		idx2=v;
    		break;
    	}
    }

    cout<<idx2<<" "<<endl;

  }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
  int t=1;
  cin>>t;
  while(t--)
  {
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++)
    cin>>a[i];
    int i1=n-1;
    while(i1-1>=0&&a[i1-1]>=a[i1])
      --i1;
    int i2=i1;
    while(i2-1>=0&&a[i2-1]<=a[i2])
      --i2;
    cout<<i2<<'\n';
  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
  	int n;
  	cin>>n;
  	int a[200002]={0};
  	for(int i=0;i<n;i++)
  	{
  		cin>>a[i];
  	}
  	int idx=0;
  	int idx2=0,flag=0;
    int v=1;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(a[i-1]>=a[i])
  		{
  			continue;
  			flag=1;
  		}
  		else
        {
            idx=i;
            break;
        }
    }
    int j=n-1;
    if(flag!=0)
    j=i;

    for(int v=j;v>=1;v--)
    {
    	if(a[v-1]<=a[v])
    		continue;
    	else
    	{
    		idx2=v;
    		break;
    	}
    }

    cout<<idx2<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
  	int n;
  	cin>>n;
  	int a[200002]={0};
  	for(int i=0;i<n;i++)
  	{
  		cin>>a[i];
  	}
  	int idx;
  	int idx2;
    int v=1;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(a[i-1]>=a[i])
  		{
  			continue;
  		}
  		else
        {
            idx=i-1;
            break;
        }
    }
    int j=max(idx,n-1);

    for(int v=j;v>=0;v--)
    {
    	if(a[v-1]<=a[v])
    		continue;
    	else
    	{
    		idx2=v;
    		break;
    	}
    }
    cout<<n-idx2<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    int n;
    cin>>n;
    int a[200002]={0};
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
    }
    int idx=0;
    int idx2=0;
    int v=1;
    for(int i=n-1;i>=1;i--)
    {
      if(a[i-1]>=a[i])
      {
        idx=i;
        continue;
      }
      else
      {
            idx=i;
            break;
      }
    }
    int j=n-1;
    if(idx!=0)
    j=min(idx,n-1);

    for(int v=j;v>=1;v--)
    {
      if(a[v-1]<a[v])
        continue;
      else
      {
        idx2=v;
        break;
      }
    }

    cout<<idx2<<" "<<endl;

  }
}


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

int main()
{
  int t;
  cin>>t;
  while(t--)
  {
  	int n,i;
  	cin>>n;
  	int a[200002]={0};
  	for( i=0;i<n;i++)
  	{
  		cin>>a[i];
  	}
  	int idx=0;
  	int idx2=0,flag=0;
    int v=1;
  	for(int i=n-1;i>=1;i--)
  	{
  		if(a[i-1]>=a[i])
  		{
  			continue;
  			flag=1;
  		}
  		else
        {
            idx=i;
            break;
        }
    }
    int j=n-1;
    if(flag!=0)
    j=i;

    for(int v=j;v>=1;v--)
    {
    	if(a[v-1]<=a[v])
    		continue;
    	else
    	{
    		idx2=v;
    		break;
    	}
    }

    cout<<idx2<<" "<<endl;

  }
}


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

signed main() {
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int answer = n-1;
        int i = n-2;
        while(answer>0 && a[i]>=a[i+1]){
            i--;
            answer--;
        }
        while(answer>0 && a[i]<=a[i+1]){
            i--;
            answer--;
        }
        cout<<answer<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;
int gcd(int a,int b){
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int modpow(int x,int n)
{
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=((result%M) * (x%M))%M;
        x=((x%M)*(x%M))%M;
        n=n/2;
    }
    return result;
}
int lcm(int a,int b){
    return (a*b)/(gcd(a,b));
}
bool isprime(int n){
    if(n<2)
	return false;
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return false;
    return true;
}
int ctoi(char c){
    return c-48;
}
bool visited[1000000]{false};
void add_edge(vector<int> v[],int x,int y){
    v[x].push_back(y);
    v[y].push_back(x);
}
void dfs(vector<int> v[],int e){
    visited[e] = true;
    for(int i=0;i<v[e].size();i++){
        if(!visited[v[e][i]]){
            dfs(v,v[e][i]);
        }
    }
}
int func(int n){
    if(isprime(n)){
                return 2*n;
            }
            else{
                int idx = 0;
                for(int i=2;i<=n;i++){
                    if(n%i==0){
                        idx = i;
                        break;
                    }
                }
                return (idx+n);
            }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        if(n==1){
            cout<<0<<"\n";
            continue;
        }
        bool decr = false;
        int answer = 0;
        for(int i=1;i<n;i++){
            if(decr == true){
                if(a[i]>a[i-1]){
                    answer = i-1;
                }
            }
            if(a[i]<a[i-1]){
                decr = true;
            }
            else{
                decr = false;
            }
        }
        cout<<answer<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;
int gcd(int a,int b){
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int modpow(int x,int n)
{
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=((result%M) * (x%M))%M;
        x=((x%M)*(x%M))%M;
        n=n/2;
    }
    return result;
}
int lcm(int a,int b){
    return (a*b)/(gcd(a,b));
}
bool isprime(int n){
    if(n<2)
	return false;
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return false;
    return true;
}
int ctoi(char c){
    return c-48;
}
bool visited[1000000]{false};
void add_edge(vector<int> v[],int x,int y){
    v[x].push_back(y);
    v[y].push_back(x);
}
void dfs(vector<int> v[],int e){
    visited[e] = true;
    for(int i=0;i<v[e].size();i++){
        if(!visited[v[e][i]]){
            dfs(v,v[e][i]);
        }
    }
}
int func(int n){
    if(isprime(n)){
                return 2*n;
            }
            else{
                int idx = 0;
                for(int i=2;i<=n;i++){
                    if(n%i==0){
                        idx = i;
                        break;
                    }
                }
                return (idx+n);
            }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        if(n<=2){
            cout<<0<<"\n";
            continue;
        }
        bool flag=false;
        for(int i=1;i<n;i++){
            if(a[i]>a[i-1]){
                flag=true;
                break;
            }
        }
        if(!flag){
            cout<<0<<"\n";
            continue;
        }
        bool decr = false;
        int answer = 0;
        for(int i=1;i<n;i++){
            if(decr == true){
                if(a[i]>=a[i-1]){
                    answer = i-1;
                }
            }
            if(a[i]<a[i-1]){
                decr = true;
            }
            else{
                decr = false;
            }
        }
        cout<<answer<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;
int gcd(int a,int b){
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int modpow(int x,int n)
{
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=((result%M) * (x%M))%M;
        x=((x%M)*(x%M))%M;
        n=n/2;
    }
    return result;
}
int lcm(int a,int b){
    return (a*b)/(gcd(a,b));
}
bool isprime(int n){
    if(n<2)
	return false;
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return false;
    return true;
}
int ctoi(char c){
    return c-48;
}
bool visited[1000000]{false};
void add_edge(vector<int> v[],int x,int y){
    v[x].push_back(y);
    v[y].push_back(x);
}
void dfs(vector<int> v[],int e){
    visited[e] = true;
    for(int i=0;i<v[e].size();i++){
        if(!visited[v[e][i]]){
            dfs(v,v[e][i]);
        }
    }
}
int func(int n){
    if(isprime(n)){
                return 2*n;
            }
            else{
                int idx = 0;
                for(int i=2;i<=n;i++){
                    if(n%i==0){
                        idx = i;
                        break;
                    }
                }
                return (idx+n);
            }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        if(n<=2){
            cout<<0<<"\n";
            continue;
        }
        bool decr = false;
        int answer = 0;
        for(int i=1;i<n;i++){
            if(decr == true){
                if(a[i]>a[i-1]){
                    answer = i-1;
                }
            }
            if(a[i]<a[i-1]){
                decr = true;
            }
            else if(a[i]>a[i-1]){
                decr = false;
            }
        }
        cout<<answer<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;
int gcd(int a,int b){
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int modpow(int x,int n)
{
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=((result%M) * (x%M))%M;
        x=((x%M)*(x%M))%M;
        n=n/2;
    }
    return result;
}
int lcm(int a,int b){
    return (a*b)/(gcd(a,b));
}
bool isprime(int n){
    if(n<2)
	return false;
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return false;
    return true;
}
int ctoi(char c){
    return c-48;
}
bool visited[1000000]{false};
void add_edge(vector<int> v[],int x,int y){
    v[x].push_back(y);
    v[y].push_back(x);
}
void dfs(vector<int> v[],int e){
    visited[e] = true;
    for(int i=0;i<v[e].size();i++){
        if(!visited[v[e][i]]){
            dfs(v,v[e][i]);
        }
    }
}
int func(int n){
    if(isprime(n)){
                return 2*n;
            }
            else{
                int idx = 0;
                for(int i=2;i<=n;i++){
                    if(n%i==0){
                        idx = i;
                        break;
                    }
                }
                return (idx+n);
            }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        if(n<=2){
            cout<<0<<"\n";
            continue;
        }
        bool flag=false;
        int idx = -1;
        for(int i=1;i<n;i++){
            if(a[i]>=a[i-1]){
                idx = i-1;
                break;
            }
        }
        if(idx < 0){
            idx = 0;
        }
        for(int i=idx+1;i<n;i++){
            if(a[i]<a[i-1]){
                flag=true;
                break;
            }
        }
        if(!flag){
            cout<<(idx)<<"\n";
            continue;
        }
        bool decr = false;
        int answer = 0;
        for(int i=1;i<n;i++){
            if(decr == true){
                if(a[i]>a[i-1]){
                    answer = i-1;
                }
            }
            if(a[i]<a[i-1]){
                decr = true;
            }
            else if(a[i]>a[i-1]){
                decr = false;
            }
        }
        cout<<answer<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;
int gcd(int a,int b){
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int modpow(int x,int n)
{
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=((result%M) * (x%M))%M;
        x=((x%M)*(x%M))%M;
        n=n/2;
    }
    return result;
}
int lcm(int a,int b){
    return (a*b)/(gcd(a,b));
}
bool isprime(int n){
    if(n<2)
	return false;
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return false;
    return true;
}
int ctoi(char c){
    return c-48;
}
bool visited[1000000]{false};
void add_edge(vector<int> v[],int x,int y){
    v[x].push_back(y);
    v[y].push_back(x);
}
void dfs(vector<int> v[],int e){
    visited[e] = true;
    for(int i=0;i<v[e].size();i++){
        if(!visited[v[e][i]]){
            dfs(v,v[e][i]);
        }
    }
}
int func(int n){
    if(isprime(n)){
                return 2*n;
            }
            else{
                int idx = 0;
                for(int i=2;i<=n;i++){
                    if(n%i==0){
                        idx = i;
                        break;
                    }
                }
                return (idx+n);
            }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        if(n<=2){
            cout<<0<<"\n";
            continue;
        }
        bool decr = false;
        int answer = 0;
        for(int i=1;i<n;i++){
            if(decr == true){
                if(a[i]>a[i-1]){
                    answer = i-1;
                }
            }
            if(a[i]<a[i-1]){
                decr = true;
            }
            else{
                decr = false;
            }
        }
        cout<<answer<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;
int gcd(int a,int b){
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int modpow(int x,int n)
{
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=((result%M) * (x%M))%M;
        x=((x%M)*(x%M))%M;
        n=n/2;
    }
    return result;
}
int lcm(int a,int b){
    return (a*b)/(gcd(a,b));
}
bool isprime(int n){
    if(n<2)
	return false;
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return false;
    return true;
}
int ctoi(char c){
    return c-48;
}
bool visited[1000000]{false};
void add_edge(vector<int> v[],int x,int y){
    v[x].push_back(y);
    v[y].push_back(x);
}
void dfs(vector<int> v[],int e){
    visited[e] = true;
    for(int i=0;i<v[e].size();i++){
        if(!visited[v[e][i]]){
            dfs(v,v[e][i]);
        }
    }
}
int func(int n){
    if(isprime(n)){
                return 2*n;
            }
            else{
                int idx = 0;
                for(int i=2;i<=n;i++){
                    if(n%i==0){
                        idx = i;
                        break;
                    }
                }
                return (idx+n);
            }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        if(n<=2){
            cout<<0<<"\n";
            continue;
        }
        bool decr = false;
        int answer = 0;
        for(int i=1;i<n;i++){
            if(decr == true){
                if(a[i]>a[i-1]){
                    answer = i-1;
                }
            }
            if(a[i]<a[i-1]){
                decr = true;
            }
        }
        cout<<answer<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;
int gcd(int a,int b){
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int modpow(int x,int n)
{
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=((result%M) * (x%M))%M;
        x=((x%M)*(x%M))%M;
        n=n/2;
    }
    return result;
}
int lcm(int a,int b){
    return (a*b)/(gcd(a,b));
}
bool isprime(int n){
    if(n<2)
	return false;
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return false;
    return true;
}
int ctoi(char c){
    return c-48;
}
bool visited[1000000]{false};
void add_edge(vector<int> v[],int x,int y){
    v[x].push_back(y);
    v[y].push_back(x);
}
void dfs(vector<int> v[],int e){
    visited[e] = true;
    for(int i=0;i<v[e].size();i++){
        if(!visited[v[e][i]]){
            dfs(v,v[e][i]);
        }
    }
}
int func(int n){
    if(isprime(n)){
                return 2*n;
            }
            else{
                int idx = 0;
                for(int i=2;i<=n;i++){
                    if(n%i==0){
                        idx = i;
                        break;
                    }
                }
                return (idx+n);
            }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        if(n<=2){
            cout<<0<<"\n";
            continue;
        }
        bool decr = false;
        int answer = 0;
        for(int i=1;i<n;i++){
            if(decr == true){
                if(a[i]>=a[i-1]){
                    answer = i-1;
                }
            }
            if(a[i]<a[i-1]){
                decr = true;
            }
            else{
                decr = false;
            }
        }
        cout<<answer<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
void solution()
{
    long long n;
    cin>>n;
    long long arr[1000];
    for(long long i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    //4 3 3 8 4 5 2
    //0 1 2 3 4 5 6
    long long index=0;
    for(long long i=n-2;i>=0;i--)
    {
        if(arr[i+1]>arr[i])
        {
            index = i+1;
            break;
        }
    }
    //cout<<index<<endl;
    for(long long i=index-1;i>=0;i--)
    {
        if(arr[i+1]<arr[i])
        {
            index = i+1;
            cout<<index<<endl;
            return;
        }
    }
    cout<<0<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solution();
    }
}


#include <bits/stdc++.h>
using namespace std;
void solution()
{
    long long n;
    cin>>n;
    //long long arr[2000000];
    vector<long long> arr(n);
    for(long long i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    //4 3 3 8 4 5 2
    //0 1 2 3 4 5 6
    long long index=0;
    for(long long i=n-2;i>=0;i--)
    {
        if(arr[i+1]>arr[i])
        {
            index = i+1;
            break;
        }
    }
    //cout<<index<<endl;
    for(long long i=index-1;i>=0;i--)
    {
        if(arr[i+1]<arr[i])
        {
            index = i+1;
            cout<<index<<endl;
            return;
        }
    }
    cout<<0<<endl;
    return;
}
int main()
{
    long long t;
    cin>>t;
    while(t--)
    {
        solution();
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[1000];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        //4 3 3 8 4 5 2
        //0 1 2 3 4 5 6
        int index;
        for(int i=n-1;i>=0;i--)
        {
            if(arr[i-1]<arr[i])
            {
                index = i;
                break;
            }
        }
        //cout<<index<<endl;
        bool flag = 0;
        for(int i=index-1;i>=0;i--)
        {
            if(arr[i-1]>arr[i])
            {
                index = i-1;
                flag = 1;
                break;
            }
        }
        //cout<<index<<endl;
        if(flag)
        {
            cout<<index+1<<endl;
        }
        else
        {
            cout<<0<<endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void solution()
{
    long long n;
    cin>>n;
    int arr[1000];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    //4 3 3 8 4 5 2
    //0 1 2 3 4 5 6
    int index=0;
    for(int i=n-2;i>=0;i--)
    {
        if(arr[i+1]>arr[i])
        {
            index = i+1;
            break;
        }
    }
    //cout<<index<<endl;
    for(int i=index-1;i>=0;i--)
    {
        if(arr[i+1]<arr[i])
        {
            index = i+1;
            cout<<index<<endl;
            return;
        }
    }
    cout<<0<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solution();
    }
}


#include <bits/stdc++.h>
using namespace std;
void solution()
{
    long long n;
    cin>>n;
    long long arr[1000];
    for(long long i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    //4 3 3 8 4 5 2
    //0 1 2 3 4 5 6
    long long index=0;
    for(long long i=n-2;i>=0;i--)
    {
        if(arr[i+1]>arr[i])
        {
            index = i+1;
            break;
        }
    }
    //cout<<index<<endl;
    for(long long i=index-1;i>=0;i--)
    {
        if(arr[i+1]<arr[i])
        {
            index = i+1;
            cout<<index<<endl;
            return;
        }
    }
    cout<<0<<endl;
}
int main()
{
    long long t;
    cin>>t;
    while(t--)
    {
        solution();
    }
}


#include <bits/stdc++.h>
using namespace std;
void solution()
{
    long long n;
    cin>>n;
    long long arr[1000];
    for(long long i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    //4 3 3 8 4 5 2
    //0 1 2 3 4 5 6
    long long index=0;
    for(long long i=n-2;i>=0;i--)
    {
        if(arr[i+1]>arr[i])
        {
            index = i+1;
            break;
        }
    }
    //cout<<index<<endl;
    for(long long i=index-1;i>=0;i--)
    {
        if(arr[i+1]<arr[i])
        {
            index = i+1;
            cout<<index<<endl;
            return;
        }
    }
    cout<<0<<endl;
    return;
}
int main()
{
    long long t;
    cin>>t;
    while(t--)
    {
        solution();
    }
}


#include <bits/stdc++.h>
using namespace std;
void solution()
{
    int n;
    cin>>n;
    int arr[1000];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    //4 3 3 8 4 5 2
    //0 1 2 3 4 5 6
    int index=0;
    for(int i=n-2;i>=0;i--)
    {
        if(arr[i+1]>arr[i])
        {
            index = i+1;
            break;
        }
    }
    //cout<<index<<endl;
    for(int i=index-1;i>=0;i--)
    {
        if(arr[i+1]<arr[i])
        {
            index = i+1;
            cout<<index<<endl;
            return;
        }
    }
    cout<<0<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solution();
    }
}


#include <bits/stdc++.h>
using namespace std;
void solution()
{
    long long n;
    cin>>n;
    long long arr[2000000];
    for(long long i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    //4 3 3 8 4 5 2
    //0 1 2 3 4 5 6
    long long index=0;
    for(long long i=n-2;i>=0;i--)
    {
        if(arr[i+1]>arr[i])
        {
            index = i+1;
            break;
        }
    }
    //cout<<index<<endl;
    for(long long i=index-1;i>=0;i--)
    {
        if(arr[i+1]<arr[i])
        {
            index = i+1;
            cout<<index<<endl;
            return;
        }
    }
    cout<<0<<endl;
    return;
}
int main()
{
    long long t;
    cin>>t;
    while(t--)
    {
        solution();
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[1000];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        //4 3 3 8 4 5 2
        //0 1 2 3 4 5 6
        int index;
        for(int i=n-1;i>=0;i--)
        {
            if(arr[i-1]<arr[i])
            {
                index = i;
                break;
            }
        }
        //cout<<index<<endl;
        bool flag = 0;
        for(int i=index-1;i>=0;i--)
        {
            if(i-1>=0)
            {
                if(arr[i-1]>arr[i])
                {
                    index = i-1;
                    flag = 1;
                    break;
                }
            }
        }
        //cout<<index<<endl;
        if(flag)
        {
            cout<<index+1<<endl;
        }
        else
        {
            cout<<0<<endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[1000];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        //4 3 3 8 4 5 2
        //0 1 2 3 4 5 6
        int index;
        for(int i=n-1;i>=0;i--)
        {
            if(arr[i-1]<arr[i])
            {
                index = i;
                break;
            }
        }
        //cout<<index<<endl;
        bool flag = 0;
        for(int i=index-1;i>=0;i--)
        {
            if(i-1>=0)
            {
                if(arr[i-1]>arr[i])
                {
                    index = i-1;
                    flag = 1;
                    break;
                }
            }
        }
        //cout<<index<<endl;
        if(n==1)
        {
            cout<<0<<endl;
        }
        else if(flag)
        {
            cout<<index+1<<endl;
        }
        else
        {
            cout<<0<<endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[1000];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        int first=0,last=n-1,count=0;
        again:
        int take = 0;
        last = n-1;
        while(first<=last)
        {
            int temp;
            if(arr[first]>arr[last])
            {
                temp = arr[last];
                if(temp>=take)
                {
                    take=temp;
                    last--;
                }
                else
                {
                    count++;
                    first=count;
                    goto again;
                }
            }
            else
            {
                temp = arr[first];
                if(temp>=take)
                {
                    take=temp;
                    first++;
                }
                else
                {
                    count++;
                    first=count;
                    goto again;
                }
            }
        }
        cout<<count<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int arr[1000];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        //4 3 3 8 4 5 2
        //0 1 2 3 4 5 6
        int index;
        for(int i=n-1;i>=0;i--)
        {
            if(arr[i-1]<arr[i])
            {
                index = i;
                break;
            }
        }
        //cout<<index<<endl;
        bool flag = 0;
        for(int i=index-1;i>=0;i--)
        {
            if(arr[i-1]>arr[i])
            {
                if(i-1>=0)
                {
                    index = i-1;
                }
                else
                {
                    index=i;
                }
                flag = 1;
                break;
            }
        }
        //cout<<index<<endl;
        if(flag)
        {
            cout<<index+1<<endl;
        }
        else
        {
            cout<<0<<endl;
        }
    }
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  int k=0;
  for(int i=n-2;i>0;i++){
    if(a[i]<a[i+1]){
    k=1;
    }
     if(a[i]<a[i-1]&&a[i]<=a[i+1]&&k){
        x=i;
        break;
     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  vector<int>v;
  for(int i=1;i<=n-2;i++){

     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i-1;

     }
  }

   cout<<x<<endl;
}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  vector<int>v;
  for(int i=1;i<=n-2;i++){

     if(a[i]<a[i-1]&&a[i]==a[i+1]){
        while(a[i]==a[i+1]){
           i++;
        }
        x=i-1;

     }else
     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i;
     }
  }

   cout<<x<<endl;
}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;

  for(int i=1;i<=n-2;i++){
     if(a[i]>a[i-1]&&a[i]>a[i+1]){
     x=i+1;
     break;
     }else
     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i;
        break;
     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  vector<int>v;
  for(int i=1;i<=n-2;i++){

     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i;

     }
  }

   cout<<x<<endl;
}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;

  for(int i=1;i<=n-2;i++){
     if((a[i]>a[i-1]&&a[i]>a[i+1])||(a[i]==a[i-1]&&a[i]>a[i+1])||(a[i]>a[i-1]&&a[i]==a[i+1])){
     x=i+1;
     break;
     }else
     if((a[i]<a[i-1]&&a[i]<a[i+1])||(a[i]==a[i-1]&&a[i]<a[i+1])||(a[i]<a[i-1]&&a[i]==a[i+1])){
        x=i;
        break;
     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;

  for(int i=n-2;i>0;i++){

     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i;
        break;
     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;

  for(int i=1;i<=n-2;i++){
     if(a[i]>a[i-1]&&a[i]>a[i+1]&&a[i-1]<a[i+1]){
     x=i+1;

     }else
     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i;

     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  int k=0;
  for(int i=n-2;i>0;i++){
    if(a[i]<a[i+1]){
    k=1;
    }
     if(a[i]<a[i-1]&&a[i]<a[i+1]&&k){
        x=i;
        break;
     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  vector<int>v;
  for(int i=1;i<=n-2;i++){

     if(a[i]<a[i-1]){
        while(a[i]>=a[i+1]){
           i++;
        }
        x=i;

     }
  }

   cout<<x<<endl;
}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  in k=0;
  for(int i=n-2;i>0;i++){
    if(a[i]<a[i+1]){
    k=1;
    }
     if(a[i]<a[i-1]&&a[i]<=a[i+1]&&k){
        x=i;
        break;
     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  vector<int>v;
  for(int i=1;i<=n-2;i++){
     if(a[i]>a[i-1]&&a[i]>a[i+1]){
     x=i+1;
     v.push_back(x);

     }else
     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i;
        v.push_back(x);

     }
  }
  if(v.size()==0||v.size()==1){
     cout<<"0"<<endl;
  }else{
  cout<<v[v.size()-2]<<endl;}

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  vector<int>v;
  for(int i=1;i<=n-2;i++){

     if(a[i]<a[i-1]&&a[i]==a[i+1]){
        while(a[i]>=a[i+1]){
           i++;
        }
        x=i-1;

     }else
     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i;
     }
  }

   cout<<x<<endl;
}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;

  for(int i=1;i<=n-2;i++){
     if(a[i]>a[i-1]&&a[i]>a[i+1]){
     x=i-1;
     }else
     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i-1;
     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;

  for(int i=1;i<=n-2;i++){
     if(a[i]>a[i-1]&&a[i]>a[i+1]&&a[i-1]<=a[i+1]){
     x=i+1;

     }else
     if(a[i]<a[i-1]&&a[i]<a[i+1]){
        x=i;

     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
#define ll long long;
long long mod=1000000007;

bool isPrime(long long n)
{

    if (n <= 1)
        return false;

    for (long long i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

bool isPalindrome(string s)
{

    int l = 0;
    int h = s.size()-1;

    while (h > l)
    {
        if (s[l++] != s[h--])
        {

            return false;
        }
    }
   return true;
}

int main(){
ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
int t;
cin>>t;
for(int i=0;i<t;i++){
  int n;
  cin>>n;
  int a[n];
  for(int i=0;i<n;i++){
     cin>>a[i];

  }
  int x=0;
  int k=0;
  for(int i=n-2;i>0;i--){
    if(a[i]<a[i+1]){
    k=1;
    }
     if(a[i]<a[i-1]&&a[i]<=a[i+1]&&k){
        x=i;
        break;
     }
  }
  cout<<x<<endl;

}
return 0;
}


#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[n];
    }
    pos=n-1;
    while(pos>0&&arr[pos-1]>=arr[pos])--pos;
    while(pos>0&&arr[pos-1]<=arr[pos])--pos;
    cout<<pos<<endl;}
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int check(vector<int>& a,int& j,int& k,int& flag){
    int min = 200001;
    for(int y=j;y<=k;y++){
        if (min>a[y]){
            min = a[y];
        }
    }
    if((a[j])!=min&&(a[k])!=min){
        flag = j;
        return j;
    }
    else if(a[k]==min){
        k-=1;
        j-=1;
    }
    return -1;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int k=n-1;
        int res;
        int flag = -1;
        for(int j=0;j<n&&j<k;j++){
            res = check(a,j,k,flag);
        }
        cout<<flag+1<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
   int pos=n-1;
    while(pos>0&&arr[pos-1]>=arr[pos])--pos;
    while(pos>0&&arr[pos-1]<=arr[pos])--pos;
    cout<<pos<<endl;}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int check(vector<int>& a,int& j,int& k,int& flag){
    int min = 200001;
    for(int y=j;y<=k;y++){
        if (min>a[y]){
            min = a[y];
        }
    }
    if(a[j]!=min&&a[k]!=min){
        flag = j;
        return j;
    }
    else if(a[k]==min){
        k-=1;
    }
    return -1;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int k=n-1;
        int res;
        int flag = -1;
        for(int j=0;j<n&&j<=k;j++){
            res = check(a,j,k,flag);
        }
        cout<<flag+1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int check(vector<int>& a,int& j,int& k,int& flag){
    int min = 200001;
    for(int y=j;y<=k;y++){
        if (min>a[y]){
            min = a[y];
        }
    }
    if((a[j])!=min&&(a[k])!=min){
        flag = j;
        return j;
    }
    else if(a[k]==min){
        k-=1;
        j-=1;
    }
    return -1;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int k=n-1;
        int res;
        int flag = -1;
        for(int j=0;j<n&&j<=k;j++){
            res = check(a,j,k,flag);
        }
        cout<<flag+1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int check(vector<int>& a,int& j,int& k){
    vector<int> b(k-j+1);
    int min = 200001,index;
    for(int y=j;y<=k;y++){
        if (min>a[y]){
            min = a[y];
        }
    }
    if(a[j]!=min&&a[k]!=min){
        return j;
    }
    else if(a[k]==min){
        k-=1;
    }
    return -1;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int k=n-1;
        int res;
        for(int j=0;j<n&&j<k;j++){
            res = check(a,j,k,flag);
        }
        cout<<res+1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int check(vector<int>& a,int& j,int& k,int& flag){
    int min = 200001;
    for(int y=j;y<=k;y++){
        if (min>a[y]){
            min = a[y];
        }
    }
    if(a[j]!=min&&a[k]!=min){
        flag = j;
        return j;
    }
    else if(a[k]==min){
        k-=1;
    }
    return -1;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int k=n-1;
        int res;
        int flag = -1;
        for(int j=0;j<n&&j<k;j++){
            res = check(a,j,k,flag);
        }
        cout<<flag+1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int check(vector<int>& a,int& j,int& k,int& flag){
    int min = 200001;
    for(int y=j;y<=k;y++){
        if (min>=a[y]){
            min = a[y];
        }
    }
    if((a[j])!=min&&(a[k])!=min){
        flag = j;
        return j;
    }
    else if(a[k]==min){
        k-=1;
    }
    return -1;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int k=n-1;
        int res;
        int flag = -1;
        for(int j=0;j<n&&j<=k;j++){
            res = check(a,j,k,flag);
        }
        cout<<flag+1<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[n];
    }
   int pos=n-1;
    while(pos>0&&arr[pos-1]>=arr[pos])--pos;
    while(pos>0&&arr[pos-1]<=arr[pos])--pos;
    cout<<pos<<endl;}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int check(vector<int>& a,int& j,int& k,int& flag){
    int min = 200001;
    for(int y=j;y<=k;y++){
        if (min>a[y]){
            min = a[y];
        }
    }
    if((a[j])!=min&&(a[k])!=min){
        flag = j;
        return j;
    }
    else if(a[k]==min){
        k-=1;
        j-=1;
    }
    return -1;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int k=n-1;
        int res;
        int flag = -1;
        for(int j=0;j<n&&j<k;j++){
            res = check(a,j,k,flag);
        }
        cout<<flag+1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int check(vector<int>& a,int& j,int& k){
    vector<int> b(k-j+1);
    int min = 200001,index;
    for(int y=j;y<=k;y++){
        if (min>a[y]){
            min = a[y];
        }
    }
    if(a[j]!=min&&a[k]!=min){
        return j;
    }
    else if(a[k]==min){
        k-=1;
    }
    return -1;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int k=n-1;
        int res;
        for(int j=0;j<n&&j<k;j++){
            res = check(a,j,k);
        }
        cout<<res+1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef long double ld;
#define fo(iter,n)	    for(ll iter=0;iter<n;iter++)
#define foc(iter,s,e)	for(ll iter=s;iter<=e;iter++)
#define fob(iter,s,e)	for(ll iter=s;iter>=e;iter--)
#define MOD				(ll)1000000007
#define pp				pair<ll,ll>
#define vii				vector<int>
#define vll				vector<ll>
#define vpp				vector<pp>
#define vss				vector<string>
#define take(A,n)		{fo(iter,n)cin>>A[iter];}
#define pb				push_back
#define pob				pop_back
#define eb				emplace_back
#define pf				push_front
#define pof				pop_front
#define mkp				make_pair
#define all(a)			a.begin(),a.end()
#define ff				first
#define ss				second
#define lcm(a,b)		(a*b)/__gcd(a,b)
#define bs(v,x)			binary_search(all(v),x)
#define ub(v,x)			upper_bound(all(v),x)
#define lb(v,x)			lower_bound(all(v),x)
#define FIO			    ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pi              ld(3.14159265358979323846)
void pr(ll x){cout<<x;}
void prt(ll x){cout<<x<<endl;}

void sol()
{
    ll n;
    cin>>n;
    ll a[n];
    ll c[n];
    fo(i,n)
    {
        cin>>a[i];
        c[i]=a[i];
    }
    sort(c,c+n);
    if(n==1||n==2){
        cout<<0<<endl;
        return;
    }

    ll tmp=0;
    for(ll i=n-2;i>=1;i--){
            if(a[i]<a[i-1]&&a[i]<a[i+1]){
                tmp=i;
                break;
            }
            else if(a[i]<a[i-1]&&a[i]<a[n-1]){
                tmp=i;
                break;
            }

    }
    prt(tmp);

}

int main()
{
    FIO;
    ll T=1;
    cin>>T;

    while(T--)
    {

       sol();
    }

    return 0;
    }


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef long double ld;
#define fo(iter,n)	    for(ll iter=0;iter<n;iter++)
#define foc(iter,s,e)	for(ll iter=s;iter<=e;iter++)
#define fob(iter,s,e)	for(ll iter=s;iter>=e;iter--)
#define MOD				(ll)1000000007
#define pp				pair<ll,ll>
#define vii				vector<int>
#define vll				vector<ll>
#define vpp				vector<pp>
#define vss				vector<string>
#define take(A,n)		{fo(iter,n)cin>>A[iter];}
#define pb				push_back
#define pob				pop_back
#define eb				emplace_back
#define pf				push_front
#define pof				pop_front
#define mkp				make_pair
#define all(a)			a.begin(),a.end()
#define ff				first
#define ss				second
#define lcm(a,b)		(a*b)/__gcd(a,b)
#define bs(v,x)			binary_search(all(v),x)
#define ub(v,x)			upper_bound(all(v),x)
#define lb(v,x)			lower_bound(all(v),x)
#define FIO			    ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pi              ld(3.14159265358979323846)
void pr(ll x){cout<<x;}
void prt(ll x){cout<<x<<endl;}

void sol()
{
    ll n;
    cin>>n;
    ll a[n];
    ll c[n];
    fo(i,n)
    {
        cin>>a[i];
        c[i]=a[i];
    }
    sort(c,c+n);
    if(n==1||n==2){
        cout<<0<<endl;
        return;
    }
    ll fl=0;
    fo(i,n)
    {
        if(a[i]!=c[i]){
            fl=1;
            break;
        }
    }
    if(fl==0){
        cout<<0<<endl;
        return;
    }

    ll tmp=0;
    for(ll i=n-2;i>=1;i--){
            if(a[i]<a[i-1]&&a[i]<a[i+1]){

                tmp=i;
                break;
            }
            else if(i!=(n-2)&&a[i]<a[i-1]&&a[i]<=a[i+1]){
                tmp=i;
                break;
            }

    }

    prt(tmp);

}

int main()
{
    FIO;
    ll T=1;
    cin>>T;

    while(T--)
    {

       sol();
    }

    return 0;
    }