#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (i = 0; i < n; ++i)
#define for1(i, n) for (i = 1; i <= n; ++i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fill(x, b) memset((x), b, sizeof((x)))
#define fill2D(x, r, c, b) memset((x), b, sizeof(x[0][0]) * r * c)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<vs> vvs;

const int mod = 1e9 + 7;

ll binPow(ll a, ll b)
{
    ll x = 1, y = a;
    while (b)
    {
        if (b & 1)
            x = (x * y) % mod;
        y = (y * y) % mod;
        b >>= 1;
    }
    return x % mod;
}

ll inverserEuler(int n) { return binPow(n, mod - 2); }

ll C(int k, int n)
{
    vll f(n + 1, 1);
    for (int i = 2; i <= n; i++)
        f[i] = (f[i - 1] * i) % mod;
    return (f[n] * ((inverserEuler(f[k]) * inverserEuler(f[n - k])) % mod) % mod) % mod;
}

/* Extend Euclid: ax + by = c; */
// ll x, y;

// void extendEuclid(ll a, ll b){if(b == 0){x = 1;y = 0;return;}extendEuclid(b,a%b);ll x1 = y, y1 = x - (a/b) * y;x = x1;y = y1;}
// nghiem : x + (b/d)*k, y - (a/d) * k;

/*--------------------------------------------------------------------------------------------------*/

double dist(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

int n, m, x;

char key[31][31];

void trunghieu()
{
    cin >> n >> m >> x;
    int i, j, ans = 0, k;
    map<char, vector<pair<int, int>>> pos;
    for1(i, n)
        for1(j, m)
    {
        cin >> key[i][j];
        pos[key[i][j]].push_back({i, j});
    }
    int q;
    cin >> q;
    string s;
    cin >> s;
    char preChar;
    vector<pair<int, int>> pS = pos['S'];
    forn(i, s.size())
    {
        auto pI = pos[tolower(s[i])];
        if(pI.empty())
        {
            cout<<-1;
            return;
        }
        if (i == 0)
        {
            if (isupper(s[i]))
            {
                if (pS.empty())
                {
                    cout << -1;
                    return;
                }
                else
                {
                    int flag = 0;
                    for (auto j : pS)
                    {
                        if (dist(j.first, j.second, pI[0].first, pI[0].second) <= x)
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if (!flag)
                        ans++;
                }
            }
        }
        else
        {
            auto pr = pos[tolower(s[i - 1])];
            if (isupper(s[i]))
            {
                if (pS.empty())
                {
                    cout << -1;
                    return;
                }
                else
                {
                    if (pS.empty())
                    {
                        cout << -1;
                        return;
                    }
                    else
                    {
                        int flag = 0;
                        for (auto j : pS)
                        {
                            if (dist(j.first, j.second, pI[0].first, pI[0].second) <= x)
                            {
                                flag = 1;
                                break;
                            }
                        }
                        if (!flag)
                            ans++;
                    }
                }
            }
        }
    }
    cout << ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie();
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    //freopen("output.txt","w",stdout);
#endif
    trunghieu();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (i = 0; i < n; ++i)
#define for1(i, n) for (i = 1; i <= n; ++i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fill(x, b) memset((x), b, sizeof((x)))
#define fill2D(x, r, c, b) memset((x), b, sizeof(x[0][0]) * r * c)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<vs> vvs;

const int mod = 1e9 + 7;

ll binPow(ll a, ll b)
{
    ll x = 1, y = a;
    while (b)
    {
        if (b & 1)
            x = (x * y) % mod;
        y = (y * y) % mod;
        b >>= 1;
    }
    return x % mod;
}

ll inverserEuler(int n) { return binPow(n, mod - 2); }

ll C(int k, int n)
{
    vll f(n + 1, 1);
    for (int i = 2; i <= n; i++)
        f[i] = (f[i - 1] * i) % mod;
    return (f[n] * ((inverserEuler(f[k]) * inverserEuler(f[n - k])) % mod) % mod) % mod;
}

/* Extend Euclid: ax + by = c; */
// ll x, y;

// void extendEuclid(ll a, ll b){if(b == 0){x = 1;y = 0;return;}extendEuclid(b,a%b);ll x1 = y, y1 = x - (a/b) * y;x = x1;y = y1;}
// nghiem : x + (b/d)*k, y - (a/d) * k;

/*--------------------------------------------------------------------------------------------------*/

double dist(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

int n, m, x;

char key[31][31];

void trunghieu()
{
    cin >> n >> m >> x;
    int i, j, ans = 0, k;
    map<char, vector<pair<int, int>>> pos;
    for1(i, n)
        for1(j, m)
    {
        cin >> key[i][j];
        pos[key[i][j]].push_back({i, j});
    }
    int q;
    cin >> q;
    string s;
    cin >> s;
    char preChar;
    vector<pair<int, int>> pS = pos['S'];
    forn(i, s.size())
    {
        auto pI = pos[tolower(s[i])];
        if (i == 0)
        {
            if (isupper(s[i]))
            {
                if (pS.empty())
                {
                    cout << -1;
                    return;
                }
                else
                {
                    int flag = 0;
                    for (auto j : pS)
                    {
                        if (dist(j.first, j.second, pI[0].first, pI[0].second) <= x)
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if (!flag)
                        ans++;
                }
            }
        }
        else
        {
            auto pr = pos[tolower(s[i - 1])];
            if (isupper(s[i]))
            {
                if (pS.empty())
                {
                    cout << -1;
                    return;
                }
                else
                {
                    if (pS.empty())
                    {
                        cout << -1;
                        return;
                    }
                    else
                    {
                        int flag = 0;
                        for (auto j : pS)
                        {
                            if (dist(j.first, j.second, pI[0].first, pI[0].second) <= x)
                            {
                                flag = 1;
                                break;
                            }
                        }
                        if (!flag)
                            ans++;
                    }
                }
            }
        }
    }
    cout << ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie();
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    //freopen("output.txt","w",stdout);
#endif
    trunghieu();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lld;
/*
void print(vector<pair<ll,ll>> arr){
for(ll i=0;i<arr.size();i++){
    cout<<"{ "<<arr[i].first<<","<<arr[i].second<<"} ";
}
cout<<endl;
}
*/
int main(){
ll n,m,k;
lld x;
string s;
cin>>n>>m;
cin>>x;
map<char,vector<pair<ll,ll>>> mp;
for(ll i=0;i<n;i++){
    string s;
    cin>>s;
    for(ll j=0;j<s.length();j++){
        if(mp.find(s[j])==mp.end()){
            vector<pair<ll,ll>> p;
            p.push_back({i,j});
            mp.insert({s[j],p});
        }
        else{
            vector<pair<ll,ll>> p=mp[s[j]];
            p.push_back({i,j});
            mp[s[j]]=p;
        }
    }

}
ll ans=0;
cin>>k;
cin>>s;
for(ll a=0;a<k;a++){
    if(mp.find(tolower(s[a]))==mp.end()){
        cout<<-1;
        exit(0);
    }
    if(isupper(s[a])){
        if(mp.find('S')==mp.end()){
            cout<<-1;
            exit(0);
        }
        vector<pair<ll,ll>> charac=mp[tolower(s[a])];
        vector<pair<ll,ll>> shift=mp['S'];
        //print(shift);
        //print(charac);
        ll q,w;
        for(q=0;q<charac.size();q++){
            for(w=0;w<shift.size();w++){
                lld dist=sqrt(pow((charac[q].first-shift[w].first),2)+pow((charac[q].second-shift[w].second),2));
                //cout<<"dist: "<<dist<<endl;
                if(dist<x){
                    break;
                }
            }
            if(w!=shift.size()){
                break;
            }
        }
        if(q!=charac.size()){
            continue;
        }
        ans++;
    }
}
cout<<ans;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lld;
/*
bool check(vector<string> arr, char ch,ll n,ll m){
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]==ch){
            //cout<<"true\n";
            return true;
        }
    }
}
//cout<<"false\n";
return false;
}
**********************************************************************************************************************
lld coor(vector<string> arr,vector<pair<ll,ll>> shift,lld x, char ch,ll n,ll m){
lld mi1=x+1;
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]==ch){
            lld mi=x+1;
            pair<ll,ll> p={i,j};
            for(ll b=0;b<shift.size();b++){
                    lld dist=sqrt(pow((shift[b].first-p.first),2)+pow((shift[b].second-p.second),2));
                    //cout<<"dist: "<<dist<<endl;
                    if(mi>dist){
                        mi=dist;
                    }
                }
                if(mi1>mi){
                    mi1=mi;
                }
        }
    }
}
return mi1;
}

*********************************************************************************************************************
void print(vector<pair<ll,ll>> arr){
for(ll i=0;i<arr.size();i++){
    cout<<"{ "<<arr[i].first<<","<<arr[i].second<<"} ";
}
cout<<endl;
}
*/
int main(){
ll n,m,k;
lld x;
string s;
cin>>n>>m;
cin>>x;
map<char,vector<pair<ll,ll>>> mp;
for(ll i=0;i<n;i++){
    string s;
    cin>>s;
    for(ll j=0;j<s.length();j++){
        if(mp.find(s[j])==mp.end()){
            vector<pair<ll,ll>> p;
            p.push_back({i,j});
            mp.insert({s[j],p});
        }
        else{
            vector<pair<ll,ll>> p=mp[s[j]];
            p.push_back({i,j});
            mp[s[j]]=p;
        }
    }

}
ll ans=0;
cin>>k;
cin>>s;
for(ll a=0;a<k;a++){
    if(mp.find(tolower(s[a]))==mp.end()){
        cout<<-1;
        exit(0);
    }
    if(isupper(s[a])){
        if(mp.find('S')==mp.end()){
            cout<<-1;
            exit(0);
        }
        lld mi=x+1;
        vector<pair<ll,ll>> charac=mp[tolower(s[a])];
        vector<pair<ll,ll>> shift=mp['S'];
        //print(shift);
        //print(charac);
        for(ll q=0;q<charac.size();q++){
            for(ll w=0;w<shift.size();w++){
                lld dist=sqrt(pow((charac[q].first-shift[w].first),2)+pow((charac[q].second-shift[w].second),2));
                //cout<<"dist: "<<dist<<endl;
                if(mi>dist){
                    mi=dist;
                }
            }
        }
        if(mi>x){
            //cout<<"mi: "<<mi<<endl;
            ans++;
        }
    }
}
cout<<ans;
/*
vector<pair<ll,ll>> shift;
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]=='S'){
            shift.push_back({i,j});
        }
    }
}
cin>>k;
cin>>s;
ll ans=0;
for(ll a=0;a<k;a++){
    lld mi=x+1;
    if(check(arr,tolower(s[a]),n,m)){
        if(isupper(s[a])){
            if(shift.size()==0){
                cout<<-1;
                exit(0);
            }
            else{
                lld dist=coor(arr,shift,x,tolower(s[a]),n,m);*/
                /*for(ll b=0;b<shift.size();b++){
                    lld dist=sqrt(pow((shift[b].first-p.first),2)+pow((shift[b].second-p.second),2));
                    //cout<<"dist: "<<dist<<endl;
                    if(mi>dist){
                        mi=dist;
                    }
                }
                if(dist>x){
                    ans++;
                }
            }
        }
    }
    else{
        cout<<-1;
        exit(0);
    }
}

cout<<ans;*/

return 0;
}


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

bool check(vector<string> arr, char ch,ll n,ll m){
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]==ch){
            //cout<<"true\n";
            return true;
        }
    }
}
//cout<<"false\n";
return false;
}
/************************************************************************************************************************/
lld coor(vector<string> arr,vector<pair<ll,ll>> shift,lld x, char ch,ll n,ll m){
lld mi1=x+1;
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]==ch){
            lld mi=x+1;
            pair<ll,ll> p={i,j};
            for(ll b=0;b<shift.size();b++){
                    lld dist=sqrt(pow((shift[b].first-p.first),2)+pow((shift[b].second-p.second),2));
                    //cout<<"dist: "<<dist<<endl;
                    if(mi>dist){
                        mi=dist;
                    }
                }
                if(mi1>mi){
                    mi1=mi;
                }
        }
    }
}
return mi1;
}

