//#include <numeric>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <deque>
#include <string>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <time.h>
#include <bitset>
#include <list>
#include <limits>
using namespace std;

typedef long long ll;//don't write as long!!!
typedef unsigned long long ull;
typedef map<int,int> mii;

double EPS = 1e-9;
int INF = 0x7FFFFFFF;//numeric_limits<int>::max();
long long INFF = 0x7FFFFFFFFFFFFFFF;//numeric_limits<long long>::max();
double PI = acos(-1.0);

#define SIZE(v) (int)v.size()
#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))

//all zero
#define REP(i,n) FOR(i,0,n)
#define REPS(i,s) for(int i = 0;s[i]!='\0';++i)
#define REPV(i,v) FOR(i,0,SIZE(v))
#define REPN(i,n) FORN(i,0,n)
#define REPD(i,n) FORD(i,n,0)
#define REPI(t,v,i) for(t::iterator i=v.begin();i!=v.end();i++)
#define REPI2(t1,t2,v,i) for(t1,t2::iterator i=v.begin();i!=v.end();i++)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define FIND(m,i) m.find(i)!=m.end()
#define UNFIND(m,i) !(FIND(m,i))

#pragma region Functions

inline void scanf(int &d1){scanf("%d",&d1);}
inline void scanf(int &d1,int &d2){scanf("%d%d",&d1,&d2);}
inline void scanf(int &d1,int &d2,int &d3){scanf("%d%d%d",&d1,&d2,&d3);}
inline void scanf(ll &d1){scanf("%I64d",&d1);}
inline void scanf(ll &d1,ll &d2){scanf("%I64d%I64d",&d1,&d2);}
inline void scanf(ll &d1,ll &d2,ll &d3){scanf("%I64d%I64d%I64d",&d1,&d2,&d3);}
inline void scanf(double &d1){scanf("%lf",&d1);}
inline void scanf(char *temp){scanf("%s",temp);}

inline void printf(int d1){printf("%d\n",d1);}
inline void printf(int d1,int d2){printf("%d %d\n",d1,d2);}
inline void printf(double f1){printf("%.17g\n",f1);}
inline void printf(ll l1){printf("%I64d\n",l1);}
inline void printf(ll l1,ll l2){printf("%I64d %I64d\n",l1,l2);}
inline void printfcase(int id){printf("Case #%d:",id);}

inline string tostring(int a){char x[100];sprintf(x,"%d",a); string s = x;return s;}
inline void toupperstring(string &s){REPV(i,s){if(s[i]>='a'&&s[i]<='z'){s[i]=s[i]-'a'+'A';}}}//transform(s.begin(),s.end(),s.begin(),::toupper);
inline void tolowerstring(string &s){REPV(i,s){if(s[i]>='A'&&s[i]<='Z'){s[i]=s[i]-'A'+'a';}}}//transform(s.begin(),s.end(),s.begin(),::tolower);
inline int toint(string a){char x[100];int res;strcpy(x,a.c_str());sscanf(x,"%d",&res);return res;}

inline void freopen(string sin,string sout){freopen(sin.c_str(),"r",stdin);freopen(sout.c_str(),"w",stdout);}

template<typename T1, typename T2,typename T>
inline typename T::iterator findorinsert(T &m,const T1& t1,const T2 & t2){
	typename T::iterator iter = m.find(t1);
	if(iter == m.end()){m.insert(mp(t1,t2));return m.find(t1);}
	else{return iter;}
}

template<typename T>
inline T gcd(T u, T v){while(v != 0){T r = u%v;u = v;v = r;}return u;}

#pragma endregion

#pragma region Structs & Classes

template<typename T,int n>
struct Vector{
	T data[n];
	void read(){REP(i,n){cin>>data[i];}}
	inline T operator*(const Vector<T,n> &v) const{
		T res = 0;REP(i,n){res+=data[i]*v[i];}return res;
	}
	inline Vector<T,n> operator+(const Vector<T,n> &v) const{
		Vector<T,n> res;REP(i,n){res[i]=data[i]+v[i];}return res;
	}
	inline Vector<T,n> operator-(const Vector<T,n> &v) const{
		Vector<T,n> res;REP(i,n){res[i]=data[i]-v[i];}return res;
	}
	inline bool operator<(const Vector<T,n> &v) const{
		REP(i,n){if(data[i]!=v[i]){return data[i]<v[i];}}return false;
	}
	inline bool operator==(const Vector<T,n> &v) const{
		REP(i,n){if(data[i]!=v[i]){return false;}}return true;
	}
	inline T& operator[](int id){return data[id];}
	inline T operator[](int id) const{return data[id];}
	inline int size()const{return n;}
};

typedef Vector<int,2> Vi2;
template<typename T>
inline Vector<T,2> make_vector(const T &t1,const T &t2){Vector<T,2> res = {t1,t2};return res;}
template<typename T>
inline Vector<T,3> make_vector(const T &t1,const T &t2,const T &t3){Vector<T,3> res = {t1,t2,t3};return res;}
#define mv make_vector
Vi2 dirs[4] = {{0,1},{0,-1},{1,0},{-1,0}};//y,x

template<int n,int m>//2n/lgn
class Prime{
public:
	Prime(int num){sieve();collect();}
	int find1factor(int num) const{
		REP(i,results_size){if(num%results[i] == 0){return results[i];}}return num;
	}
	bool bits[n+1];int results[m],result_size;
private:
	void sieve(){
		RESET(bits,true);bits[0] = false;bits[1] = false;
		for(int i = 2;i <= n;++i){
			if(bits[i]){
				for(int j = i+i;j <= n;j+=i){bits[j] = false;}
			}
		}
	}
	void collect(){
		for(int i = 2;i <= n;++i){if(bits[i]){results[result_size]=i;result_size++}}
	}
};

template<int n,int m>
struct Graph{
	int head[n],edge_id;
	struct{
		int to,next;
	}edges[m];
	void init(int num = n){edge_id = 0;memset(head,-1,sizeof(int)*num);}//!!
	int add(int from,int to){
		edges[edge_id].next = head[from];edges[edge_id].to = to;
		head[from] = edge_id;edge_id++;return head[from];
	}
	inline int next(int edge) const{return edges[edge].next;}
	inline int to(int edge) const{return edges[edge].to;}
};

#define REPG(g,id,e) for(int e = g.head[id];e != -1;e = g.next(e))

#pragma endregion

const int max_n = 200000;
int n,m;
Vi2 data[max_n];

