#!/usr/bin/env python3 class UFNode: def __init__(self): self.parent = self def find(self): if self.parent is not self: self.parent = self.parent.find() return self.parent def union(self, other): self.find().parent = other.find() def makegrid(N, routes, ignoreK): result = [UFNode() for _ in range(N)] for U, V, K in routes: if K != ignoreK: result[U-1].union(result[V-1]) for i in range(N): result[i] = result[i].find() result[i].Kset = set() for U, V, K in routes: if K != ignoreK: result[U-1].Kset.add(K) return result for x in range(1, int(input()) + 1): N, M, Q = [int(a) for a in input().split()] routes = [[int(a) for a in input().split()] for i in range(M)] comps = [[int(a) for a in input().split()] for i in range(Q)] grid = [makegrid(N, routes, K) for K in ({K for U, V, K in routes} | {0})] y = 0 for P, C in comps: for line in grid: if line[P-1] is line[C-1] and len(line[P-1].Kset) % 2 == 1: y += 1 break print(f'Case #{x}:', y)