/***********************************************************************************************************************/
int main(){
ll n,m,t,k;
lld x;
string s;
cin>>n>>m;
cin>>x;
vector<string> arr;
for(ll i=0;i<n;i++){
    string s;
    cin>>s;
    arr.push_back(s);
}
vector<pair<ll,ll>> shift;
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]=='S'){
            shift.push_back({i,j});
        }
    }
}
cin>>k;
cin>>s;
ll ans=0;
for(ll a=0;a<k;a++){
    lld mi=x+1;
    if(check(arr,tolower(s[a]),n,m)){
        if(isupper(s[a])){
            if(shift.size()==0){
                cout<<-1;
                exit(0);
            }
            else{
                lld dist=coor(arr,shift,x,tolower(s[a]),n,m);
                /*for(ll b=0;b<shift.size();b++){
                    lld dist=sqrt(pow((shift[b].first-p.first),2)+pow((shift[b].second-p.second),2));
                    //cout<<"dist: "<<dist<<endl;
                    if(mi>dist){
                        mi=dist;
                    }
                }*/
                if(dist>x){
                    ans++;
                }
            }
        }
    }
    else{
        cout<<-1;
        exit(0);
    }
}

cout<<ans;
return 0;
}


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

bool check(vector<string> arr, char ch,ll n,ll m){
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]==ch){
            //cout<<"true\n";
            return true;
        }
    }
}
//cout<<"false\n";
return false;
}
pair<ll,ll> coor(vector<string> arr, char ch,ll n,ll m){
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]==ch){
            //cout<<"{ "<<i<<","<<j<<" }\n";
            return {i,j};
        }
    }
}
//cout<<"-1 -1\n";
return {-1,-1};
}
int main(){
ll n,m,t,k;
lld x;
string s;
cin>>n>>m;
cin>>x;
vector<string> arr;
for(ll i=0;i<n;i++){
    string s;
    cin>>s;
    arr.push_back(s);
}
vector<pair<ll,ll>> shift;
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]=='S'){
            shift.push_back({i,j});
        }
    }
}
cin>>k;
cin>>s;
ll ans=0;
for(ll a=0;a<k;a++){
    lld mi=x+1;
    if(check(arr,tolower(s[a]),n,m)){
        if(isupper(s[a])){
            if(shift.size()==0){
                cout<<-1;
                exit(0);
            }
            else{
                pair<ll,ll> p=coor(arr,tolower(s[a]),n,m);
                for(ll b=0;b<shift.size();b++){
                    lld dist=sqrt(pow((shift[b].first-p.first),2)+pow((shift[b].second-p.second),2));
                    //cout<<"dist: "<<dist<<endl;
                    if(mi>dist){
                        mi=dist;
                    }
                }
                if(mi>x){
                    ans++;
                }
            }
        }
    }
    else{
        cout<<-1;
        exit(0);
    }
}

cout<<ans;
return 0;
}


#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <sstream>
using namespace std;
bool can[26];
int n, m, k, q, res = 0;
char key[31][31];
char text[1000000];
vector<pair<int, int>> posKey[26], shift;

int dist(int i, int j, int shr, int shc)
{
	return (i - shr)*(i - shr) + (j - shc)*(j - shc);
}

bool check(int i, int j)
{
	for (int w = 0;w < shift.size();w++)
	{
		int dis = dist(i, j, shift[w].first, shift[w].second);
		if (dis <= k*k)
			return true;
	}
	return false;
}

int main()

{
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 0;i < n; i++)              // keyboard keys
		scanf("%s", key[i]);

	for (int i = 0;i<n;i++)
		for (int j = 0;j < m;j++)
		{
			if (key[i][j] == 'S')
				shift.push_back(make_pair(i, j));                          // position of each key
			else
				posKey[key[i][j] - 'a'].push_back(make_pair(i, j));
		}

	for (int i = 0;i<26; i++)
		for (int j = 0;j <posKey[i].size() && !can[i]; j++)
		{                                                                         //check of each key if can press on it with one hand
			can[i] = can[i] | check(posKey[i][j].first, posKey[i][j].second);
		}

	scanf("%d%s", &q, text);
	for (int i = 0;i < q;i++)
	{
		if (islower(text[i]))
		{
			if (posKey[text[i] - 'a'].empty())
			{
				res = -1;
				break;
			}
		}
		else
		{
			text[i] = tolower(text[i]);

			if (shift.empty() || posKey[text[i] - 'a'].empty())
			{
				res = -1;
				break;

			}

			if (!can[text[i] - 'a'])
				++res;
		}
	}
	printf("%d", res);

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lld;
/*
bool check(vector<string> arr, char ch,ll n,ll m){
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]==ch){
            //cout<<"true\n";
            return true;
        }
    }
}
//cout<<"false\n";
return false;
}
**********************************************************************************************************************
lld coor(vector<string> arr,vector<pair<ll,ll>> shift,lld x, char ch,ll n,ll m){
lld mi1=x+1;
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]==ch){
            lld mi=x+1;
            pair<ll,ll> p={i,j};
            for(ll b=0;b<shift.size();b++){
                    lld dist=sqrt(pow((shift[b].first-p.first),2)+pow((shift[b].second-p.second),2));
                    //cout<<"dist: "<<dist<<endl;
                    if(mi>dist){
                        mi=dist;
                    }
                }
                if(mi1>mi){
                    mi1=mi;
                }
        }
    }
}
return mi1;
}

*********************************************************************************************************************
void print(vector<pair<ll,ll>> arr){
for(ll i=0;i<arr.size();i++){
    cout<<"{ "<<arr[i].first<<","<<arr[i].second<<"} ";
}
cout<<endl;
}
*/