int main(){
	scanf(n,m);
	int last = -1,startpos = -1;
	REP(i,n){
		scanf(data[i][0]);
		if(data[i][0]==last){
			data[i][1] = startpos;
		}
		else{
			last = data[i][0];
			startpos = i;
		}
	}
	REP(i,m){
		int l,r,x;
		scanf(l,r,x);
		l--;
		r--;
		bool find = false;
		while(r>=l){
			if(data[r][0]!=x){
				printf(r+1);
				find = true;
				break;
			}
			else{
				r = data[r][1]-1;
			}
		}
		if(!find){
			printf(-1);
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	int  *arr = new int [n];
	for(int i=0; i<n; i++)
		scanf("%d",&arr[i]);
	int *arr2 = new int[n];
	arr2[0]=-1;
	for(int i=1; i<n; i++)
	{
		if( arr[i] != arr[i-1] )
			arr2[i] = i-1;
		else
			arr2[i] = arr2[i-1];
	}
	for(int i=1; i<=m; i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		a--; b--;
		if( arr[b] == c )
		{
			if( arr[b] >= a)
				printf("%d\n", arr2[b]+1);
			else
				printf("-1\n");
		}
		else
			printf("%d\n", b+1);
	}
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	int  *arr = new int [n];
	for(int i=0; i<n; i++)
		scanf("%d",&arr[i]);
	int *arr2 = new int[n];
	arr2[0]=-1;
	for(int i=1; i<n; i++)
	{
		if( arr[i] != arr[i-1] )
			arr2[i] = i-1;
		else
			arr2[i] = arr2[i-1];
	}
	for(int i=1; i<=m; i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		a--; b--;
		if( arr[b] == c )
		{
			if( arr[b] >= a &&(arr2[b]!=-1))

					printf("%d\n", arr2[b]+1);

			else
				printf("-1\n");
		}
		else
				printf("%d\n", b+1);
	}
}


#include<iostream>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	int  *arr = new int [n];
	for(int i=0; i<n; i++)
		scanf("%d",&arr[i]);
	int *arr2 = new int[n];
	arr2[0]=-1;
	for(int i=1; i<n; i++)
	{
		if( arr[i] != arr[i-1] )
			arr2[i] = i-1;
		else
			arr2[i] = arr2[i-1];
	}
	for(int i=1; i<=m; i++)
	{
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		a--; b--;
		if( arr[b] == c )
		{
			if( arr2[b] >= a &&  arr2[b]!=-1 )

					printf("%d\n", arr2[b]+1);

			else
				printf("-1\n");
		}
		else
				printf("%d\n", b+1);
	}
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

typedef long long ll;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define all(a) a.begin(), a.end()
#define mp make_pair

using namespace std;

struct req {
	int l, r, x;
};

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n, m;
	cin >> n >> m;
	vector<int> a(n);
	forn(i, n) cin >> a[i];
	vector<req> req(m);
	forn(i, m) {
		cin >> req[i].l >> req[i].r >> req[i].x;
		req[i].l--;
		req[i].r--;
	}
	vector<int> z(n);
	z[0] = -1;
	for (int i = 1; i < n; i++) {
		if (a[i] != a[i - 1])
			z[i] = i - 1;
		else z[i] = z[i - 1];
	}
	forn(i, m) {
		if (a[req[i].r] != req[i].x)
			cout << req[i].r + 1 << endl;
		else if (z[req[i].r] >= req[i].l)
			cout << z[req[i].r] + 1 << endl;
		else cout << -1 << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>

typedef long long ll;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define all(a) a.begin(), a.end()
#define mp make_pair

using namespace std;

struct req {
	int l, r, x;
};

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n, m;
	cin >> n >> m;
	vector<int> a(n);
	forn(i, n) scanf("%d", &a[i]);
	vector<req> req(m);
	forn(i, m) {
		scanf("%d%d%d", &req[i].l, &req[i].r, &req[i].x);
		req[i].l--;
		req[i].r--;
	}
	vector<int> z(n);
	z[0] = -1;
	for (int i = 1; i < n; i++) {
		if (a[i] != a[i - 1])
			z[i] = i - 1;
		else z[i] = z[i - 1];
	}
	forn(i, m) {
		if (a[req[i].r] != req[i].x)
			printf("%d\n", req[i].r + 1);
		else if (z[req[i].r] >= req[i].l)
			printf("%d\n", z[req[i].r] + 1);
		else printf("-1\n");
	}
}


#include <iostream>

using namespace std;

unsigned int n, m, a[4 * 100000], l, r, x;

int main()
{
	cin >> n >> m;
	for (unsigned int i = 0, j = 0; i < n; ++i)
	{
		cin >> x;
		if (a[j] == 0)
			a[j] = x;
		if (a[j] == x)
			a[j + 1]++;
		else
		{
			j += 2;
			a[j] = x;
			a[j + 1]++;
		}
	}
	for (unsigned int i = 0, j = 0, k = 1; i < m; ++i, j = 0, k = 1)
	{
		cin >> l >> r >> x;
		for (; k < l; j+=2)
		{
			k += a[j + 1];
		}
		int ans = -1;
		for (; k <= r; j+=2)
		{
			if (a[j] != x)
			{
				ans = k;
				break;
			}
			k += a[j + 1];
		}
		cout << ans << endl;
	}

	return 0;
}


#include <iostream>

using namespace std;

int n, m, a[200000], b[200000], r, l, x;

int main()
{
	cin >> n >> m;
	cin >> a[0];
	b[0] = -1;
	for (int i = 1; i < n; ++i)
	{
		cin >> a[i];
		if (a[i] == a[i - 1])
			b[i] = b[i - 1];
		else
			b[i] = i - 1;
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> l >> r >> x;
		int ans = -1;
		for (int j = r-1; j >= l-1;)
		{
			if (a[j] != x)
			{
				ans = j+1;
				break;
			}
			else
				j = b[j];
		}
		cout << ans << endl;
	}

	return 0;
}


#include <iostream>

using namespace std;

int n, m, a[200000], b[200000], r, l, x;

int main()
{
	//cin >> n >> m;
	scanf_s("%d%d%d", &n, &m, &a[0]);
	//cin >> a[0];
	b[0] = -1;
	for (int i = 1; i < n; ++i)
	{
		//cin >> a[i];
		scanf_s("%d", &a[i]);
		if (a[i] == a[i - 1])
			b[i] = b[i - 1];
		else
			b[i] = i - 1;
	}
	for (int i = 0; i < m; ++i)
	{
		cin >> l >> r >> x;
		int ans = -1;
		for (int j = r-1; j >= l-1;)
		{
			if (a[j] != x)
			{
				ans = j+1;
				break;
			}
			else
				j = b[j];
		}
		//cout << ans << endl;
		printf("%d\n", ans);
	}

	return 0;
}


#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <iterator>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define mp make_pair
#define pb push_back
#define eps (double)(1e-9)
#define MOD (ll)(1e9 + 7)

const int maxlen = 2e5;
#define PI 3.14159265358979323846

pair<pii,pii> t[4 * maxlen];

int N;

int get_N(int n)
{
	int i = 1;
	while (i <= n)
		i <<= 1;
	//N = i;
	if (i == n * 2)
		return i;
	else
		return i*2;
}

void build(int i, int k)
{
	int v = N/2 + i;
	t[v] = mp(mp(k,i), mp(k,i));
	while (v != 1)
	{
		v >>= 1;
		t[v].first = min(t[2 * v].first, t[2 * v + 1].first);
		t[v].second = max(t[2 * v].second, t[2 * v + 1].second);
	}
}

pair<pii,pii> get_min_max(int v, int tl, int tr, int l, int r)
{
	if (l > r)
		return mp(mp((int)(1e9),-1), mp(0,-1));
	if (tl == l && tr == r)
		return t[v];
	int tm = (tl + tr) / 2;
	pair<pii,pii> p1, p2;
	p1 = get_min_max(2 * v, tl, tm, l, min(tm, r));
	p2 = get_min_max(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
	if (p1 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p2;
	else if (p2 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p1;
	else
		return mp(min(p1.first, p2.first), max(p1.second, p2.second));
}
int main()
{
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n, m, i, j;
	scanf("%d%d", &n, &m);
	N = get_N(n);
	for (i = 0; i < 4 * maxlen; ++i)
		t[i] = mp(mp(1e9, -1), mp(0, -1));
	for (i = 0; i < n; ++i)
	{
		scanf("%d", &j);
		build(i, j);
	}

	for (i = 0; i < m; ++i)
	{
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		pair<pii, pii> ans = get_min_max(1, 0, n-1, l - 1, r - 1);
		if (ans.first.first == x && ans.second.first == x)
			printf("-1\n");
		else if (ans.first.first != x)
			printf("%d\n", ans.first.second+1);
		else
			printf("%d\n", ans.second.second+1);
	}
	return 0;
}


#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <iterator>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define mp make_pair
#define pb push_back
#define eps (double)(1e-9)
#define MOD (ll)(1e9 + 7)

const int maxlen = 2e5;
#define PI 3.14159265358979323846

pair<pii,pii> t[4 * maxlen];

int N;

void get_N(int n)
{
	int i = 1;
	while (i <= n)
		i <<= 1;
	N = i;
	return;
}

void build(int i, int k)
{
	int v = N + i;
	t[v] = mp(mp(k,i), mp(k,i));
	while (v != 1)
	{
		v >>= 1;
		t[v].first = min(t[2 * v].first, t[2 * v + 1].first);
		t[v].second = max(t[2 * v].second, t[2 * v + 1].second);
	}
}

pair<pii,pii> get_min_max(int v, int tl, int tr, int l, int r)
{
	if (l > r)
		return mp(mp((int)(1e9),-1), mp(0,-1));
	if (tl == l && tr == r)
		return t[v];
	int tm = (tl + tr) / 2;
	pair<pii,pii> p1, p2;
	p1 = get_min_max(2 * v, tl, tm, l, min(tm, r));
	p2 = get_min_max(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
	if (p1 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p2;
	else if (p2 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p1;
	else
		return mp(min(p1.first, p2.first), max(p1.second, p2.second));
}
int main()
{
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n, m, i, j;
	scanf("%d%d", &n, &m);
	get_N(n);
	for (i = 0; i < 4 * maxlen; ++i)
		t[i] = mp(mp(1e9, -1), mp(0, -1));
	for (i = 0; i < n; ++i)
	{
		scanf("%d", &j);
		build(i, j);
	}

	for (i = 0; i < m; ++i)
	{
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		pair<pii, pii> ans = get_min_max(1, 0, n-1, l - 1, r - 1);
		if (ans.first.first == x && ans.second.first == x)
			printf("-1\n");
		else if (ans.first.first != x)
			printf("%d\n", ans.first.second+1);
		else
			printf("%d\n", ans.second.second+1);
	}
	return 0;
}


#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <iterator>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define mp make_pair
#define pb push_back
#define eps (double)(1e-9)
#define MOD (ll)(1e9 + 7)

const int maxlen = 2e5;
#define PI 3.14159265358979323846

pair<pii,pii> t[4 * maxlen];

int N;

int get_N(int n)
{
	int i = 1;
	while (i <= n)
		i <<= 1;
	//N = i;
	if (i == n * 2)
		return i;
	else
		return i*2;
}

void build(int i, int k)
{
	int v = N/2 + i;
	t[v] = mp(mp(k,i), mp(k,i));
	while (v != 1)
	{
		v >>= 1;
		t[v].first = min(t[2 * v].first, t[2 * v + 1].first);
		t[v].second = max(t[2 * v].second, t[2 * v + 1].second);
	}
}

pair<pii,pii> get_min_max(int v, int tl, int tr, int l, int r)
{
	if (l > r)
		return mp(mp((int)(1e9),-1), mp(0,-1));
	if (tl == l && tr == r)
		return t[v];
	int tm = (tl + tr) / 2;
	pair<pii,pii> p1, p2;
	p1 = get_min_max(2 * v, tl, tm, l, min(tm, r));
	p2 = get_min_max(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
	if (p1 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p2;
	else if (p2 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p1;
	else
		return mp(min(p1.first, p2.first), max(p1.second, p2.second));
}
int main()
{
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n, m, i, j;
	scanf("%d%d", &n, &m);
	N = get_N(n);
	for (i = 0; i < 4 * maxlen; ++i)
		t[i] = mp(mp(1e9, -1), mp(0, -1));
	for (i = 0; i < n; ++i)
	{
		scanf("%d", &j);
		build(i, j);
	}

	for (i = 0; i < m; ++i)
	{
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		pair<pii, pii> ans = get_min_max(1, 0, N/2-1, l - 1, r - 1);
		if (ans.first.first == x && ans.second.first == x)
			printf("-1\n");
		else if (ans.first.first != x)
			printf("%d\n", ans.first.second+1);
		else
			printf("%d\n", ans.second.second+1);
	}
	return 0;
}


#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <iterator>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define mp make_pair
#define pb push_back
#define eps (double)(1e-9)
#define MOD (ll)(1e9 + 7)

const int maxlen = 2e5;
#define PI 3.14159265358979323846

pair<pii,pii> t[4 * maxlen];

int N;

void get_N(int n)
{
	int i = 1;
	while (i <= n)
		i <<= 1;
	N = i;
	return;
}

void build(int i, int k)
{
	int v = N + i;
	t[v] = mp(mp(k,i), mp(k,i));
	while (v != 1)
	{
		v >>= 1;
		t[v].first = min(t[2 * v].first, t[2 * v + 1].first);
		t[v].second = max(t[2 * v].second, t[2 * v + 1].second);
	}
}

pair<pii,pii> get_min_max(int v, int tl, int tr, int l, int r)
{
	if (l > r)
		return mp(mp((int)(1e9),-1), mp(0,-1));
	if (tl == l && tr == r)
		return t[v];
	int tm = (tl + tr) / 2;
	pair<pii,pii> p1, p2;
	p1 = get_min_max(2 * v, tl, tm, l, min(tm, r));
	p2 = get_min_max(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
	if (p1 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p2;
	else if (p2 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p1;
	else
		return mp(min(p1.first, p2.first), max(p1.second, p2.second));
}
int main()
{
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n, m, i, j;
	scanf("%d%d", &n, &m);
	get_N(n);
	for (i = 0; i < 4 * maxlen; ++i)
		t[i] = mp(mp(1e9, -1), mp(0, -1));
	for (i = 0; i < n; ++i)
	{
		scanf("%d", &j);
		build(i, j);
	}

	for (i = 0; i < m; ++i)
	{
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		pair<pii, pii> ans = get_min_max(1, 0, n, l - 1, r - 1);
		if (ans.first.first == x && ans.second.first == x)
			printf("-1\n");
		else if (ans.first.first != x)
			printf("%d\n", ans.first.second+1);
		else
			printf("%d\n", ans.second.second+1);
	}
	return 0;
}


#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <iterator>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define mp make_pair
#define pb push_back
#define eps (double)(1e-9)
#define MOD (ll)(1e9 + 7)

const int maxlen = 2e5;
#define PI 3.14159265358979323846

pair<pii,pii> t[4 * maxlen];

int N;

int get_N(int n)
{
	int i = 1;
	while (i <= n)
		i <<= 1;
	//N = i;
	return i*2;
}

void build(int i, int k)
{
	int v = N + i;
	t[v] = mp(mp(k,i), mp(k,i));
	while (v != 1)
	{
		v >>= 1;
		t[v].first = min(t[2 * v].first, t[2 * v + 1].first);
		t[v].second = max(t[2 * v].second, t[2 * v + 1].second);
	}
}

pair<pii,pii> get_min_max(int v, int tl, int tr, int l, int r)
{
	if (l > r)
		return mp(mp((int)(1e9),-1), mp(0,-1));
	if (tl == l && tr == r)
		return t[v];
	int tm = (tl + tr) / 2;
	pair<pii,pii> p1, p2;
	p1 = get_min_max(2 * v, tl, tm, l, min(tm, r));
	p2 = get_min_max(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
	if (p1 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p2;
	else if (p2 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p1;
	else
		return mp(min(p1.first, p2.first), max(p1.second, p2.second));
}
int main()
{
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n, m, i, j;
	scanf("%d%d", &n, &m);
	N = get_N(n);
	for (i = 0; i < 4 * maxlen; ++i)
		t[i] = mp(mp(1e9, -1), mp(0, -1));
	for (i = 0; i < n; ++i)
	{
		scanf("%d", &j);
		build(i, j);
	}

	for (i = 0; i < m; ++i)
	{
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		pair<pii, pii> ans = get_min_max(1, 0, n, l - 1, r - 1);
		if (ans.first.first == x && ans.second.first == x)
			printf("-1\n");
		else if (ans.first.first != x)
			printf("%d\n", ans.first.second+1);
		else
			printf("%d\n", ans.second.second+1);
	}
	return 0;
}


#pragma comment(linker,"/STACK:64000000")
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <iterator>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define mp make_pair
#define pb push_back
#define eps (double)(1e-9)
#define MOD (ll)(1e9 + 7)

const int maxlen = 2e5;
#define PI 3.14159265358979323846

pair<pii,pii> t[4 * maxlen];

int N;

int get_N(int n)
{
	int i = 1;
	while (i <= n)
		i <<= 1;
	//N = i;
	if (i == n * 2)
		return i;
	else
		return i*2;
}

void build(int i, int k)
{
	int v = N/2 + i;
	t[v] = mp(mp(k,i), mp(k,i));
	while (v != 1)
	{
		v >>= 1;
		t[v].first = min(t[2 * v].first, t[2 * v + 1].first);
		t[v].second = max(t[2 * v].second, t[2 * v + 1].second);
	}
}

pair<pii,pii> get_min_max(int v, int tl, int tr, int l, int r)
{
	if (l > r)
		return mp(mp((int)(1e9),-1), mp(0,-1));
	if (tl == l && tr == r)
		return t[v];
	int tm = (tl + tr) / 2;
	pair<pii,pii> p1, p2;
	p1 = get_min_max(2 * v, tl, tm, l, min(tm, r));
	p2 = get_min_max(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
	if (p1 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p2;
	else if (p2 == mp(mp((int)(1e9), -1), mp(0, -1)))
		return p1;
	else
		return mp(min(p1.first, p2.first), max(p1.second, p2.second));
}
int main()
{
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif
	int n, m, i, j;
	scanf("%d%d", &n, &m);
	N = get_N(n);
	for (i = 0; i < 4 * maxlen; ++i)
		t[i] = mp(mp(1e9, -1), mp(0, -1));
	for (i = 0; i < n; ++i)
	{
		scanf("%d", &j);
		build(i, j);
	}

	for (i = 0; i < m; ++i)
	{
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		pair<pii, pii> ans = get_min_max(1, 0, n, l - 1, r - 1);
		if (ans.first.first == x && ans.second.first == x)
			printf("-1\n");
		else if (ans.first.first != x)
			printf("%d\n", ans.first.second+1);
		else
			printf("%d\n", ans.second.second+1);
	}
	return 0;
}


#include "iostream"
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

struct range
{
	int l, r;
};
vector <range> nums[1000010];
int arrays[200100];
int n, m;

int main()
{
	scanf("%d%d", &n, &m);

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

	int k = arrays[1], st = 1;
	for (i = 2; i <= n + 1; i++)
	{
		if (arrays[i] != k)
		{
			range rr;
			rr.l = st, rr.r = i - 1;
			nums[k].push_back(rr);

			st = i, k = arrays[i];
		}
	}

	int j;
	for (j = 1; j <= m; j++)
	{
		int ll, rr, xx;
		scanf("%d%d%d", &ll, &rr, &xx);

		int pos = -1;

		int szx = nums[xx].size();
		if (szx != 0)
		{
			int p, pl, pr;
			for (p = 0; p < szx; p++)
			{
				pl = nums[xx][p].l;
				pr = nums[xx][p].r;

				if (pl < ll&&ll <= pr&&pr < rr) pos = rr;
				if (ll <= pl&&pr < rr) pos = pr + 1;
				if (ll < pl&&pr <= rr) pos = pl - 1;
				if (ll < pl&&pl <= rr&&rr < pr) pos = ll;
			}
			if (nums[xx][szx - 1].r < ll) pos = ll;
			if (nums[xx][0].l > rr) pos = ll;
		}
		printf("%d\n", pos);
	}

	//while (1);
	return 0;
}


#include "iostream"
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

struct range
{
	int l, r;
};
vector <range> nums[1000010];
int arrays[200100];
int n, m;

int main()
{
	scanf("%d%d", &n, &m);

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

	int k = arrays[1], st = 1;
	for (i = 2; i <= n + 1; i++)
	{
		if (arrays[i] != k)
		{
			range rr;
			rr.l = st, rr.r = i - 1;
			nums[k].push_back(rr);

			st = i, k = arrays[i];
		}
	}

	int j;
	for (j = 1; j <= m; j++)
	{
		int ll, rr, xx;
		scanf("%d%d%d", &ll, &rr, &xx);

		int pos = ll;

		int szx = nums[xx].size();
		if (szx != 0)
		{
			int p, pl, pr, flg = 1;

			for (p = 0; p < szx; p++)
			{
				pl = nums[xx][p].l;
				pr = nums[xx][p].r;

				if (pl <= ll&&rr <= pr)
				{
					flg = 0;
					break;
				}
				if (pl <= ll&&ll <= pr&&pr < rr)
				{
					pos = pr + 1;
					break;
				}
				if (ll < pl&&pl <= rr&&rr <= pr)
				{
					pos = pl - 1;
					break;
				}
			}
			if (flg == 0) pos = -1;

		}
		printf("%d\n", pos);
	}

	//while (1);
	return 0;
}


#include "iostream"
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;

struct range
{
	int l, r;
};
vector <range> nums[1000010];
int arrays[200100];
int n, m;

int main()
{
	scanf("%d%d", &n, &m);

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

	int k = arrays[1], st = 1;
	for (i = 2; i <= n + 1; i++)
	{
		if (arrays[i] != k)
		{
			range rr;
			rr.l = st, rr.r = i - 1;
			nums[k].push_back(rr);

			st = i, k = arrays[i];
		}
	}

	int j;
	for (j = 1; j <= m; j++)
	{
		int ll, rr, xx;
		scanf("%d%d%d", &ll, &rr, &xx);

		int pos = ll;

		int szx = nums[xx].size();
		if (szx != 0)
		{
			int p, pl, pr, flg = 1;

			for (p = 0; p < szx; p++)
			{
				pl = nums[xx][p].l;
				pr = nums[xx][p].r;

				if (pl <= ll&&rr <= pr)
				{
					flg = 0;
					break;
				}
				if (pl <= ll&&ll <= pr&&pr < rr)
				{
					pos = pr + 1;
					break;
				}
				if (ll < pl&&pl <= rr&&rr <= pr)
				{
					pos = pl - 1;
					break;
				}
			}
			if (flg == 0) pos = -1;

		}
		printf("%d\n", pos);
	}

	 //while (1);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>

void Divi26(int num, int Rnum);
void NotDivi(int num, int Rnum);
void Down26(int num, int Rnum);
void CharNum(char *Arr, int length);
void RXCY(char *Arr, int length);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	scanf("%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			scanf("%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//ó���� R�ƴҶ�
				CharNum(arr, length);
				break;
			}
			if (arr[0] == 'R') {//RC23 ������ �ɷ�����
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length) {//  BC23�� R23C55���� �ٲٴ� �Լ�
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//���ĺ��� �޾ƿ�
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//���ο�迭�� ���ĺ��� ���ڷ� �ٲ㼭 ����
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//���� ����Ұ���~~
		x = (x * 26) + AlpaNum[w];
	}
	printf("R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length) {//R23C55 �� BC23���� �ٲٴ� �Լ�
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R�ڿ� �ִ� ���� �ޱ�
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'�ڿ� �ִ� ���ڵ鸸 �޾ƿ���
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//���������� ���ڵ��� ������ �ٲٱ�
	Rnum = atoi(RNum);//R23C55 �� 23�� ������

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum, Rnum);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum);
	}
	else {
		Down26(newnum, Rnum);
	}

}
void Divi26(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	for (int jj = 0; jj <count; jj++) {
		printf("%c", (char)(newtemp[jj] + 64));
	}
	printf("%d\n", Rnum);

}
void NotDivi(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;
	for (int i = 0; i< 7; i++) {

		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		else if (num == 474738) {
			printf("ZZGD");
			break;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
	printf("%d ", temp[bb]);
	}*/
	for (int w = 0; w < length; w++) {//������ �����°�
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	if (temp[0] == 26 && temp[1] == 26 && temp[2] == 26) {
		printf("ZZGD");
		goto zzz;
	}

	for (int j = 0; j < count; j++) {
		printf("%c", (char)(newtemp[j] + 64));
	}
	zzz:
	printf("%d\n", Rnum);
}
void Down26(int num, int Rnum) {
	int array[1] = { num };
	printf("%c%d\n", (char)(num + 64), Rnum);

}


#include<stdio.h>
#include<stdlib.h>

void Divi26(int num, int Rnum);
void NotDivi(int num, int Rnum);
void Down26(int num, int Rnum);
void CharNum(char *Arr, int length);
void RXCY(char *Arr, int length);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	scanf("%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			scanf("%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//ó���� R�ƴҶ�
				CharNum(arr, length);
				break;
			}
			if (arr[0] == 'R') {//RC23 ������ �ɷ�����
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length) {//  BC23�� R23C55���� �ٲٴ� �Լ�
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//���ĺ��� �޾ƿ�
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//���ο�迭�� ���ĺ��� ���ڷ� �ٲ㼭 ����
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//���� ����Ұ���~~
		x = (x * 26) + AlpaNum[w];
	}
	printf("R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length) {//R23C55 �� BC23���� �ٲٴ� �Լ�
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R�ڿ� �ִ� ���� �ޱ�
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'�ڿ� �ִ� ���ڵ鸸 �޾ƿ���
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//���������� ���ڵ��� ������ �ٲٱ�
	Rnum = atoi(RNum);//R23C55 �� 23�� ������

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum, Rnum);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum);
	}
	else {
		Down26(newnum, Rnum);
	}

}
void Divi26(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}
				else
					continue;
			}

		}
		if (temp[0] == 0) { //��� ��ģ��
			break;
		}
	}//�̱����� ��������
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}

	for (int jj = 0; jj <count; jj++) {
		printf("%c", (char)(newtemp[jj] + 64));
	}
	printf("%d\n", Rnum);

}
void NotDivi(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;
	for (int i = 0; i< 7; i++) {

		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		/*else if (num == 474738) {
			printf("ZZGD");
			break;
		}*/
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
	printf("%d ", temp[bb]);
	}*/
	//������ �����°�

	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}
				else
					continue;
			}

		}
		if (temp[0] == 0) { //��� ��ģ��
			break;
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}

	/*if (temp[0] == 26 && temp[1] == 26 && temp[2] == 26) {
		printf("ZZGD");
		goto zzz;
	}*/

	for (int j = 0; j < count; j++) {
		printf("%c", (char)(newtemp[j] + 64));
	}
	//zzz:
	printf("%d\n", Rnum);
}
void Down26(int num, int Rnum) {
	int array[1] = { num };
	printf("%c%d\n", (char)(num + 64), Rnum);

}


#include<stdio.h>
#include<stdlib.h>

int a = 0;
void Divi26(int num, int Rnum);
void NotDivi(int num, int Rnum);
void Down26(int num, int Rnum);
void CharNum(char *Arr, int length);
void RXCY(char *Arr, int length);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	scanf("%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			scanf("%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//처음이 R아닐때
				CharNum(arr, length);
				break;
			}
			if (arr[0] == 'R') {//RC23 같은거 걸러내기
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length) {//  BC23를 R23C55으로 바꾸는 함수
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//알파벳만 받아옴
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//새로운배열에 알파벳을 숫자로 바꿔서 넣음
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//이제 계산할고임~~
		x = (x * 26) + AlpaNum[w];
	}
	printf("R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length) {//R23C55 를 BC23으로 바꾸는 함수
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R뒤에 있는 숫자 받기
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'뒤에 있는 숫자들만 받아오기
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//문자형태인 숫자들을 정수로 바꾸기
	Rnum = atoi(RNum);//R23C55 의 23의 정수형

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum, Rnum);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum);
	}
	else {
		Down26(newnum, Rnum);
	}

}
void Divi26(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
/*	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}*/
	if (temp[0] == 0)
		a++;
	for (int jj = a; jj <length; jj++) {
		printf("%c", (char)(newtemp[jj] + 64));
	}
	printf("%d\n", Rnum);

}
void NotDivi(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;
	for (int i = 0; i< 7; i++) {

		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		/*else if (num == 474738) {
			printf("ZZGD");
			break;
		}*/
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
	printf("%d ", temp[bb]);
	}*/
	for (int w = 0; w < length; w++) {//위에서 가져온거
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}

	//if (temp[0] == 26 && temp[1] == 26 && temp[2] == 26) {
	//	printf("ZZGD");
//		goto zzz;
	//}

	for (int j = 0; j < count; j++) {
		printf("%c", (char)(newtemp[j] + 64));
	}
//	zzz:
	printf("%d\n", Rnum);
}
void Down26(int num, int Rnum) {
	int array[1] = { num };
	printf("%c%d\n", (char)(num + 64), Rnum);

}


#include<stdio.h>
#include<stdlib.h>

void Divi26(int num, int Rnum);
void NotDivi(int num, int Rnum);
void Down26(int num, int Rnum);
void CharNum(char *Arr, int length);
void RXCY(char *Arr, int length);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	scanf("%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			scanf("%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//ó���� R�ƴҶ�
				CharNum(arr, length);
				break;
			}
			if (arr[0] == 'R') {//RC23 ������ �ɷ�����
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length) {//  BC23�� R23C55���� �ٲٴ� �Լ�
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//���ĺ��� �޾ƿ�
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//���ο�迭�� ���ĺ��� ���ڷ� �ٲ㼭 ����
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//���� ����Ұ���~~
		x = (x * 26) + AlpaNum[w];
	}
	printf("R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length) {//R23C55 �� BC23���� �ٲٴ� �Լ�
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R�ڿ� �ִ� ���� �ޱ�
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'�ڿ� �ִ� ���ڵ鸸 �޾ƿ���
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//���������� ���ڵ��� ������ �ٲٱ�
	Rnum = atoi(RNum);//R23C55 �� 23�� ������

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum, Rnum);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum);
	}
	else {
		Down26(newnum, Rnum);
	}

}
void Divi26(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	for (int jj = 0; jj <count; jj++) {
		printf("%c", (char)(newtemp[jj] + 64));
	}
	printf("%d\n", Rnum);

}
void NotDivi(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;
	for (int i = 0; i< 7; i++) {

		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		/*else if (num == 474738) {
			printf("ZZGD");
			break;
		}*/
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
	printf("%d ", temp[bb]);
	}*/
	for (int w = 0; w < length; w++) {//������ �����°�
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}

	/*if (temp[0] == 26 && temp[1] == 26 && temp[2] == 26) {
		printf("ZZGD");
		goto zzz;
	}*/

	for (int j = 0; j < count; j++) {
		printf("%c", (char)(newtemp[j] + 64));
	}
	//zzz:
	printf("%d\n", Rnum);
}
void Down26(int num, int Rnum) {
	int array[1] = { num };
	printf("%c%d\n", (char)(num + 64), Rnum);

}


#include<stdio.h>
#include<stdlib.h>

void Divi26(int num, int Rnum, FILE *out_fp);
void NotDivi(int num, int Rnum, FILE *out_fp);
void Down26(int num, int Rnum, FILE *out_fp);
void CharNum(char *Arr, int length, FILE *out_fp);
void RXCY(char *Arr, int length, FILE *out_fp);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	FILE *inp_fp, *out_fp;
	inp_fp = fopen("spreadsheet.inp", "rt");
	out_fp = fopen("spreadsheet.out", "wt");

	fscanf(inp_fp, "%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			fscanf(inp_fp, "%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//ó���� R�ƴҶ�
				CharNum(arr, length, out_fp);
				break;
			}
			if (arr[0] == 'R') {//RC23 ������ �ɷ�����
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length, out_fp);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length, out_fp);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length, out_fp);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length, FILE *out_fp) {//  BC23�� R23C55���� �ٲٴ� �Լ�
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//���ĺ��� �޾ƿ�
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//���ο�迭�� ���ĺ��� ���ڷ� �ٲ㼭 ����
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//���� ����Ұ���~~
		x = (x * 26) + AlpaNum[w];
	}
	fprintf(out_fp, "R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length, FILE *out_fp) {//R23C55 �� BC23���� �ٲٴ� �Լ�
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R�ڿ� �ִ� ���� �ޱ�
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'�ڿ� �ִ� ���ڵ鸸 �޾ƿ���
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//���������� ���ڵ��� ������ �ٲٱ�
	Rnum = atoi(RNum);//R23C55 �� 23�� ������

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum, Rnum, out_fp);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum, out_fp);
	}
	else {
		Down26(newnum, Rnum, out_fp);
	}

}
void Divi26(int num, int Rnum, FILE *out_fp) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	//newtemp[count - 1] = newtemp[count - 1] + 1;
	for (int jj = 0; jj <count; jj++) {
		fprintf(out_fp, "%c", (char)(newtemp[jj] + 64));
	}
	fprintf(out_fp, "%d\n", Rnum);

}
void NotDivi(int num, int Rnum, FILE *out_fp) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;
	for (int i = 0; i< 6; i++) {

		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		else if (num == 474738) {
			printf("ZZGD");
			break;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
	printf("%d ", temp[bb]);
	}*/
	for (int w = 0; w < length; w++) {//������ �����°�
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}//�̱��� ������
	//newtemp[count - 1] = newtemp[count - 1] + 1;
	for (int j = 0; j < count; j++) {
		fprintf(out_fp, "%c", (char)(newtemp[j] + 64));
	}
	fprintf(out_fp, "%d\n", Rnum);
}
void Down26(int num, int Rnum, FILE *out_fp) {
	int array[1] = { num };
	fprintf(out_fp, "%c%d\n", (char)(num + 64), Rnum);
	//fprintf(out_fp, "%c%d\n", (char)(num + 65), Rnum);//1���Ѱ�

}


