#include #include #include #include #include #include #include #include #include #include using namespace std; struct edge{ int to; int cap; int rev; }; int Ford_Fulkerson_dfs(vector > &G, int pos, int flow, vector &used, int t){ if(pos == t){ return flow; }else{ used[pos] = true; for(int i=0; i0){ int myflow = Ford_Fulkerson_dfs(G, G[pos][i].to, min(flow, G[pos][i].cap), used, t); if(myflow > 0){ G[pos][i].cap -= myflow; G[ G[pos][i].to ][ G[pos][i].rev ].cap += myflow; return myflow; } } } return 0; } } int Ford_Fulkerson(vector > &G, int s, int t){ const int INF = 100000000; int flow=0; for(;;){ vector used(G.size(), false); int f = Ford_Fulkerson_dfs(G, s, INF, used, t); if(f==0) break; else flow += f; } return flow; } void add_edge(vector > &G, int from, int to, int cap){ G[from].push_back( (edge){to, cap, (int)G[to].size()} ); G[to].push_back( (edge){from, 0, (int)G[from].size()-1} ); } void solve(){ char c[2000]; scanf("%s", c); string s = c; int n=s.size(); vector G; vector R; vector W; for(int i=0; i> Graph(n+2); int source = n; int sink = n+1; for(int i=0; i0){ add_edge(Graph, W[i-1], W[i], 1000); } */ auto next = lower_bound(G.begin(), G.end(), W[i]); if(next == G.end()){ cout << "impossible" << endl; return; } add_edge(Graph, W[i], *next, 1); } for(int i=0; i0){ add_edge(Graph, G[i-1], G[i], 1000); } auto next = lower_bound(R.begin(), R.end(), G[i]); if(next == R.end()){ cout << "impossible" << endl; return; } add_edge(Graph, G[i], *next, 1); } for(int i=0; i0){ add_edge(Graph, R[i-1], R[i], 1000); } } int ans = Ford_Fulkerson(Graph, source, sink); if(ans == G.size() && W.size()>=ans){ cout << "possible" << endl; }else{ cout << "impossible" << endl; } } int main(){ int T; cin >> T; while(T--){ solve(); } }