int main(){
ll n,m,k;
lld x;
string s;
cin>>n>>m;
cin>>x;
map<char,vector<pair<ll,ll>>> mp;
for(ll i=0;i<n;i++){
    string s;
    cin>>s;
    for(ll j=0;j<s.length();j++){
        if(mp.find(s[j])==mp.end()){
            vector<pair<ll,ll>> p;
            p.push_back({i,j});
            mp.insert({s[j],p});
        }
        else{
            vector<pair<ll,ll>> p=mp[s[j]];
            p.push_back({i,j});
            mp[s[j]]=p;
        }
    }

}
ll ans=0;
cin>>k;
cin>>s;
vector<pair<ll,ll>> shift=mp['S'];
//print(shift);
for(ll a=0;a<k;a++){
    if(mp.find(tolower(s[a]))==mp.end()){
        cout<<-1;
        exit(0);
    }
    if(isupper(s[a])){
        if(mp.find('S')==mp.end()){
            cout<<-1;
            exit(0);
        }
        lld mi=x+1;
        vector<pair<ll,ll>> charac=mp[tolower(s[a])];
        //print(charac);
        for(ll q=0;q<charac.size();q++){
            for(ll w=0;w<shift.size();w++){
                lld dist=sqrt(pow((charac[q].first-shift[w].first),2)+pow((charac[q].second-shift[w].second),2));
                //cout<<"dist: "<<dist<<endl;
                if(mi>dist){
                    mi=dist;
                }
            }
        }
        if(mi>x){
            //cout<<"mi: "<<mi<<endl;
            ans++;
        }
    }
}
cout<<ans;
/*
vector<pair<ll,ll>> shift;
for(ll i=0;i<n;i++){
    for(ll j=0;j<m;j++){
        if(arr[i][j]=='S'){
            shift.push_back({i,j});
        }
    }
}
cin>>k;
cin>>s;
ll ans=0;
for(ll a=0;a<k;a++){
    lld mi=x+1;
    if(check(arr,tolower(s[a]),n,m)){
        if(isupper(s[a])){
            if(shift.size()==0){
                cout<<-1;
                exit(0);
            }
            else{
                lld dist=coor(arr,shift,x,tolower(s[a]),n,m);*/
                /*for(ll b=0;b<shift.size();b++){
                    lld dist=sqrt(pow((shift[b].first-p.first),2)+pow((shift[b].second-p.second),2));
                    //cout<<"dist: "<<dist<<endl;
                    if(mi>dist){
                        mi=dist;
                    }
                }
                if(dist>x){
                    ans++;
                }
            }
        }
    }
    else{
        cout<<-1;
        exit(0);
    }
}

cout<<ans;*/

return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lld;
/*
void print(vector<pair<ll,ll>> arr){
for(ll i=0;i<arr.size();i++){
    cout<<"{ "<<arr[i].first<<","<<arr[i].second<<"} ";
}
cout<<endl;
}
*/
int main(){
ll n,m,k;
lld x;
string s;
cin>>n>>m;
cin>>x;
map<char,vector<pair<ll,ll>>> mp;
for(ll i=0;i<n;i++){
    string s;
    cin>>s;
    for(ll j=0;j<s.length();j++){
        mp[s[j]].push_back({i,j});
        /*if(mp.find(s[j])==mp.end()){
            vector<pair<ll,ll>> p;
            p.push_back({i,j});
            mp.insert({s[j],p});
        }
        else{
            vector<pair<ll,ll>> p=mp[s[j]];
            p.push_back({i,j});
            mp[s[j]]=p;
        }*/
    }

}
ll ans=0;
cin>>k;
cin>>s;
for(ll a=0;a<k;a++){
    if(mp.find(tolower(s[a]))==mp.end()){
        cout<<-1;
        exit(0);
    }
    if(isupper(s[a])){
        if(mp.find('S')==mp.end()){
            cout<<-1;
            exit(0);
        }
        vector<pair<ll,ll>> charac=mp[tolower(s[a])];
        vector<pair<ll,ll>> shift=mp['S'];
        //print(shift);
        //print(charac);
        ll q,w;
        for(q=0;q<charac.size();q++){
            for(w=0;w<shift.size();w++){
                lld dist=sqrt(pow((charac[q].first-shift[w].first),2)+pow((charac[q].second-shift[w].second),2));
                //cout<<"dist: "<<dist<<endl;
                if(dist<=x){
                    break;
                }
            }
            if(w!=shift.size()){
                break;
            }
        }
        if(q!=charac.size()){
            continue;
        }
        ans++;
    }
}
cout<<ans;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lld;
/*
void print(vector<pair<ll,ll>> arr){
for(ll i=0;i<arr.size();i++){
    cout<<"{ "<<arr[i].first<<","<<arr[i].second<<"} ";
}
cout<<endl;
}
*/
int main(){
ll n,m,k;
lld x;
string s;
cin>>n>>m;
cin>>x;
map<char,vector<pair<ll,ll>>> mp;
for(ll i=0;i<n;i++){
    string s;
    cin>>s;
    for(ll j=0;j<s.length();j++){
        if(mp.find(s[j])==mp.end()){
            vector<pair<ll,ll>> p;
            p.push_back({i,j});
            mp.insert({s[j],p});
        }
        else{
            vector<pair<ll,ll>> p=mp[s[j]];
            p.push_back({i,j});
            mp[s[j]]=p;
        }
    }

}
ll ans=0;
cin>>k;
cin>>s;
for(ll a=0;a<k;a++){
    if(mp.find(tolower(s[a]))==mp.end()){
        cout<<-1;
        exit(0);
    }
    if(isupper(s[a])){
        if(mp.find('S')==mp.end()){
            cout<<-1;
            exit(0);
        }
        vector<pair<ll,ll>> charac=mp[tolower(s[a])];
        vector<pair<ll,ll>> shift=mp['S'];
        //print(shift);
        //print(charac);
        ll q,w;
        for(q=0;q<charac.size();q++){
            for(w=0;w<shift.size();w++){
                lld dist=sqrt(pow((charac[q].first-shift[w].first),2)+pow((charac[q].second-shift[w].second),2));
                //cout<<"dist: "<<dist<<endl;
                if(dist<=x){
                    break;
                }
            }
            if(w!=shift.size()){
                break;
            }
        }
        if(q!=charac.size()){
            continue;
        }
        ans++;
    }
}
cout<<ans;
return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97)
       {

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }

   cout<<c;
   }
   else
    cout<<"-1"<<endl;
   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd,atc;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         {
             atd.push_back(arr[i][j]);
         }
       else
         atc.push_back(arr[i][j]);

   }

   }

   }
   for(int i=0;i<atc.size();i++)
   {
       for(int j=0;j<atd.size();j++)
       {
           if(atc[i]==atd[j])
            atd[j]='$';
       }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0,last=-5;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {   if(last!=i-1)
               c++;

               last=i;

           break;}
       }

       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97)
       {

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }
   if(c!=0)
   cout<<c;
   else
    cout<<"-1";

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0,last=-5;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {   if(last!=i-1)
               c++;

               last=i;

           break;}
       }

       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

cout<<atd[0]<<atd[1]<<atd[2]<<endl;

   int q;
   cin>>q;
   char str[q];
   int c=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97)
       {

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }
   if(c!=0)
   cout<<c;
   else
    cout<<"-1";

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       if(int(str[i])>=97)
       {
           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<n;j++)
               {
                   if(arr[l][j]==str[i])
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1";
               break;
           }

       }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         {
             atd.push_back(arr[i][j]);
         }
         else
         {
             for(int l=0;l<atd.size();l++)
             {
                 if(arr[i][j]==atd[l])
                    atd[l]=='$';
             }
         }
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0,last=-5;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {   if(last!=i-1)
               c++;

               last=i;

           break;}
       }

       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd,atc;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           d=ceil(d);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         {
             atd.push_back(arr[i][j]);
         }
       else
         atc.push_back(arr[i][j]);

   }

   }

   }
   for(int i=0;i<atc.size();i++)
   {
       for(int j=0;j<atd.size();j++)
       {
           if(atc[i]==atd[j])
            atd[j]='$';
       }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0,last=-5;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {   if(last!=i-1)
               c++;

               last=i;

           break;}
       }

       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         {
             atd.push_back(arr[i][j]);
         }
         else
         {
             for(int l=0;l<atd.size();l++)
             {
                 if(arr[i][j]==atd[l])
                    atd[l]='$';
             }
         }
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0,last=-5;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {   if(last!=i-1)
               c++;

               last=i;

           break;}
       }

       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }
   if(c!=0)
   cout<<c;
   else if(ns==0 && cap==0)
    cout<<"0";
   else
    cout<<"-1";

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd,atc;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           d=ceil(d);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         {
             atd.push_back(arr[i][j]);
         }
       else
         atc.push_back(arr[i][j]);

   }

   }

   }
   for(int i=0;i<atc.size();i++)
   {
       for(int j=0;j<atd.size();j++)
       {
           if(atc[i]==atd[j])
            atd[j]='$';
       }

   }

   int q;
   cin>>q;
   char str[q];
   vector<char> result;
   int c=0,cap=0,todo=0,last=-5;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {
               result.push_back(atd[j]);

           break;}
       }

       }
   }
   cout<<endl;
   if(todo==0)
    cout<<result.size();

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97)
       {

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }
   if(c!=0)
   cout<<c;
   else
    cout<<"-1";

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       if(int(str[i])>=97)
       {
           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==str[i])
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0,last=-1;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j] && last!=i-1)
           {c++;
           last=i;
           break;}
       }
       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0,last=-1;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {   if(last!=i-1)
               c++;

               last=i;

           break;}
       }

       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<str[i]<<endl;
           break;
       }
       if(int(str[i])>=97)
       {
           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==str[i])
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<str[i]<<endl;
               break;
           }

       }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;
   for(int k=0;k<ns;k++)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d>x)
           {
               atd.push_back(arr[i][j]);

           }
       }
   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97)
       {

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j])
           {c++;
           break;}
       }
       }
   }
   if(c!=0)
   cout<<c;
   else
    cout<<"-1";

   return 0;

}