#include<stdio.h>
#include<stdlib.h>

void Divi26(int num, int Rnum);
void NotDivi(int num, int Rnum);
void Down26(int num, int Rnum);
void CharNum(char *Arr, int length);
void RXCY(char *Arr, int length);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	scanf("%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			scanf("%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//ó���� R�ƴҶ�
				CharNum(arr, length);
				break;
			}
			if (arr[0] == 'R') {//RC23 ������ �ɷ�����
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length) {//  BC23�� R23C55���� �ٲٴ� �Լ�
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//���ĺ��� �޾ƿ�
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//���ο�迭�� ���ĺ��� ���ڷ� �ٲ㼭 ����
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//���� ����Ұ���~~
		x = (x * 26) + AlpaNum[w];
	}
	printf("R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length) {//R23C55 �� BC23���� �ٲٴ� �Լ�
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R�ڿ� �ִ� ���� �ޱ�
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'�ڿ� �ִ� ���ڵ鸸 �޾ƿ���
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//���������� ���ڵ��� ������ �ٲٱ�
	Rnum = atoi(RNum);//R23C55 �� 23�� ������

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum,Rnum);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum);
	}
	else {
		Down26(newnum, Rnum);
	}

}
void Divi26(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	for (int jj = 0; jj <count; jj++) {
		printf("%c", (char)(newtemp[jj] + 64));
	}
	printf("%d\n", Rnum);

}
void NotDivi(int num,int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length=0;
	for (int i = 0; i< 7; i++) {

		if (num > 26){
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k] ;
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
		printf("%d ", temp[bb]);
	}*/
	for (int w = 0; w < length; w++) {//������ �����°�
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}//�̱��� ������

	for (int j = 0; j < count; j++) {
		printf("%c", (char)(newtemp[j]+64));
	}
	printf("%d\n", Rnum);
}
void Down26(int num,int Rnum) {
	int array[1] = { num };
	printf("%c%d\n",(char)(num + 64), Rnum);

}


