// Arup Guha
// 2/24/2025
// Alternate Solution to 2025 COP 4516 Final Individual Contest Problem: Follow the Rainbow Brick Roads

import java.util.*;

public class oz {

	final public static String COLORS = "ROYGBIV";
	public static int[] bitmap;
	final public static long INFINITY = 1000000000000L;
	
	public static void main(String[] args) {
	
		// Store which index each color is in.
		bitmap = new int[1<<8];
		for (int i=0; i<COLORS.length(); i++)
			bitmap[COLORS.charAt(i)] = i;
		
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=0; loop<nC; loop++) {
			
			// Get basic parameters.
			int n = stdin.nextInt();
			int m = stdin.nextInt();
			int q = stdin.nextInt();
			
			// Make seven lists for each color.
			ArrayList<edge>[] edges = new ArrayList[7];
			for (int i=0; i<7; i++) edges[i] = new ArrayList<edge>();
			
			// Store each edge in the appropriate list.
			for (int i=0; i<m; i++) {
				int u = stdin.nextInt()-1;
				int v = stdin.nextInt()-1;
				long w = stdin.nextInt();
				int color = bitmap[stdin.next().charAt(0)];
				edges[color].add(new edge(u, v, w));
				edges[color].add(new edge(v, u, w));
			}
			
			// Store shortest distances for all bitmasks of colors here.
			long[][] dist = new long[1<<7][];
			
			// Try each combination of roads.
			for (int mask=0; mask<(1<<7); mask++) {
				
				// Store the subgraph with only edges with the colors represented by mask.
				ArrayList<edge> mygraph = new ArrayList<edge>();
				
				// Go through each color to see if it's in mask.
				for (int j=0; j<7; j++) {
					
					// Skip over this color roads, they will not be added.
					if ((mask&(1<<j)) == 0) continue;
					
					// Just add these edges in.
					for (edge e: edges[j])
						mygraph.add(e);
				}
				
				// Run Bellman-Ford.
				dist[mask] = bellmanford(mygraph, n, 0);
			}
			
			// Process queries.
			for (int i=0; i<q; i++) {
				
				// Get destination and subset of colors.
				int dest = stdin.nextInt()-1;
				String mycolors = stdin.next();
				
				// Convert subset of colors to a mask.
				int mymask = 0;
				for (int j=0; j<mycolors.length(); j++)
					mymask += (1<<bitmap[mycolors.charAt(j)]);
				
				// Here is my answer.
				long res = dist[mymask][dest];
				
				// Output accordingly.
				if (res == INFINITY)
					System.out.println(-1);
				else
					System.out.println(res);
			}
		}
	}
	
	// Returns shortest distances in the graph from source vertex s to all other vertices.
	public static long[] bellmanford(ArrayList<edge> graph, int numV, int s) {
		
		// Set up.
		long[] res = new long[numV];
		Arrays.fill(res, INFINITY);
		res[s] = 0;
		
		// Edge relaxation n times. (Can do n-1...)
		for (int i=0; i<numV; i++)
			for (edge e: graph)
				res[e.v] = Math.min(res[e.v], res[e.u] + e.w);
			
		// Ta da!
		return res;
	}
}

class edge {
	public int u;
	public int v;
	public long w;
	
	public edge(int myu, int myv, long myw) {
		u = myu;
		v = myv;
		w = myw;
	}
}