#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int main()
{
    int n,m,x;
    cin>>n>>m>>x;

    char arr[n][m];
    vector<int> si,sj;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')
            {
                si.push_back(i);
                sj.push_back(j);
            }
        }

    }

   int ns=sj.size();
   vector<char> atd;

   if(ns!=0)
   {

   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
           int flag=0;
            for(int k=0;k<ns;k++)
           {
           int d1=((i-si[k])*(i-si[k]))+((j-sj[k])*(j-sj[k]));
           float d=sqrt(d1);
           if(d<=x)
           {
              flag=1;

           }
           }

       if(flag==0)
         atd.push_back(arr[i][j]);
   }

   }

   }

   int q;
   cin>>q;
   char str[q];
   int c=0,cap=0,todo=0,last=-1;
   for(int i=0;i<q;i++)
   {
       cin>>str[i];
       if(int(str[i])<97 && ns==0)
       {
           todo=1;
           cout<<"-1"<<endl;
           break;
       }
       char temp;
       if(int(str[i])<97)
         temp=int(str[i]+32);
       else
         temp=str[i];

           int flag=0;
           for(int l=0;l<n;l++)
           {
               for(int j=0;j<m;j++)
               {
                   if(arr[l][j]==temp)
                   {
                       flag=1;
                       break;
                   }
               }

               if(flag==1)
                break;
           }

           if(flag==0)
           {
               todo=1;
               cout<<"-1"<<endl;
               break;
           }

       if(int(str[i])<97)
       {
        cap++;

       char t=int(str[i]+32);

       for(int j=0;j<atd.size();j++)
       {
           if(t==atd[j] && last!=i-1)
           {c++;

           break;}
       }

       last=i;
       }
   }
   if(todo==0)
    cout<<c;

   return 0;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool is_present(string s[],char ch,int n,int m,float& x,float & y){

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]==ch){
                x=i+0.5;
                y=j+0.5;
                return true;
            }
        }
    }
    return false;
}
float calc(float x1,float x2,float y1,float y2){
    float dist=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return sqrt(dist);
}
bool is_present2(string s[],char S,char ch,int n,int m,float& x, float& y,int xx,int& cnt){
    bool ok=false;
    float minm=9999999.9999,dist=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='S'){
                ok=true;
                dist=calc(x,i+0.5,y,j+0.5);
                if(minm>dist){
                    minm=dist;
                }
            }
        }
    }
    if(minm>=(float)xx){
        cnt++;
    }
    return ok;
}
int main()
{
   int n,m,xx;scanf("%d%d%d",&n,&m,&xx);
   string s[n];
   for(int i=0;i<n;i++){
    cin>>s[i];
   }
   bool ok=true;
   int l;scanf("%d",&l);
   string t;cin>>t;
   int cnt=0;
   for(int i=0;i<l;i++){
    char ch=t[i];
    float x,y;
    if(islower(ch)){
        if(!is_present(s,ch,n,m,x,y)){
            ok=false;
            break;
        }
    }
    else{
        char ch2=tolower(ch);
        if(!(is_present(s,ch2,n,m,x,y) && is_present2(s,'S',ch,n,m,x,y,xx,cnt))){
            ok=false;
            break;
        }
    }
    //cout<<x<<" "<<y<<endl;
   }
   if(ok){
    printf("%d",cnt);
   }
   else{
    printf("-1");
   }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool is_present(string s[],char ch,int n,int m,float& x,float & y){

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]==ch){
                x=i+0.5;
                y=j+0.5;
                return true;
            }
        }
    }
    return false;
}
float calc(float x1,float x2,float y1,float y2){
    float dist=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return sqrt(dist);
}
bool is_present2(string s[],char S,char ch,int n,int m,float& x, float& y,int xx,int& cnt){
    bool ok=false;
    float minm=9999999,dist=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='S'){
                ok=true;
                dist=calc(x,i+0.5,y,j+0.5);
                if(minm>dist){
                    minm=dist;
                }
            }
        }
    }
    if(minm>(float)xx){
        cnt++;
    }
    return ok;
}
int main()
{
   int n,m,xx;scanf("%d%d%d",&n,&m,&xx);
   string s[n];
   for(int i=0;i<n;i++){
    cin>>s[i];
   }
   bool ok=true;
   int l;scanf("%d",&l);
   string t;cin>>t;
   int cnt=0;
   for(int i=0;i<l;i++){
    char ch=t[i];
    float x,y;
    if(islower(ch)){
        if(!is_present(s,ch,n,m,x,y)){
            ok=false;
            break;
        }
    }
    else{
        char ch2=tolower(ch);
        if(!(is_present(s,ch2,n,m,x,y) && is_present2(s,'S',ch,n,m,x,y,xx,cnt))){
            ok=false;
            break;
        }
    }
    //cout<<x<<" "<<y<<endl;
   }
   if(ok){
    printf("%d",cnt);
   }
   else{
    printf("-1");
   }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool is_present(string s[],char ch,int n,int m,float& x,float & y){

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]==ch){
                x=i+0.5;
                y=j+0.5;
                return true;
            }
        }
    }
    return false;
}
float calc(float x1,float x2,float y1,float y2){
    float dist=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return sqrt(dist);
}
bool is_present2(string s[],char S,char ch,int n,int m,float& x, float& y,int xx,int& cnt){
    bool ok=false;
    float minm=FLT_MAX,dist=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='S'){
                ok=true;
                dist=calc(x,i+0.5,y,j+0.5);
                if(minm>dist){
                    minm=dist;
                }
            }
        }
    }
    if(minm>xx && ok){
        cnt++;
    }
    return ok;
}
int main()
{
   int n,m,xx;scanf("%d%d%d",&n,&m,&xx);
   string s[n];
   for(int i=0;i<n;i++){
    cin>>s[i];
   }
   bool ok=true;
   int l;scanf("%d",&l);
   string t;cin>>t;
   int cnt=0;
   for(int i=0;i<l;i++){
    char ch=t[i];
    float x,y;
    if(islower(ch)){
        if(!is_present(s,ch,n,m,x,y)){
            ok=false;
            break;
        }
    }
    else{
        char ch2=tolower(ch);
        if(!(is_present(s,ch2,n,m,x,y) && is_present2(s,'S',ch,n,m,x,y,xx,cnt))){
            ok=false;
            break;
        }
    }
    //cout<<x<<" "<<y<<endl;
   }
   if(ok){
    printf("%d",cnt);
   }
   else{
    printf("-1");
   }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool is_present(string s[],char ch,int n,int m,float& x,float & y){

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]==ch){
                x=i+0.5;
                y=j+0.5;
                return true;
            }
        }
    }
    return false;
}
float calc(float x1,float x2,float y1,float y2){
    float dist=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return sqrt(dist);
}
bool is_present2(string s[],char S,char ch,int n,int m,float& x, float& y,int xx,int& cnt){
    bool ok=false;
    float minm=FLT_MAX,dist=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='S'){
                ok=true;
                dist=calc(x,i+0.5,y,j+0.5);
                if(minm>dist){
                    minm=dist;
                }
            }
        }
    }
    if(minm>xx && ok==true){
        cnt++;
    }
    return ok;
}
int main()
{
   int n,m,xx;scanf("%d%d%d",&n,&m,&xx);
   string s[n];
   for(int i=0;i<n;i++){
    cin>>s[i];
   }
   bool ok=true;
   int l;scanf("%d",&l);
   string t;cin>>t;
   int cnt=0;
   for(int i=0;i<l;i++){
    char ch=t[i];
    float x,y;
    if(islower(ch)){
        if(!is_present(s,ch,n,m,x,y)){
            ok=false;
            break;
        }
    }
    else{
        char ch2=tolower(ch);
        if(!(is_present(s,ch2,n,m,x,y) && is_present2(s,'S',ch,n,m,x,y,xx,cnt))){
            ok=false;
            break;
        }
    }
    //cout<<x<<" "<<y<<endl;
   }
   if(ok){
    printf("%d",cnt);
   }
   else{
    printf("-1");
   }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int ok[128]={},appear[128]={};
int calc(int x1,int x2,int y1,int y2){
    int dist=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return dist;
}
int main()
{
   int n,m,x;scanf("%d%d%d",&n,&m,&x);
   string s[n];
   for(int i=0;i<n;i++){
    cin>>s[i];
   }
   int S=0;
   for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
        if(s[i][j]=='S'){
            S=1;
            for(int u=0;u<n;u++){
                for(int v=0;v<m;v++){
                    if(calc(i,u,j,v)<=x*x){
                        ok[(int)s[u][v]]=1;
                    }
                }
            }
        }
        else{
            appear[(int)s[i][j]]=1;
        }
    }
   }
   int f=0,ans=0;
   int l;scanf("%d",&l);
   string t;cin>>t;
    for(int i=0;t[i];i++){
        if(t[i]>='A' && t[i]<='Z'){
            if(!S || !appear[(int)t[i]+32])
            f=1;
            ans+=!ok[(int)t[i]+32];
        }
        else if(!appear[(int)t[i]]){
            f=1;
        }
    }
    if(f){
        printf("-1");
    }
    else{
        printf("%d",ans);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool is_present(string s[],char ch,int n,int m,float& x,float & y){

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]==ch){
                x=i+0.5;
                y=j+0.5;
                return true;
            }
        }
    }
    return false;
}
float calc(float x1,float x2,float y1,float y2){
    float dist=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return sqrt(dist);
}
bool is_present2(string s[],char S,char ch,int n,int m,float& x, float& y,int xx,int& cnt){
    bool ok=false;
    float minm=FLT_MAX,dist=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='S'){
                ok=true;
                dist=calc(x,i+0.5,y,j+0.5);
                if(minm>dist){
                    minm=dist;
                }
            }
        }
    }
    if(minm>(float)xx){
        cnt++;
    }
    return ok;
}
int main()
{
   int n,m,xx;scanf("%d%d%d",&n,&m,&xx);
   string s[n];
   for(int i=0;i<n;i++){
    cin>>s[i];
   }
   bool ok=true;
   int l;scanf("%d",&l);
   string t;cin>>t;
   int cnt=0;
   for(int i=0;i<l;i++){
    char ch=t[i];
    float x,y;
    if(islower(ch)){
        if(!is_present(s,ch,n,m,x,y)){
            ok=false;
            break;
        }
    }
    else{
        char ch2=tolower(ch);
        if(!(is_present(s,ch2,n,m,x,y) && is_present2(s,'S',ch,n,m,x,y,xx,cnt))){
            ok=false;
            break;
        }
    }
    //cout<<x<<" "<<y<<endl;
   }
   if(ok){
    printf("%d",cnt);
   }
   else{
    printf("-1");
   }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int i,j,k,a,b,n,m,x,q,counti=0,flag;
    char S[100][100],T[500001];
    vector < pair<int, int> > d;

    scanf("%d %d %d",&n,&m,&x);
    for(i=0;i<n;i++)
        scanf("%s",S[i]);

    scanf("%d",&q);
    scanf("%s",T);

    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(S[i][j]=='S')
            {
                d.push_back(make_pair(i,j));
            }
        }
    }

    for(i=0;i<q;i++)
    {
        flag=-1;
        for(j=0;j<n&&flag!=1;j++)
        {
            for(k=0;k<m&&flag!=1;k++)
            {
                if(S[j][k]==tolower(T[i]))
                {
                    if(T[i]==tolower(T[i]))
                        flag=1;
                    for(a=0;a<d.size()&&flag!=1;a++)
                    {
                        flag=0;
                        if(x>=sqrt(1.0*( pow(d[a].first-j,2) + pow(d[a].second-k,2) )))
                            flag=1;
                    }
                }
            }
        }
        if(flag==0)
            counti++;
        else if(flag==-1)
            break;
    }

    if(flag!=-1)
        printf("%d",counti);
    else
        printf("-1");
}


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