#include<stdio.h>
#include<stdlib.h>

void Divi26(int num, int Rnum);
void NotDivi(int num, int Rnum);
void Down26(int num, int Rnum);
void CharNum(char *Arr, int length);
void RXCY(char *Arr, int length);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	scanf("%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			scanf("%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//ó���� R�ƴҶ�
				CharNum(arr, length);
				break;
			}
			if (arr[0] == 'R') {//RC23 ������ �ɷ�����
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length) {//  BC23�� R23C55���� �ٲٴ� �Լ�
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//���ĺ��� �޾ƿ�
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//���ο�迭�� ���ĺ��� ���ڷ� �ٲ㼭 ����
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//���� ����Ұ���~~
		x = (x * 26) + AlpaNum[w];
	}
	printf("R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length) {//R23C55 �� BC23���� �ٲٴ� �Լ�
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R�ڿ� �ִ� ���� �ޱ�
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'�ڿ� �ִ� ���ڵ鸸 �޾ƿ���
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//���������� ���ڵ��� ������ �ٲٱ�
	Rnum = atoi(RNum);//R23C55 �� 23�� ������

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum, Rnum);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum);
	}
	else {
		Down26(newnum, Rnum);
	}

}
void Divi26(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	for (int jj = 0; jj <count; jj++) {
		printf("%c", (char)(newtemp[jj] + 64));
	}
	printf("%d\n", Rnum);

}
void NotDivi(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;
	for (int i = 0; i< 7; i++) {

		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		else if (num == 474738) {
			printf("ZZGD");
			break;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
	printf("%d ", temp[bb]);
	}*/
	for (int w = 0; w < length; w++) {//������ �����°�
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	for (int j = 0; j < count; j++) {
		printf("%c", (char)(newtemp[j] + 64));
	}
	printf("%d\n", Rnum);
}
void Down26(int num, int Rnum) {
	int array[1] = { num };
	printf("%c%d\n", (char)(num + 64), Rnum);

}


#include<stdio.h>
#include<stdlib.h>

void Divi26(int num, int Rnum);
void NotDivi(int num, int Rnum);
void Down26(int num, int Rnum);
void CharNum(char *Arr, int length);
void RXCY(char *Arr, int length);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	scanf("%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			scanf("%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//ó���� R�ƴҶ�
				CharNum(arr, length);
				break;
			}
			if (arr[0] == 'R') {//RC23 ������ �ɷ�����
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length) {//  BC23�� R23C55���� �ٲٴ� �Լ�
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//���ĺ��� �޾ƿ�
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//���ο�迭�� ���ĺ��� ���ڷ� �ٲ㼭 ����
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//���� ����Ұ���~~
		x = (x * 26) + AlpaNum[w];
	}
	printf("R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length) {//R23C55 �� BC23���� �ٲٴ� �Լ�
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R�ڿ� �ִ� ���� �ޱ�
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'�ڿ� �ִ� ���ڵ鸸 �޾ƿ���
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//���������� ���ڵ��� ������ �ٲٱ�
	Rnum = atoi(RNum);//R23C55 �� 23�� ������

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum,Rnum);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum);
	}
	else {
		Down26(newnum, Rnum);
	}

}
void Divi26(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	for (int jj = 0; jj <count; jj++) {
		printf("%c", (char)(newtemp[jj] + 64));
	}
	printf("%d\n", Rnum);

}
void NotDivi(int num,int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int length=0;
	for (int i = 0; i< 7; i++) {

		if (num > 26){
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k] ;
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
		printf("%d ", temp[bb]);
	}*/
	for (int w = 0; w < length; w++) {//������ �����°�
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	for (int j = 0; j < length; j++) {
		printf("%c", (char)(temp[j]+64));
	}
	printf("%d\n", Rnum);
}
void Down26(int num,int Rnum) {
	int array[1] = { num };
	printf("%c%d\n",(char)(num + 64), Rnum);

}


#include<stdio.h>
#include<stdlib.h>

void Divi26(int num, int Rnum);
void NotDivi(int num, int Rnum);
void Down26(int num, int Rnum);
void CharNum(char *Arr, int length);
void RXCY(char *Arr, int length);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	scanf("%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			scanf("%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//ó���� R�ƴҶ�
				CharNum(arr, length);
				break;
			}
			if (arr[0] == 'R') {//RC23 ������ �ɷ�����
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length) {//  BC23�� R23C55���� �ٲٴ� �Լ�
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//���ĺ��� �޾ƿ�
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//���ο�迭�� ���ĺ��� ���ڷ� �ٲ㼭 ����
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//���� ����Ұ���~~
		x = (x * 26) + AlpaNum[w];
	}
	printf("R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length) {//R23C55 �� BC23���� �ٲٴ� �Լ�
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R�ڿ� �ִ� ���� �ޱ�
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'�ڿ� �ִ� ���ڵ鸸 �޾ƿ���
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//���������� ���ڵ��� ������ �ٲٱ�
	Rnum = atoi(RNum);//R23C55 �� 23�� ������

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum, Rnum);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum);
	}
	else {
		Down26(newnum, Rnum);
	}

}
void Divi26(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	for (int jj = 0; jj <count; jj++) {
		printf("%c", (char)(newtemp[jj] + 64));
	}
	printf("%d\n", Rnum);

}
void NotDivi(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;
	for (int i = 0; i< 7; i++) {

		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		/*else if (num == 474738) {
			printf("ZZGD");
			break;
		}*/
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
	printf("%d ", temp[bb]);
	}*/
	for (int w = 0; w < length; w++) {//������ �����°�
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}

	if (temp[0] == 26 && temp[1] == 26 && temp[2] == 26) {
		printf("ZZGD");
		goto zzz;
	}

	for (int j = 0; j < count; j++) {
		printf("%c", (char)(newtemp[j] + 64));
	}
	zzz:
	printf("%d\n", Rnum);
}
void Down26(int num, int Rnum) {
	int array[1] = { num };
	printf("%c%d\n", (char)(num + 64), Rnum);

}


