#include <iostream>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to, cost, next;
}edge[MAXN * MAXN];
int n; //任务数
int time[MAXN]; //完成任务所需时间
int total, head[MAXN];
int dis[MAXN], vis[MAXN], cnt[MAXN];
void addEdge(int now, int v, int w)
{
edge[total].to = v;
edge[total].cost = w;
edge[total].next = head[now];
head[now] = total++;
}
bool SPFA()
{
for (int i = 0; i < MAXN; i++)
{
vis[i] = 0;
cnt[i] = 0;
dis[i] = -INF;
}
queue<int> q;
dis[0] = 0;
vis[0] = 1;
cnt[0] = 1;
q.push(0);
while (!q.empty())
{
int now = q.front();
q.pop();
vis[now] = 0;
for (int i = head[now]; i != -1; i = edge[i].next)
{
int v = edge[i].to, w = edge[i].cost;
if (dis[v] < dis[now] + w)
{
dis[v] = dis[now] + w;
if (!vis[v])
{
vis[v] = 1;
q.push(v);
cnt[v]++;
if (cnt[v] > n)
return false;
}
}
}
}
return true;
}
int main()
{
int Case = 0;
while (cin >> n)
{
if (n == 0)
break;
for (int i = 1; i <= n; i++)
cin >> time[i];
total = 0;
memset(head, -1, sizeof(head));
string s;
int a, b;
while (cin >> s)
{
if (s == "#")
break;
cin >> a >> b;
if (s == "FAS")
addEdge(b, a, -time[a]);
else if (s == "FAF")
addEdge(b, a, time[b] - time[a]);
else if (s == "SAF")
addEdge(b, a, time[b]);
else if (s == "SAS")
addEdge(b, a, 0);
}
for (int i = 1; i <= n; i++)
addEdge(0, i, 0);
cout << "Case " << ++Case << ":" << endl;
if (SPFA())
{
for (int i = 1; i <= n; i++)
cout << i << " " << dis[i] << endl;
}
else
cout << "impossible" << endl;
cout << endl;
}
return 0;
}