int main()
{
    int n;
    cin>>n;
    vector<int> v, vs;
    for(int i=0; i<n; i++)
    {
        int p;
        cin>>p;
        v.push_back(p);
    }
    vs = v;
    sort(vs.begin(), vs.end());
    int sumarr1[n], sumarr2[n];
    sumarr1[0] = v[0];
    sumarr2[0] = vs[0];
    for(int i=1; i<n; i++)
    {
        sumarr1[i] = v[i]+sumarr1[i-1];
        sumarr2[i] = vs[i]+sumarr2[i-1];
    }
    int m;
    cin>>m;
    for(int d=0; d<m; d++)
    {
        int t, l, r;
        cin>>t>>l>>r;
        l--;
        r--;
        if(t == 1)
        {
            if(l != 0)
            {
                cout<<sumarr1[r]-sumarr1[l-1]<<endl;
            }
            else
            {
                cout<<sumarr1[r];
            }
        }
        else
        {
            if(l != 0)
            {
                cout<<sumarr2[r]-sumarr2[l-1]<<endl;
            }
            else
            {
                cout<<sumarr2[r];
            }
        }
    }
    return 0;
}


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

int main()
{
    int n;
    cin>>n;
    vector<long long int> v, vs;
    for(int i=0; i<n; i++)
    {
        long long int p;
        cin>>p;
        v.push_back(p);
    }
    vs = v;
    sort(vs.begin(), vs.end());
    long long int sumarr1[n], sumarr2[n];
    sumarr1[0] = v[0];
    sumarr2[0] = vs[0];
    for(int i=1; i<n; i++)
    {
        sumarr1[i] = v[i]+sumarr1[i-1];
        sumarr2[i] = vs[i]+sumarr2[i-1];
    }
    int m;
    cin>>m;
    for(int d=0; d<m; d++)
    {
        int t, l, r;
        cin>>t>>l>>r;
        l--;
        r--;
        if(t == 1)
        {
            if(l != 0)
            {
                cout<<sumarr1[r]-sumarr1[l-1]<<endl;
            }
            else
            {
                cout<<sumarr1[r]<<endl;
            }
        }
        else
        {
            if(l != 0)
            {
                cout<<sumarr2[r]-sumarr2[l-1]<<endl;
            }
            else
            {
                cout<<sumarr2[r]<<endl;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)
	cin>>a[i];
	for(int i=0;i<n;i++){

	b[i]=a[i];
	cout<<b[i]<<endl;}
	int m,type,l,r,ans=0;
	cin>>m;
	while(m--){
		cin>>type>>l>>r;
		int ans=0;
		if(type==1)
		{
			for(int i=l-1;i<r;i++){
				ans+=b[i];
			}
		}
		else if(type==2){
			sort(a,a+n);
			for(int i=l-1;i<r;i++)
				ans+=a[i];
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
long long int a[1000002],b[1000002],c[1000002];
int main(){
	int n;
	cin>>n;

	 b[0]=0,c[0]=0;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	for(int i=1;i<=n;i++){
		b[i]=b[i-1]+a[i];
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
		c[i]=c[i-1]+a[i];
	int m,type,l,r;
	cin>>m;
	while(m--){
		cin>>type>>l>>r;
		if(type==1)
		cout<<b[r]-b[l-1]<<endl;
		else if(type==2)
		cout<<c[r]-c[l-1]<<endl;
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)
	cin>>a[i];
	for(int i=0;i<n;i++){

	b[i]=a[i];
	}
	int m,type,l,r,ans=0;
	cin>>m;
	while(m--){
		cin>>type>>l>>r;
		int ans=0;
		if(type==1)
		{
			for(int i=l-1;i<r;i++){
				ans+=b[i];
			}
		}
		else if(type==2){
			sort(a,a+n);
			for(int i=l-1;i<r;i++)
				ans+=a[i];
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
long long int a[10002],b[10002],c[10002];
int main(){
	int n;
	cin>>n;

	 b[0]=0,c[0]=0;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	for(int i=1;i<=n;i++){
		b[i]=b[i-1]+a[i];
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
		c[i]=c[i-1]+a[i];
	int m,type,l,r;
	cin>>m;
	while(m--){
		cin>>type>>l>>r;
		if(type==1)
		cout<<b[r]-b[l-1]<<endl;
		else if(type==2)
		cout<<c[r]-c[l-1]<<endl;
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++)
	cin>>a[i];
	int m,type,l,r,ans=0;
	cin>>m;
	while(m--){
		cin>>type>>l>>r;
		int ans=0;
		if(type==1)
		{
			for(int i=l-1;i<r;i++){
				ans+=a[i];
			}
		}
		else if(type==2){
			sort(a,a+n);
			for(int i=l-1;i<r;i++)
				ans+=a[i];
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
long long int a[1000002],b[1000002];
int main(){
	int n;
	cin>>n;
//	int a[n],b[n];
	for(int i=0;i<n;i++)
	cin>>a[i];
	for(int i=0;i<n;i++){

	b[i]=a[i];
	}
	int m,type,l,r,ans=0;
	cin>>m;
	while(m--){
		cin>>type>>l>>r;
		int ans=0;
		if(type==1)
		{
			for(int i=l-1;i<r;i++){
				ans+=b[i];
			}
		}
		else if(type==2){
			sort(a,a+n);
			for(int i=l-1;i<r;i++)
				ans+=a[i];
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n+2],b[n+2],c[n+2];
	 b[0]=0,c[0]=0;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	for(int i=1;i<=n;i++){
		b[i]=b[i-1]+a[i];
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++)
		c[i]=c[i-1]+a[i];
	int m,type,l,r;
	cin>>m;
	while(m--){
		cin>>type>>l>>r;
		if(type==1)
		cout<<b[r]-b[l-1]<<endl;
		else if(type==2)
		cout<<c[r]-c[l-1]<<endl;
	}
}


#include <iostream>
#include<algorithm>
using namespace std;
int arr[100001];
long long cum1[100001],cum2[100001];
int main()
{
    int n,m,t,r,l;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
        cum1[i]=cum1[i-1]+arr[i];
    }
    sort(arr+1,arr+n+1);
    for(int i=1;i<=n;i++)
    {
        cum2[i]=cum2[i-1]+arr[i];
    }
cin>>m;
    while(m--)
    {
        cin>>t>>l>>r;
        if(t==1)
        {
            cout<<cum1[r]-cum1[l-1]<<endl;
        }
        else
        {
            cout<<cum2[r]-cum2[l-1]<<endl;
        }
    }
    return 0;
}


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

int main()
{
    int nu[100001],ok[100001];
    int n;
    cin>>n;
    for(int x=1;x<=n;x++)
    {
        cin>>nu[x];
        ok[x]=nu[x];
    }
    sort(ok,ok+n);
    long long prifnu[100001],prifok[100001];
    for(int i=1;i<=n;i++)
    {
        prifnu[i]=nu[i]+prifnu[i-1];
        prifok[i]=ok[i]+prifok[i-1];
    }
    int m;
    cin>>m;
    for(int x=0;x<m;x++)
    {
        int t,l,r;
        cin>>t>>l>>r;
        if(t==1)
        {
            cout<<prifnu[r]-prifnu[l-1];
        }
        else
        {
            cout<<prifok[r]-prifok[l-1];
        }
    }
    return 0;
}


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

int main()
{
    int nu[100001],ok[100001];
    int n;
    cin>>n;
    for(int x=1;x<=n;x++)
    {
        cin>>nu[x];
        ok[x]=nu[x];
    }
    sort(ok+1,ok+n+1);
    int prifnu[100001]={},prifok[100001]={};
    for(int i=1;i<=n;i++)
    {
        prifnu[i]=nu[i]+prifnu[i-1];
        prifok[i]=ok[i]+prifok[i-1];
    }
    int m;
    cin>>m;
    for(int x=0;x<m;x++)
    {
        int t,l,r;
        cin>>t>>l>>r;
        if(t==1)
        {
            cout<<prifnu[r]-prifnu[l-1];
        }
        else
        {
            cout<<prifok[r]-prifok[l-1];
        }
    }
    return 0;
}


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

int main()
{
    int nu[100001],ok[100001];
    int n;
    cin>>n;
    for(int x=1;x<=n;x++)
    {
        cin>>nu[x];
        ok[x]=nu[x];
    }
    sort(ok,ok+n);
    int prifnu[100001],prifok[100001];
    for(int i=1;i<=n;i++)
    {
        prifnu[i]=nu[i]+prifnu[i-1];
        prifok[i]=ok[i]+prifok[i-1];
    }
    int m;
    cin>>m;
    for(int x=0;x<n;x++)
    {
        int t,l,r;
        cin>>t>>l>>r;
        if(t==1)
        {
            cout<<prifnu[r]-prifnu[l-1];
        }
        else
        {
            cout<<prifok[r]-prifok[l-1];
        }
    }
    return 0;
}


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

int main()
{
    int nu[100001],ok[100001];
    int n;
    cin>>n;
    for(int x=1;x<=n;x++)
    {
        cin>>nu[x];
        ok[x]=nu[x];
    }
    sort(ok,ok+n);
    long long prifnu[100001]={},prifok[100001]={};
    for(int i=1;i<=n;i++)
    {
        prifnu[i]=nu[i]+prifnu[i-1];
        prifok[i]=ok[i]+prifok[i-1];
    }
    int m;
    cin>>m;
    for(int x=0;x<m;x++)
    {
        int t,l,r;
        cin>>t>>l>>r;
        if(t==1)
        {
            cout<<prifnu[r]-prifnu[l-1];
        }
        else
        {
            cout<<prifok[r]-prifok[l-1];
        }
    }
    return 0;
}


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

int main()
{
    int nu[100001],ok[100001];
    int n;
    cin>>n;
    for(int x=1;x<=n;x++)
    {
        cin>>nu[x];
        ok[x]=nu[x];
    }
    sort(ok,ok+n);
    int prifnu[100001],prifok[100001];
    for(int i=1;i<=n;i++)
    {
        prifnu[i]=nu[i]+prifnu[i-1];
        prifok[i]=ok[i]+prifok[i-1];
    }
    int m;
    cin>>m;
    for(int x=0;x<m;x++)
    {
        int t,l,r;
        cin>>t>>l>>r;
        if(t==1)
        {
            cout<<prifnu[r]-prifnu[l-1];
        }
        else
        {
            cout<<prifok[r]-prifok[l-1];
        }
    }
    return 0;
}


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

int main()
{
    int nu[100001],ok[100001];
    int n;
    cin>>n;
    for(int x=1;x<=n;x++)
    {
        cin>>nu[x];
        ok[x]=nu[x];
    }
    sort(ok,ok+n);
    int prifnu[100001]={},prifok[100001]={};
    for(int i=1;i<=n;i++)
    {
        prifnu[i]=nu[i]+prifnu[i-1];
        prifok[i]=ok[i]+prifok[i-1];
    }
    int m;
    cin>>m;
    for(int x=0;x<m;x++)
    {
        int t,l,r;
        cin>>t>>l>>r;
        if(t==1)
        {
            cout<<prifnu[r]-prifnu[l-1];
        }
        else
        {
            cout<<prifok[r]-prifok[l-1];
        }
    }
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main(){

	int A[100001], B[100001];
	long long cmA[100001]={}, cmB[100001] = {};
	int  n;
	cin >> n;
	for (int i = 1; i <= n; i++){
		cin >> A[i];
		B[i] = A[i];
	}
	sort(B+1 , B + n + 1);
	for (int i = 1; i <= n; i++){
		cmA[i] = A[i] + cmA[i - 1];

		cmB[i] = B[i] + cmB[i - 1];
	}

	int  m;
	cin >> m;
	for (int i = 0; i < m; i++){
		int t, l, r;
		cin >> t >> l >> r;
		if (t == 1){
			cout << cmA[r] - cmA[l - 1] << endl;
		}
		else{
			cout << cmB[r] - cmB[l - 1] << endl;

		}

	}
	return 0;
}


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

int main()
{
    int n;
    cin>>n;
    int i;
    int a[n],b[n];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b,b+n);
    int s[n+1],s1[n+1];
    s[0]=0;
    s1[0]=0;
    s[1]=a[0];
    s1[1]=b[0];
    for(i=1;i<n;i++){
        s[i+1]=a[i]+s[i];
        s1[i+1]=b[i]+s1[i];
    }
    int m;
    cin>>m;
    int x,l,r;
    ll sum;
    while(m--)
    {
        cin>>x>>l>>r;
        if(x==1)
        {
            cout<<s[r]-s[l-1]<<"\n";
        }
        else
        {
            cout<<s1[r]-s1[l-1]<<"\n";
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    int n;
    cin>>n;
    int i;
    int a[n],b[n];
    for(i=1;i<n;i++)
    {
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b,b+n);
    int s[n+1],s1[n+1];
    s[0]=0;
    s1[0]=0;
    s[1]=a[0];
    s1[1]=b[0];
    for(i=1;i<n;i++){
        s[i+1]=a[i]+s[i];
        s1[i+1]=b[i]+s1[i];
    }
    int m;
    cin>>m;
    int x,l,r;
    ll sum;
    while(m--)
    {
        cin>>x>>l>>r;
        if(x==1)
        {
            cout<<s[r]-s[l-1]<<"\n";
        }
        else
        {
            cout<<s1[r]-s1[l-1]<<"\n";
        }
    }
}


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

int main()
{
    int n;
    cin>>n;
    int i;
    ll a[n],b[n];
    for(i=0;i<n;i++)
        {cin>>a[i];
        b[i]=a[i];
        }
    sort(b,b+n);
    int j,m;
    cin>>m;
    int x;
    ll y,z;
    for(j=0;j<m;j++)
    {
        cin>>x;
        cin>>y>>z;
        if(x==1)
        {
            ll sum=0;
            for(i=y-1;i<z;i++)
            {
                sum+=a[i];
            }
            cout<<sum<<"\n";
        }
        else
        {
            ll sum=0;
            for(i=y-1;i<z;i++)
            {
                sum+=b[i];
            }
            cout<<sum<<"\n";
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    int n;
    cin>>n;
    int i;
    int s[n+2],s1[n+2];
    int a[n+1];
    s[0]=0;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
        s[i]=s[i-1]+a[i];
    }
    sort(a,a+n);
    s1[0]=0;
    for(i=1;i<=n;i++){
       s1[i]=s1[i-1]+a[i];
    }
    int m;
    cin>>m;
    int x,l,r;
    while(m--)
    {
        cin>>x>>l>>r;
        if(x==1)
        {
            cout<<s[r]-s[l-1]<<"\n";
        }
        else
        {
            cout<<s1[r]-s1[l-1]<<"\n";
        }
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
vector<long long> sum( vector<long long>arr){
     vector<long long> s (arr.size(),0);
    for (long long i = 1;i<arr.size();i++){
        s[i]= s[i-1]+arr[i];
    }
    return s;
}
long long first ( vector<long long> s,long long l,long long r){
    long long ans;
    ans = s[r]-s[l-1];
    return ans;
}

int main()
{
    long long n,m,type,l,r,t;
     scanf ("%lld",&n);
    vector<long long> arr(n+1,0);
    vector<long long> temp_arr(n+1,0);

    for (long long i=1;i<=n;i++){
        scanf ("%lld",&arr[i]);
    }
   vector<long long> s;
   temp_arr= arr;
   scanf ("%lld",&m);
    for (long long i=0;i<m;i++){
        scanf ("%lld %lld %lld",&type,&l,&r);
        if (type!=1){
            sort(temp_arr.begin(), temp_arr.end());
            s= sum (temp_arr);
        }else{
           s= sum (arr);
        }

        printf("%lld\n",first (s,l,r));
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int divisor (int x){
    int cnt=0;
    for (int i=1;i<=x;i++ ){
        if (x%i==0){
            cnt++;
            cout <<i<<endl;
        }
    }
    if (sqrt(x)*sqrt(x)!=x&&cnt>1){
           cnt++;
        }
    return cnt;
}
void zeros ( vector<int> arr){
    for (int i =0;i<(int)arr.size();i++){
        arr.push_back(0);
    }
}
vector<long long> sum( vector<long long>arr){
     vector<long long> s (arr.size(),0);
    for (long long i = 1;i<arr.size();i++){
        s[i]= s[i-1]+arr[i];
    }
    return s;
}
int first ( vector<long long> s,long long l,long long r){
    long long ans;
    ans = s[r]-s[l-1];
    return ans;
}

int main()
{
    long long n,m,type,l,r,t;
    cin>>n;
    vector<long long> arr(n+1,0);
    vector<long long> temp_arr(n+1,0);
    for (long long i=1;i<=n;i++){
        cin>>t;
        arr[i]=t;
    }
   vector<long long> s;
   temp_arr= arr;
    cin>>m;
    for (long long i=0;i<m;i++){
        cin>>type>>l>>r;
        if (type!=1){
            sort(temp_arr.begin(), temp_arr.end());
            s= sum (temp_arr);
        }else{
           s= sum (arr);
        }

        cout<<first (s,l,r)<<endl;
    }
    return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="prefix sum1" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/prefix sum1" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/prefix sum1" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<lib_finder disable_auto="1" />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


#include <iostream>

using namespace std;

int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    while (a<c){
        a = a + b;
        if (a>c){
            a= c;
            break;
        }
        c = c - d;
    }
    cout<<a;
    return 0;
}


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

int main()
{
    long long n,m,type,l,r,t;
    scanf ("%lld",&n);
    long long arr [n+1];

    long long first [n+1];
    long long second [n+1];

    for (int i =1;i<n+1;i++){
        scanf ("%lld",&arr[i]);
        first [i] = first[i-1]+arr[i];
    }
    sort(arr+1,arr+n+1);
    for (int i =1 ;i< n+1; i++){
        second [i] = second[i-1]+arr[i];
    }

    scanf ("%lld",&m);
    for (int i =0;i<m;i++){
        scanf ("%lld %lld %lld",&type,&l,&r);
        if (type==1){
            printf("%lld\n",first[r]-first[l-1]);
        }else{
            printf("%lld\n",second[r]-second[l-1]);
        }
    }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int divisor (int x){
    int cnt=0;
    for (int i=1;i<=x;i++ ){
        if (x%i==0){
            cnt++;
            cout <<i<<endl;
        }
    }
    if (sqrt(x)*sqrt(x)!=x&&cnt>1){
           cnt++;
        }
    return cnt;
}
void zeros ( vector<int> arr){
    for (int i =0;i<(int)arr.size();i++){
        arr.push_back(0);
    }
}
vector<long long> sum( vector<long long>arr){
     vector<long long> s (arr.size(),0);
    for (long long i = 1;i<arr.size();i++){
        s[i]= s[i-1]+arr[i];
    }
    return s;
}
long long first ( vector<long long> s,long long l,long long r){
    long long ans;
    ans = s[r]-s[l-1];
    return ans;
}

int main()
{
    long long n,m,type,l,r,t;
    cin>>n;
    vector<long long> arr(n+1,0);
    vector<long long> temp_arr(n+1,0);
    for (long long i=1;i<=n;i++){
        cin>>t;
        arr[i]=t;
    }
   vector<long long> s;
   temp_arr= arr;
    cin>>m;
    for (long long i=0;i<m;i++){
        cin>>type>>l>>r;
        if (type!=1){
            sort(temp_arr.begin(), temp_arr.end());
            s= sum (temp_arr);
        }else{
           s= sum (arr);
        }

        cout<<first (s,l,r)<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,l,q,r;
int arr[1000009] = {0};
int s1[1000009]  = {0};
int s2[1000009]  = {0};
int main()
{

    int x;
    scanf("%d",&n);
    ++n;
    for(int i=1;i<n;++i)
        scanf("%d",&arr[i]);

    for(int i=1;i<n;++i) s1[i] += arr[i] + s1[i-1];

    sort(arr,arr+n);
    for(int i=1;i<n;++i) s2[i] += arr[i] + s2[i-1];
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d %d %d",&q,&l,&r);
        if(q == 1) printf("%d\n", (s1[r] - s1[l-1]));
        else printf("%d\n", (s2[r] - s2[l-1]));
    }

    return 0;
}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
int arr[100000], cons[100000], ar[100000];
using namespace std;
int main() {
	int x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	for (int i = 1; i <= n; i++)
		ar[i]= arr[i];
		cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			sort(ar, ar + (n+1));
			cons[1] = ar[1];
			for (int j = 2; j <= n; j++)
				cons[j] = cons[j - 1] + ar[j];
			/*for (int j = 1; j <= n; j++)
				cout << ar[j] << " ";
			cout << endl;
			for (int j = 1; j <= n; j++)
				cout << cons[j] << " ";
			cout << endl;
			*/
			cout << cons[r] - cons[l - 1] << endl;
		}
		else
		{
			cons[1] = arr[1];
			for (int j = 2; j <= n; j++)
			{
				cons[j] = cons[j - 1] + arr[j];
			}
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;

}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
 long long arr[100000], cons[100000],con[100000];
 long long ar[100000];
using namespace std;
int main() {
	long long x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];

	cons[1] = arr[1];
	for (int j = 2; j <= n; j++)
		cons[j] = cons[j - 1] + arr[j];

	for (int i = 1; i <= n; i++)
		ar[i]= arr[i];
	sort(ar, ar + (n + 1));
	con[1] = ar[1];
	for (int j = 2; j <= n; j++)
		con[j] = con[j - 1] + ar[j];
		cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			cout << con[r] - con[l - 1] << endl;
		}
		else
		{
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;

}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
using namespace std;
 long long  cons[100000],con[100000];
 int ar[100000], arr[100000];
int main() {
	long long x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	cons[1] = arr[1];
	for (int j = 2; j <= n; j++)
		cons[j] = cons[j - 1] + arr[j];

	for (int i = 1; i <= n; i++)
		ar[i]= arr[i];

	sort(ar, ar + (n + 1));
	con[1] = ar[1];
	for (int j = 2; j <= n; j++)
		con[j] = con[j - 1] + ar[j];

		cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			cout << con[r] - con[l - 1] << endl;
		}
		else
		{
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;

}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
unsigned long long arr[100000], cons[100000],con[100000];
unsigned long long ar[100000];
using namespace std;
int main() {
	int x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];

	cons[1] = arr[1];
	for (int j = 2; j <= n; j++)
		cons[j] = cons[j - 1] + arr[j];

	for (int i = 1; i <= n; i++)
		ar[i]= arr[i];
	sort(ar, ar + (n + 1));
	con[1] = ar[1];
	for (int j = 2; j <= n; j++)
		con[j] = con[j - 1] + ar[j];
		cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			cout << con[r] - con[l - 1] << endl;
		}
		else
		{
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;

}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
using namespace std;
long long  cons[100000], con[1000000];
int ar[1000000], arr[1000000];
int main() {
	long long x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	cons[1] = arr[1];
	for (int j = 2; j <= n; j++)
		cons[j] = cons[j - 1] + arr[j];

	for (int i = 1; i <= n; i++)
		ar[i] = arr[i];

	sort(ar, ar + (n + 1));
	con[1] = ar[1];
	for (int j = 2; j <= n; j++)
		con[j] = con[j - 1] + ar[j];

	cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			cout << con[r] - con[l - 1] << endl;
		}
		else
		{
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;
	return 0;
}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
int arr[100000], cons[100000], ar[100000];
using namespace std;
int main() {
	int x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	for (int i = 1; i <= n; i++)
		ar[i]= arr[i];
		cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			sort(ar, ar + n);
			cons[1] = ar[1];
			for (int j = 2; j <= n; j++)
				cons[j] = cons[j - 1] + ar[j];
			cout << cons[r] - cons[l - 1] << endl;
		}
		else
		{
			cons[1] = arr[1];
			for (int j = 2; j <= n; j++)
			{
				cons[j] = cons[j - 1] + arr[j];
			}
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;

}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
unsigned long long arr[100000], cons[100000],con[100000];
unsigned long long ar[100000];
using namespace std;
int main() {
	long long x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];

	cons[1] = arr[1];
	for (int j = 2; j <= n; j++)
		cons[j] = cons[j - 1] + arr[j];

	for (int i = 1; i <= n; i++)
		ar[i]= arr[i];
	sort(ar, ar + (n + 1));
	con[1] = ar[1];
	for (int j = 2; j <= n; j++)
		con[j] = con[j - 1] + ar[j];
		cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			cout << con[r] - con[l - 1] << endl;
		}
		else
		{
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;

}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
int arr[100000], cons[100000];
long long ar[100000];
using namespace std;
int main() {
	int x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	for (int i = 1; i <= n; i++)
		ar[i]= arr[i];
		cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			sort(ar, ar + (n+1));
			cons[1] = ar[1];
			for (int j = 2; j <= n; j++)
				cons[j] = cons[j - 1] + ar[j];
			/*for (int j = 1; j <= n; j++)
				cout << ar[j] << " ";
			cout << endl;
			for (int j = 1; j <= n; j++)
				cout << cons[j] << " ";
			cout << endl;
			*/
			cout << cons[r] - cons[l - 1] << endl;
		}
		else
		{
			cons[1] = arr[1];
			for (int j = 2; j <= n; j++)
			{
				cons[j] = cons[j - 1] + arr[j];
			}
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;

}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
unsigned long long arr[100000], cons[100000];
unsigned long long ar[100000];
using namespace std;
int main() {
	int x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	for (int i = 1; i <= n; i++)
		ar[i]= arr[i];
		cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			sort(ar, ar + (n+1));
			cons[1] = ar[1];
			for (int j = 2; j <= n; j++)
				cons[j] = cons[j - 1] + ar[j];
			/*for (int j = 1; j <= n; j++)
				cout << ar[j] << " ";
			cout << endl;
			for (int j = 1; j <= n; j++)
				cout << cons[j] << " ";
			cout << endl;
			*/
			cout << cons[r] - cons[l - 1] << endl;
		}
		else
		{
			cons[1] = arr[1];
			for (int j = 2; j <= n; j++)
			{
				cons[j] = cons[j - 1] + arr[j];
			}
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;

}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iomanip>
int arr[100000], cons[100000], ar[100000];
using namespace std;
int main() {
	int x, n, m, l, r, t;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	for (int i = 1; i <= n; i++)
		ar[i]= arr[i];
		cin >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> t >> l >> r;
		if (t == 2)
		{
			sort(ar, ar + n);
			cons[1] = ar[1];
			for (int j = 2; j <= n; j++)
				cons[j] = cons[j - 1] + ar[j];
			/*for (int j = 1; j <= n; j++)
				cout << ar[j] << " ";
			cout << endl;
			for (int j = 1; j <= n; j++)
				cout << cons[j] << " ";
			cout << endl;
			*/
			cout << cons[r] - cons[l - 1] << endl;
		}
		else
		{
			cons[1] = arr[1];
			for (int j = 2; j <= n; j++)
			{
				cons[j] = cons[j - 1] + arr[j];
			}
			cout << cons[r] - cons[l - 1] << endl;
		}
	}
	cin >> x;

}


// THIS IS WRITTEN BY ME YAD YA FANLA
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string >
#include<algorithm>
#include<deque>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;

#define ll long long
#define ull unsigned long long
int main ()
{
	std::ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		ll n,arr[100000]={0},freq1[10000000]={0},freq2[10000000]={0},sum =0;
		cin>>n;
		for (ll i=1 ;i<=n ;i++)
		{
		cin>>arr[i];
		sum+=arr[i];
		freq1[i]=sum;
		}
	    	sum=0;
	    sort (arr,arr+n+1);
		for (ll i=1; i<=n ;i++)
		{
		sum+=arr[i];
		freq2[i]=sum;
		}
		   ll q;
		   cin>>q;
		   for (ll j=1 ;j<=q ;j++)
		   {
		       ll t,x,y,mx=0,mn=0;
		       cin>>t >> x >>y;
		       if (t == 1)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		           cout<< freq1[mx] - freq1[mn-1]<<endl;
		       }
		      else  if (t == 2)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		            cout<< freq2[mx] - freq2[mn-1] << endl;
		       }
		   }
			//system("pause");
	return 0;

}


// THIS IS WRITTEN BY ME YAD YA FANLA
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string >
#include<algorithm>
#include<deque>
#include<vector>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;

#define ll long long
#define ull unsigned long long
int main ()
{
	std::ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		ll n;
		cin>>n;
		vector <ll>arr1(n+1);
		vector <ll>arr2(n+1);
		arr1[0]=0;
		arr2[0]=0;
		for (ll i=1 ;i<=n ;i++)
		{
		cin>>arr1[i];
		arr2[i]=arr1[i];
		}
		sort (arr2.begin(),arr2.end());
		for (ll i=1 ;i<=n ;i++)
		{
		arr1[i] = arr1[i] + arr1[i-1];
		arr2[i] = arr2[i] + arr2[i-1];
		}
		 ll q;
		 cin >> q;
		 for (ll j=1 ;j<=q ;j++)
		 {
		 ll t,x,y;
		 cin>>t>>x>>y;
		 if (t == 1 )
			 cout << arr1[y] - arr1[x-1]<<endl;
		 else if (t == 2)
			 cout << arr2[y] - arr2[x-1] << endl;
		 }

			//system("pause");
	return 0;

}


// THIS IS WRITTEN BY ME YAD YA FANLA
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<queue>
#include<cmath>
#include<string >
#include<algorithm>
#include<deque>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;

#define ll long long
#define ull unsigned long long
int main ()
{
	std::ios_base::sync_with_stdio(0);
		cin.tie(NULL);
		ll n,arr[100000]={0},freq1[100000000]={0},freq2[100000000]={0},sum =0;
		cin>>n;
		for (ll i=1 ;i<=n ;i++)
		{
		cin>>arr[i];
		sum+=arr[i];
		freq1[i]=sum;
		}
	    	sum=0;
	    sort (arr,arr+n+1);
		for (ll i=1; i<=n ;i++)
		{
		sum+=arr[i];
		freq2[i]=sum;
		}
		   ll q;
		   cin>>q;
		   for (ll j=1 ;j<=q ;j++)
		   {
		       ll t,x,y,mx=0,mn=0;
		       cin>>t >> x >>y;
		       if (t == 1)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		           cout<< freq1[mx] - freq1[mn-1]<<endl;
		       }
		      else  if (t == 2)
		       {
		           mx=max(x,y);
		           mn=min(x,y);
		            cout<< freq2[mx] - freq2[mn-1] << endl;
		       }
		   }
			//system("pause");
	return 0;

}