#include<stdio.h>
#include<stdlib.h>

void Divi26(int num, int Rnum);
void NotDivi(int num, int Rnum);
void Down26(int num, int Rnum);
void CharNum(char *Arr, int length);
void RXCY(char *Arr, int length);

int main() {

	int n;
	int length;
	char arr[20] = { 0 };
	int count = 0;

	scanf("%d ", &n);
	/*if ((n < 1) || (n > 10000)) {
	return 0;
	}*/
	for (int ii = 0; ii < n; ii++) {

		for (int j = 0; j < 20; j++) {
			scanf("%c", &arr[j]);
			//arr[j] = mun;
			if (arr[j] == '\n') {
				length = j;
				break;
			}
		}
		for (int b = 0; b < length; b++) {
			if (arr[0] != 'R') {//ó���� R�ƴҶ�
				CharNum(arr, length);
				break;
			}
			if (arr[0] == 'R') {//RC23 ������ �ɷ�����
				if ((arr[1] < 0x30) || (arr[1] > 0x39)) {
					CharNum(arr, length);
					break;
				}
			}
			if (arr[0] == 'R') {
				if (arr[1] >= '1'&&arr[1] <= '9') {
					if (arr[b] == 'C') {
						RXCY(arr, length);
						break;
					}
					else {
						if (b != length - 1)
							continue;
						else {
							CharNum(arr, length);
							break;
						}
					}
				}
			}
		}

	}
}
void CharNum(char *Arr, int length) {//  BC23�� R23C55���� �ٲٴ� �Լ�
	char alpa[11] = { 0 };
	char num[10] = { 0 };
	int NumNum;
	int AlpaNum[10] = { 0 };
	int x = 0;
	int y = 0;
	int k = 0;
	int kn = 0;
	for (int i = 0; i < length; i++) {//���ĺ��� �޾ƿ�
		if ((Arr[i] < 0x30) || (Arr[i] > 0x39)) {
			alpa[k] = Arr[i];
			k++;
		}
		else {
			num[kn] = Arr[i];
			kn++;
		}
	}
	NumNum = atoi(num);

	for (int j = 0; j < k; j++) {//���ο�迭�� ���ĺ��� ���ڷ� �ٲ㼭 ����
		AlpaNum[j] = alpa[j] - 64;
	}

	for (int w = 0; w < k; w++) {//���� ����Ұ���~~
		x = (x * 26) + AlpaNum[w];
	}
	printf("R%dC%d\n", NumNum, x);

}

void RXCY(char *Arr, int length) {//R23C55 �� BC23���� �ٲٴ� �Լ�
	char num[10] = { 0 };
	int k = 0;
	int l = 0;
	int newnum;
	int Rnum;
	char RNum[10] = { 0 };
	char alpaCh[10] = { 0 };
	int kim[10] = { 0 };
	int hee[10] = { 0 };
	int ju[10] = { 0 };

	for (int j = 0; j < length; j++) {//R�ڿ� �ִ� ���� �ޱ�
		if (Arr[j] >= '0'&&Arr[j] <= '9') {
			RNum[l] = Arr[j];
			l++;
		}
		if (Arr[j] == 'C')
			break;
	}
	for (int i = 0; i < length; i++) {//'C'�ڿ� �ִ� ���ڵ鸸 �޾ƿ���
		if (Arr[i] == 'C') {
			for (int j = i + 1; j < length; j++) {
				num[k] = Arr[j];
				k++;
			}
		}
	}
	newnum = atoi(num);//���������� ���ڵ��� ������ �ٲٱ�
	Rnum = atoi(RNum);//R23C55 �� 23�� ������

	if ((newnum % 26 == 0) && (newnum>26)) {
		Divi26(newnum, Rnum);

	}
	else if ((newnum % 26 != 0) && (newnum>26)) {
		NotDivi(newnum, Rnum);
	}
	else {
		Down26(newnum, Rnum);
	}

}
void Divi26(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;

	for (int i = 0; i < 6; i++) {
		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;
			num = (num - na) / 26;
		}
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	for (int w = 0; w < length; w++) {
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}
				else
					continue;
			}
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}
	for (int jj = 0; jj <count; jj++) {
		printf("%c", (char)(newtemp[jj] + 64));
	}
	printf("%d\n", Rnum);

}
void NotDivi(int num, int Rnum) {
	int na;
	int array[6] = { 0 };
	int temp[6] = { 0 };
	int newtemp[6] = { 0 };
	int length = 0;
	for (int i = 0; i< 7; i++) {

		if (num > 26) {
			na = num % 26;
			array[i] = na;
			length++;

			num = (num - na) / 26;
		}
		/*else if (num == 474738) {
			printf("ZZGD");
			break;
		}*/
		else {
			array[i] = num;
			length++;
			break;
		}
	}

	int kk = 0;
	for (int k = length - 1; k >= 0; k--) {
		temp[kk] = array[k];
		kk++;
	}
	/*for (int bb = 0; bb < length; bb++) {
	printf("%d ", temp[bb]);
	}*/
	for (int w = 0; w < length; w++) {//������ �����°�
		for (int j = 0; j < length; j++) {
			if (temp[j] == 0) {
				if (temp[j - 1] > 0) {
					temp[j] = 26;
					temp[j - 1] = temp[j - 1] - 1;
				}

			}
			else
				continue;
		}
	}
	int count = 0;
	for (int a = 0; a < length; a++) {
		if (temp[a] != 0) {
			newtemp[count] = temp[a];
			count++;
		}
		else if (temp[a] == 0)
			continue;
	}

	/*if (temp[0] == 26 && temp[1] == 26 && temp[2] == 26) {
		printf("ZZGD");
		goto zzz;
	}*/

	for (int j = 0; j < count; j++) {
		printf("%c", (char)(newtemp[j] + 64));
	}
	//zzz:
	printf("%d\n", Rnum);
}
void Down26(int num, int Rnum) {
	int array[1] = { num };
	printf("%c%d\n", (char)(num + 64), Rnum);

}


#include <iostream>
#include <string>
#include <algorithm>
#include <climits>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

typedef pair<string, bool> Pair;
typedef long long ll;
vector <ll> v3;

int f1(int y)
{
    int x=1;
	if(y==1)
	{
		return x;
	}
	else
	{
		for(int i=0; y!=1; i++)
		{
			x=x*26;
			y--;
		}
		return x;
	}
}

/////////
int f2(int y, int o, int &qw)
{
	//cout<<v3[o]<<endl;
	int x=y%v3[o];
	qw=x/v3[o-1];
	return x;
}

void fv();

int main()
{
	//ifstream ff("file.txt");
	fv();
	int n;
	cin>>n;
	string s="";
	string s2="";
	string result="";
	string s3="";
	int sum=0;
	vector <Pair> v1(n);
	vector <string> v2(n);
	enum DL {A, D, A1};
	bool b1=false;
	bool b2=false;
	DL P=A;
	for(int i=0; i<n; i++)
	{
		cin>>v1[i].first;
		P=A;
		s="";
		s2="";
		result="";
		s3="";
		sum=0;
		b2=false;
		b1=false;
		for(int j=0; j<v1[i].first.size(); j++)
		{
			if(P==A && isalpha(v1[i].first[j])==false)
			{
				P=D;
			}
			else if(P==D && isalpha(v1[i].first[j])==true)
			{
				P=A1;
				break;
			}

		}
		if(P==D)
		{
			v1[i].second=true;
		}
		else if(P=A1)
		{
			v1[i].second=false;
		}
		P=A;
		//cout<<v1[i].second<<endl;
		if(v1[i].second == true)
		{
			for(int j=0; j<v1[i].first.size(); j++)
			{
				if(isalpha(v1[i].first[j])==false)
				{
						//s+='R';
					b1=true;
				}
				if(b1==true)
				{
					s+=v1[i].first[j];
				}
				else
				{
					s2+=v1[i].first[j];
				}
			}
			b1=false;
			for(int j=0; j<s2.size(); j++)
			{
				if(j!=s2.size()-1)
				{
					//cout<<((int)s2[j]-64)<<endl;
					sum+=((int)s2[j]-64)*f1(s2.size()-j);
					//cout<<sum<<" "<<s2.size()<<endl;
				}
				else
				{
					sum+=((int)s2[j]-64);
				}
			}
			//cout<<sum<<endl;
			string u;
			stringstream ss;
			ss<<sum;
			ss>>u;
			result='R'+s+"C"+u;
			cout<<result<<endl;

		}
		else
		{
			P=A;
			for(int j=0; j<v1[i].first.size(); j++)
			{
				 if(P==A && isalpha(v1[i].first[j])==false && b2==false)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==false && b2==true)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==true && b2==true)
				{
					P=D;
				}
				else if(P==D && isalpha(v1[i].first[j])==false && b2==true)
				{
					s2+=v1[i].first[j];
				}

			}
			int y;
			istringstream ist(s2);
			ist>>y;
			int r;
			int it=y%26;
			if(it==0)
			{
				it=26;
			}
			s3+=char(it+64);
			//cout<<s3<<endl;
			y-=it;
			if(y/26<=26)
			{
				it=y/26;
				s3+=char(it+64);
				y=0;
			}
			int o=1;
			while(y!=0)
			{
				//cout<<y<<endl;

				{
					int qw;
					it=f2(y, o, qw);
					//cout<<it<<endl;
					//cout<<qw<<endl;
					if(qw==0)
					{
						qw=26;
					}
					if(it==0)
					{
						y-=v3[o];
						//cout<<v3[o]<<endl;
					}
					else
					{
						y-=it;
					}
					s3+=char(qw+64);
				}
				o++;
			}
			reverse(s3.begin(), s3.end());
			string result1=s3+s;
			cout<<result1<<endl;
			//////////
		}
	}

	return 0;
}

void fv()
{
	int x=1;
	while(x<1000000)
	{
		x*=26;
		v3.push_back(x);
	}
}


#include <iostream>
#include <string>
#include <algorithm>
#include <climits>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

typedef pair<string, bool> Pair;
typedef long long ll;
vector <ll> v3;

int f1(int y)
{
    int x=1;
	if(y==1)
	{
		return x;
	}
	else
	{
		for(int i=0; y!=1; i++)
		{
			x=x*26;
			y--;
		}
		return x;
	}
}

/////////
int f2(int y, int o, int &qw)
{
	cout<<v3[o]<<endl;
	int x=y%v3[o];
	qw=x/v3[o-1];
	return x;
}

void fv();

int main()
{
	//ifstream ff("file.txt");
	fv();
	int n;
	cin>>n;
	string s="";
	string s2="";
	string result="";
	string s3="";
	int sum=0;
	vector <Pair> v1(n);
	vector <string> v2(n);
	enum DL {A, D, A1};
	bool b1=false;
	bool b2=false;
	DL P=A;
	for(int i=0; i<n; i++)
	{
		cin>>v1[i].first;
		P=A;
		s="";
		s2="";
		result="";
		s3="";
		sum=0;
		b2=false;
		b1=false;
		for(int j=0; j<v1[i].first.size(); j++)
		{
			if(P==A && isalpha(v1[i].first[j])==false)
			{
				P=D;
			}
			else if(P==D && isalpha(v1[i].first[j])==true)
			{
				P=A1;
				break;
			}

		}
		if(P==D)
		{
			v1[i].second=true;
		}
		else if(P=A1)
		{
			v1[i].second=false;
		}
		P=A;
		//cout<<v1[i].second<<endl;
		if(v1[i].second == true)
		{
			for(int j=0; j<v1[i].first.size(); j++)
			{
				if(isalpha(v1[i].first[j])==false)
				{
						//s+='R';
					b1=true;
				}
				if(b1==true)
				{
					s+=v1[i].first[j];
				}
				else
				{
					s2+=v1[i].first[j];
				}
			}
			b1=false;
			for(int j=0; j<s2.size(); j++)
			{
				if(j!=s2.size()-1)
				{
					//cout<<((int)s2[j]-64)<<endl;
					sum+=((int)s2[j]-64)*f1(s2.size()-j);
					//cout<<sum<<" "<<s2.size()<<endl;
				}
				else
				{
					sum+=((int)s2[j]-64);
				}
			}
			//cout<<sum<<endl;
			string u;
			stringstream ss;
			ss<<sum;
			ss>>u;
			result='R'+s+"C"+u;
			cout<<result<<endl;

		}
		else
		{
			P=A;
			for(int j=0; j<v1[i].first.size(); j++)
			{
				 if(P==A && isalpha(v1[i].first[j])==false && b2==false)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==false && b2==true)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==true && b2==true)
				{
					P=D;
				}
				else if(P==D && isalpha(v1[i].first[j])==false && b2==true)
				{
					s2+=v1[i].first[j];
				}

			}
			int y;
			istringstream ist(s2);
			ist>>y;
			int r;
			int it=y%26;
			if(it==0)
			{
				it=26;
			}
			s3+=char(it+64);
			cout<<s3<<endl;
			y-=it;
			if(y/26<=26)
			{
				it=y/26;
				s3+=char(it+64);
				y=0;
			}
			int o=1;
			while(y!=0)
			{
				cout<<y<<endl;

				{
					int qw;
					it=f2(y, o, qw);
					cout<<it<<endl;
					cout<<qw<<endl;
					if(qw==0)
					{
						qw=26;
					}
					if(it==0)
					{
						y-=v3[o];
						cout<<v3[o]<<endl;
					}
					else
					{
						y-=it;
					}
					s3+=char(qw+64);
				}
				o++;
			}
			reverse(s3.begin(), s3.end());
			string result1=s3+s;
			cout<<result1<<endl;
			//////////
		}
	}

	return 0;
}

void fv()
{
	int x=1;
	while(x<1000000)
	{
		x*=26;
		v3.push_back(x);
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

typedef pair <string, int> Pair;
typedef long long ll;

int main()
{
	//int u=65;
	//cout<<(char)u<<endl;
	//ifstream ff("file.txt");
	int n;
	cin>>n;
	vector <Pair> ip(n);
	int len;
	int  jk=0;
	for(int i=0; i<n; i++)
	{
		cin>>ip[i].first;
		jk=0;
		if(ip[i].first[0]=='R')
		{
			len=ip[i].first.length();
			for(int j=0; j<len-1; j++)
			{
				if(isalpha(ip[i].first[j])==false && isalpha(ip[i].first[j+1])==true)
				{
					ip[i].second=0;
					goto line1;
				}
			}
			ip[i].second=1;
line1:;
		}
		else
		{
			ip[i].second=1;
		}
	}
	string str="";
	string str1="";
	ll O_1=0;
	int mom=0;
	bool b2=false;
	int hran;
	int hramoct;

	for(int i=0; i<n; i++)
	{
		len=ip[i].first.length();
		mom=0;
		O_1=0;
		str="";
		str1="";
		b2=false;
		if(ip[i].second==1)
		{
			for(int j=len-1; j>=0; j--)
			{
				if(isalpha(ip[i].first[j])==false)
				{
					str+=ip[i].first[j];
				}
				else
				{
					str1+=ip[i].first[j];
					O_1+=((int)ip[i].first[j]-64)*(mom==0 ? 1 : mom *26);
					//cout<<((int)ip[i].first[j])<<endl;
					//cout<<O_1<<endl;
					mom++;
				}
			}
			reverse(str.begin(),str.end());
			cout<<'R'<<str<<'C'<<O_1<<endl;;
		}
		else
		{
			for(int j=1; j<len; j++)
			{
				if(isalpha(ip[i].first[j])==false)
				{
					if(b2==false)
					{
						str+=ip[i].first[j];
					}
					else
					{
						str1+=ip[i].first[j];
					}
				}
				else
				{
					b2=true;
				}

			}

			istringstream ist(str1);
			ist>>hran;
			//������ � ���
			hramoct=hran%26;
			hran=hran/26;
			stringstream ss;
			ss<<hran;
			ss>>str1;
			//������ �������
			string helpf="";
			//cout<<str1<<endl;
			int lym=str1.length();
			for(int j=0; j<lym; j++)
			{
				if(hran!=0)
				{
					helpf+=((int)(hran)+64);
				}
				//cout<<helpf<<endl;
			}
			//cout<<helpf<<endl;
			helpf+=(int)(hramoct+64);
			cout<<helpf<<str<<endl;
		}
	}

	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

typedef pair <string, int> Pair;
typedef long long ll;

int main()
{
	//int u=65;
	//cout<<(char)u<<endl;
	ifstream ff("file.txt");
	int n;
	ff>>n;
	vector <Pair> ip(n);
	int len;
	int  jk=0;
	for(int i=0; i<n; i++)
	{
		ff>>ip[i].first;
		jk=0;
		if(ip[i].first[0]=='R')
		{
			len=ip[i].first.length();
			for(int j=0; j<len-1; j++)
			{
				if(isalpha(ip[i].first[j])==false && isalpha(ip[i].first[j+1])==true)
				{
					ip[i].second=0;
					goto line1;
				}
			}
			ip[i].second=1;
line1:;
		}
		else
		{
			ip[i].second=1;
		}
	}
	string str="";
	string str1="";
	ll O_1=0;
	int mom=0;
	bool b2=false;
	int hran;
	int hramoct;

	for(int i=0; i<n; i++)
	{
		len=ip[i].first.length();
		mom=0;
		O_1=0;
		str="";
		str1="";
		b2=false;
		if(ip[i].second==1)
		{
			for(int j=len-1; j>=0; j--)
			{
				if(isalpha(ip[i].first[j])==false)
				{
					str+=ip[i].first[j];
				}
				else
				{
					str1+=ip[i].first[j];
					O_1+=((int)ip[i].first[j]-64)*(mom==0 ? 1 : mom *26);
					//cout<<((int)ip[i].first[j])<<endl;
					//cout<<O_1<<endl;
					mom++;
				}
			}
			reverse(str.begin(),str.end());
			cout<<'R'<<str<<'C'<<O_1<<endl;;
		}
		else
		{
			for(int j=1; j<len; j++)
			{
				if(isalpha(ip[i].first[j])==false)
				{
					if(b2==false)
					{
						str+=ip[i].first[j];
					}
					else
					{
						str1+=ip[i].first[j];
					}
				}
				else
				{
					b2=true;
				}

			}

			istringstream ist(str1);
			ist>>hran;
			//������ � ���
			hramoct=hran%26;
			hran=hran/26;
			stringstream ss;
			ss<<hran;
			ss>>str1;
			//������ �������
			string helpf="";
			///////
			//cout<<str1<<endl;
			int lym=str1.length();
			for(int j=0; j<lym; j++)
			{
				helpf+=((int)(hran)+64);
				//cout<<helpf<<endl;
			}
			//cout<<helpf<<endl;
			helpf+=(int)(hramoct+64);
			if(helpf[0]=='@')
			{
				helpf.erase(0,1);
			}
			cout<<helpf<<str<<endl;
		}
	}

	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <climits>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

typedef pair<string, bool> Pair;
typedef long long ll;
vector <ll> v3;

int f1(int y)
{
    int x=1;
	if(y==1)
	{
		return x;
	}
	else
	{
		for(int i=0; y!=1; i++)
		{
			x=x*26;
			y--;
		}
		return x;
	}
}

/////////
int f2(int y, int o, int &qw)
{
	//cout<<v3[o]<<endl;
	int x=y%v3[o];
	qw=x/v3[o-1];
	return x;
}

void fv();

int main()
{
	//ifstream ff("file.txt");
	fv();
	int n;
	cin>>n;
	string s="";
	string s2="";
	string result="";
	string s3="";
	int sum=0;
	vector <Pair> v1(n);
	vector <string> v2(n);
	enum DL {A, D, A1};
	bool b1=false;
	bool b2=false;
	DL P=A;
	for(int i=0; i<n; i++)
	{
		cin>>v1[i].first;
		P=A;
		s="";
		s2="";
		result="";
		s3="";
		sum=0;
		b2=false;
		b1=false;
		for(int j=0; j<v1[i].first.size(); j++)
		{
			if(P==A && isalpha(v1[i].first[j])==false)
			{
				P=D;
			}
			else if(P==D && isalpha(v1[i].first[j])==true)
			{
				P=A1;
				break;
			}

		}
		if(P==D)
		{
			v1[i].second=true;
		}
		else if(P=A1)
		{
			v1[i].second=false;
		}
		P=A;
		//cout<<v1[i].second<<endl;
		if(v1[i].second == true)
		{
			for(int j=0; j<v1[i].first.size(); j++)
			{
				if(isalpha(v1[i].first[j])==false)
				{
						//s+='R';
					b1=true;
				}
				if(b1==true)
				{
					s+=v1[i].first[j];
				}
				else
				{
					s2+=v1[i].first[j];
				}
			}
			b1=false;
			for(int j=0; j<s2.size(); j++)
			{
				if(j!=s2.size()-1)
				{
					//cout<<((int)s2[j]-64)<<endl;
					sum+=((int)s2[j]-64)*f1(s2.size()-j);
					//cout<<sum<<" "<<s2.size()<<endl;
				}
				else
				{
					sum+=((int)s2[j]-64);
				}
			}
			//cout<<sum<<endl;
			string u;
			stringstream ss;
			ss<<sum;
			ss>>u;
			result='R'+s+"C"+u;
			cout<<result<<endl;

		}
		else
		{
			P=A;
			for(int j=0; j<v1[i].first.size(); j++)
			{
				 if(P==A && isalpha(v1[i].first[j])==false && b2==false)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==false && b2==true)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==true && b2==true)
				{
					P=D;
				}
				else if(P==D && isalpha(v1[i].first[j])==false && b2==true)
				{
					s2+=v1[i].first[j];
				}

			}
			int y;
			istringstream ist(s2);
			ist>>y;
			int r;
			int it=y%26;
			if(it==0)
			{
				it=26;
			}
			s3+=char(it+64);
			//cout<<s3<<endl;
			y-=it;
			if(y/26<=26 && y!=0)
			{
				//it=y/26;
				s3+=char(it+64);
				y=0;
			}
			int o=1;
			while(y!=0)
			{
				//cout<<y<<endl;

				{
					int qw;
					it=f2(y, o, qw);
					//cout<<it<<endl;
					//cout<<qw<<endl;
					if(qw==0)
					{
						qw=26;
					}
					if(it==0)
					{
						y-=v3[o];
						//cout<<v3[o]<<endl;
					}
					else
					{
						y-=it;
					}
					s3+=char(qw+64);
				}
				o++;
			}
			reverse(s3.begin(), s3.end());
			string result1=s3+s;
			cout<<result1<<endl;
			//////////
		}
	}

	return 0;
}

void fv()
{
	int x=1;
	while(x<1000000)
	{
		x*=26;
		v3.push_back(x);
	}
}


#include <iostream>
#include <string>
#include <algorithm>
#include <climits>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

typedef pair<string, bool> Pair;
typedef long long ll;
vector <ll> v3;

int f1(int y)
{
    int x=1;
	if(y==1)
	{
		return x;
	}
	else
	{
		for(int i=0; y!=1; i++)
		{
			x=x*26;
			y--;
		}
		return x;
	}
}

/////////
int f2(int y, int o, int &qw)
{
	//cout<<v3[o]<<endl;
	int x=y%v3[o];
	qw=x/v3[o-1];
	return x;
}

void fv();

int main()
{
	//ifstream ff("file.txt");
	fv();
	int n;
	cin>>n;
	string s="";
	string s2="";
	string result="";
	string s3="";
	int sum=0;
	vector <Pair> v1(n);
	vector <string> v2(n);
	enum DL {A, D, A1};
	bool b1=false;
	bool b2=false;
	DL P=A;
	for(int i=0; i<n; i++)
	{
		cin>>v1[i].first;
		P=A;
		s="";
		s2="";
		result="";
		s3="";
		sum=0;
		b2=false;
		b1=false;
		for(int j=0; j<v1[i].first.size(); j++)
		{
			if(P==A && isalpha(v1[i].first[j])==false)
			{
				P=D;
			}
			else if(P==D && isalpha(v1[i].first[j])==true)
			{
				P=A1;
				break;
			}

		}
		if(P==D)
		{
			v1[i].second=true;
		}
		else if(P=A1)
		{
			v1[i].second=false;
		}
		P=A;
		//cout<<v1[i].second<<endl;
		if(v1[i].second == true)
		{
			for(int j=0; j<v1[i].first.size(); j++)
			{
				if(isalpha(v1[i].first[j])==false)
				{
						//s+='R';
					b1=true;
				}
				if(b1==true)
				{
					s+=v1[i].first[j];
				}
				else
				{
					s2+=v1[i].first[j];
				}
			}
			b1=false;
			for(int j=0; j<s2.size(); j++)
			{
				if(j!=s2.size()-1)
				{
					//cout<<((int)s2[j]-64)<<endl;
					sum+=((int)s2[j]-64)*f1(s2.size()-j);
					//cout<<sum<<" "<<s2.size()<<endl;
				}
				else
				{
					sum+=((int)s2[j]-64);
				}
			}
			//cout<<sum<<endl;
			string u;
			stringstream ss;
			ss<<sum;
			ss>>u;
			result='R'+s+"C"+u;
			cout<<result<<endl;

		}
		else
		{
			P=A;
			for(int j=0; j<v1[i].first.size(); j++)
			{
				 if(P==A && isalpha(v1[i].first[j])==false && b2==false)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==false && b2==true)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==true && b2==true)
				{
					P=D;
				}
				else if(P==D && isalpha(v1[i].first[j])==false && b2==true)
				{
					s2+=v1[i].first[j];
				}

			}

		//	cout<<s<<" "<<s2<<endl;
			//////////
			int y;
			istringstream ist(s2);
			ist>>y;
			int r;
			int it=y%26;
			if(it==0)
			{
				it=26;
			}
			s3+=char(it+64);
		//	cout<<s3<<endl;
			y-=it;
			int o=1;
			while(y!=0)
			{
				//cout<<y<<endl;
				if(y/26<=26)
				{
					it=y/26;
					s3+=char(it+64);
					y=0;
				}
				else
				{
					int qw;
					it=f2(y, o, qw);
				//	cout<<it<<endl;
				//	cout<<qw<<endl;
					if(qw==0)
					{
						qw=26;
					}
					if(it==0)
					{
						y-=v3[o];
						//cout<<v3[o]<<endl;
					}
					else
					{
						y-=it;
					}
					s3+=char(qw+64);
				}
				o++;
			}
			reverse(s3.begin(), s3.end());
			string result1=s3+s;
			cout<<result1<<endl;
			//////////
		}

	}

	return 0;
}

void fv()
{
	int x=1;
	while(x<1000000)
	{
		x*=26;
		v3.push_back(x);
	}
}


#include <iostream>
#include <string>
#include <algorithm>
#include <climits>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

typedef pair<string, bool> Pair;
typedef long long ll;
vector <ll> v3;

int f1(int y)
{
    int x=1;
	if(y==1)
	{
		return x;
	}
	else
	{
		for(int i=0; y!=1; i++)
		{
			x=x*26;
			y--;
		}
		return x;
	}
}

/////////
int f2(int y, int o, int &qw)
{
	//cout<<v3[o]<<endl;
	int x=y%v3[o];
	qw=x/v3[o-1];
	return x;
}

void fv();

int main()
{
	ifstream ff("file.txt");
	fv();
	int n;
	ff>>n;
	string s="";
	string s2="";
	string result="";
	string s3="";
	int sum=0;
	vector <Pair> v1(n);
	vector <string> v2(n);
	enum DL {A, D, A1};
	bool b1=false;
	bool b2=false;
	DL P=A;
	for(int i=0; i<n; i++)
	{
		ff>>v1[i].first;
		P=A;
		s="";
		s2="";
		result="";
		s3="";
		sum=0;
		b2=false;
		b1=false;
		for(int j=0; j<v1[i].first.size(); j++)
		{
			if(P==A && isalpha(v1[i].first[j])==false)
			{
				P=D;
			}
			else if(P==D && isalpha(v1[i].first[j])==true)
			{
				P=A1;
				break;
			}

		}
		if(P==D)
		{
			v1[i].second=true;
		}
		else if(P=A1)
		{
			v1[i].second=false;
		}
		P=A;
		//cout<<v1[i].second<<endl;
		if(v1[i].second == true)
		{
			for(int j=0; j<v1[i].first.size(); j++)
			{
				if(isalpha(v1[i].first[j])==false)
				{
						//s+='R';
					b1=true;
				}
				if(b1==true)
				{
					s+=v1[i].first[j];
				}
				else
				{
					s2+=v1[i].first[j];
				}
			}
			b1=false;
			for(int j=0; j<s2.size(); j++)
			{
				if(j!=s2.size()-1)
				{
					//cout<<((int)s2[j]-64)<<endl;
					sum+=((int)s2[j]-64)*f1(s2.size()-j);
					//cout<<sum<<" "<<s2.size()<<endl;
				}
				else
				{
					sum+=((int)s2[j]-64);
				}
			}
			//cout<<sum<<endl;
			string u;
			stringstream ss;
			ss<<sum;
			ss>>u;
			result='R'+s+"C"+u;
			cout<<result<<endl;

		}
		else
		{
			P=A;
			for(int j=0; j<v1[i].first.size(); j++)
			{
				 if(P==A && isalpha(v1[i].first[j])==false && b2==false)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==false && b2==true)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==true && b2==true)
				{
					P=D;
				}
				else if(P==D && isalpha(v1[i].first[j])==false && b2==true)
				{
					s2+=v1[i].first[j];
				}

			}
			int y;
			istringstream ist(s2);
			ist>>y;
			int r;
			int it=y%26;
			if(it==0)
			{
				it=26;
			}
			s3+=char(it+64);
			//cout<<s3<<endl;
			y-=it;
			if(y/26<=26 && y!=0)
			{
				//it=y/26;
				s3+=char(it+64);
				y=0;
			}
			int o=1;
			while(y!=0)
			{
				//cout<<y<<endl;

				{
					int qw;
					it=f2(y, o, qw);
					//cout<<it<<endl;
					//cout<<qw<<endl;
					if(qw==0)
					{
						qw=26;
					}
					if(it==0)
					{
						y-=v3[o];
						//cout<<v3[o]<<endl;
					}
					else
					{
						y-=it;
					}
					s3+=char(qw+64);
				}
				o++;
			}
			reverse(s3.begin(), s3.end());
			string result1=s3+s;
			cout<<result1<<endl;
			//////////
		}
	}

	return 0;
}

void fv()
{
	int x=1;
	while(x<1000000)
	{
		x*=26;
		v3.push_back(x);
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

typedef pair <string, int> Pair;
typedef long long ll;

int main()
{
	//int u=65;
	//cout<<(char)u<<endl;
	ifstream ff("file.txt");
	int n;
	ff>>n;
	vector <Pair> ip(n);
	int len;
	int  jk=0;
	for(int i=0; i<n; i++)
	{
		ff>>ip[i].first;
		jk=0;
		if(ip[i].first[0]=='R')
		{
			len=ip[i].first.length();
			for(int j=0; j<len-1; j++)
			{
				if(isalpha(ip[i].first[j])==false && isalpha(ip[i].first[j+1])==true)
				{
					ip[i].second=0;
					goto line1;
				}
			}
			ip[i].second=1;
line1:;
		}
		else
		{
			ip[i].second=1;
		}
	}
	string str="";
	string str1="";
	ll O_1=0;
	int mom=0;
	bool b2=false;
	int hran;
	int hramoct;

	for(int i=0; i<n; i++)
	{
		len=ip[i].first.length();
		mom=0;
		O_1=0;
		str="";
		str1="";
		b2=false;
		if(ip[i].second==1)
		{
			for(int j=len-1; j>=0; j--)
			{
				if(isalpha(ip[i].first[j])==false)
				{
					str+=ip[i].first[j];
				}
				else
				{
					str1+=ip[i].first[j];
					O_1+=((int)ip[i].first[j]-64)*(mom==0 ? 1 : mom *26);
					//cout<<((int)ip[i].first[j])<<endl;
					//cout<<O_1<<endl;
					mom++;
				}
			}
			reverse(str.begin(),str.end());
			cout<<'R'<<str<<'C'<<O_1<<endl;;
		}
		else
		{
			for(int j=1; j<len; j++)
			{
				if(isalpha(ip[i].first[j])==false)
				{
					if(b2==false)
					{
						str+=ip[i].first[j];
					}
					else
					{
						str1+=ip[i].first[j];
					}
				}
				else
				{
					b2=true;
				}

			}

			istringstream ist(str1);
			ist>>hran;
			//������ � ���
			hramoct=hran%26;
			hran=hran/26;
			stringstream ss;
			ss<<hran;
			ss>>str1;
			//������ �������
			string helpf="";
			//cout<<str1<<endl;
			int lym=str1.length();
			for(int j=0; j<lym; j++)
			{
				if(hran!=0)
				{
					helpf+=((int)(hran)+64);
				}
				//cout<<helpf<<endl;
			}
			//cout<<helpf<<endl;
			helpf+=(int)(hramoct+64);

			cout<<helpf<<str<<endl;
		}
	}

	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <climits>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

typedef pair<string, bool> Pair;
typedef long long ll;
vector <ll> v3;

int f1(int y)
{
    int x=1;
	if(y==1)
	{
		return x;
	}
	else
	{
		for(int i=0; y!=1; i++)
		{
			x=x*26;
			y--;
		}
		return x;
	}
}

/////////
int f2(int y, int o, int &qw)
{
	//cout<<v3[o]<<endl;
	int x=y%v3[o];
	qw=x/v3[o-1];
	return x;
}

void fv();

int main()
{
	//ifstream ff("file.txt");
	fv();
	int n;
	cin>>n;
	string s="";
	string s2="";
	string result="";
	string s3="";
	int sum=0;
	vector <Pair> v1(n);
	vector <string> v2(n);
	enum DL {A, D, A1};
	bool b1=false;
	bool b2=false;
	DL P=A;
	for(int i=0; i<n; i++)
	{
		cin>>v1[i].first;
		P=A;
		s="";
		s2="";
		result="";
		s3="";
		sum=0;
		b2=false;
		b1=false;
		for(int j=0; j<v1[i].first.size(); j++)
		{
			if(P==A && isalpha(v1[i].first[j])==false)
			{
				P=D;
			}
			else if(P==D && isalpha(v1[i].first[j])==true)
			{
				P=A1;
				break;
			}

		}
		if(P==D)
		{
			v1[i].second=true;
		}
		else if(P=A1)
		{
			v1[i].second=false;
		}
		P=A;
		//cout<<v1[i].second<<endl;
		if(v1[i].second == true)
		{
			for(int j=0; j<v1[i].first.size(); j++)
			{
				if(isalpha(v1[i].first[j])==false)
				{
						//s+='R';
					b1=true;
				}
				if(b1==true)
				{
					s+=v1[i].first[j];
				}
				else
				{
					s2+=v1[i].first[j];
				}
			}
			b1=false;
			for(int j=0; j<s2.size(); j++)
			{
				if(j!=s2.size()-1)
				{
					//cout<<((int)s2[j]-64)<<endl;
					sum+=((int)s2[j]-64)*f1(s2.size()-j);
					//cout<<sum<<" "<<s2.size()<<endl;
				}
				else
				{
					sum+=((int)s2[j]-64);
				}
			}
			//cout<<sum<<endl;
			string u;
			stringstream ss;
			ss<<sum;
			ss>>u;
			result='R'+s+"C"+u;
			cout<<result<<endl;

		}
		else
		{
			P=A;
			for(int j=0; j<v1[i].first.size(); j++)
			{
				 if(P==A && isalpha(v1[i].first[j])==false && b2==false)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==false && b2==true)
				{
					s+=v1[i].first[j];
					b2=true;
				}
				else if(P==A && isalpha(v1[i].first[j])==true && b2==true)
				{
					P=D;
				}
				else if(P==D && isalpha(v1[i].first[j])==false && b2==true)
				{
					s2+=v1[i].first[j];
				}

			}
			int y;
			istringstream ist(s2);
			ist>>y;
			int r;
			int it=y%26;
			if(it==0)
			{
				it=26;
			}
			s3+=char(it+64);
			//cout<<s3<<endl;
			y-=it;
			if(y/26<=26 && y!=0)
			{
				it=y/26;
				s3+=char(it+64);
				y=0;
			}
			int o=1;
			while(y!=0)
			{
				//cout<<y<<endl;

				{
					int qw;
					it=f2(y, o, qw);
					//cout<<it<<endl;
					//cout<<qw<<endl;
					if(qw==0)
					{
						qw=26;
					}
					if(it==0)
					{
						y-=v3[o];
						//cout<<v3[o]<<endl;
					}
					else
					{
						y-=it;
					}
					s3+=char(qw+64);
				}
				o++;
			}
			reverse(s3.begin(), s3.end());
			string result1=s3+s;
			cout<<result1<<endl;
			//////////
		}
	}

	return 0;
}

void fv()
{
	int x=1;
	while(x<1000000)
	{
		x*=26;
		v3.push_back(x);
	}
}