#include <bits/stdc++.h>
using namespace std;
struct node {
  vector<pair<int, int> > colors;
};
struct grafo {
  node *array;
  int tam;
  grafo(int v) {
    tam = v;
    array = new node[v + 1];
  }
  ~grafo() { delete[] array; }
  void insert_node(int a, int b, int c) {
    array[a].colors.push_back(make_pair(b, c));
    array[b].colors.push_back(make_pair(a, c));
  }
};
void DFS(int start, int fim, bool visited[][101], grafo &x, int color,
         int cont);
bool flag;
int main() {
  int vertices, arestas, a, b, c;
  scanf("%d %d", &vertices, &arestas);
  grafo x(vertices);
  for (int i = 1; i <= arestas; i++) {
    scanf("%d %d %d", &a, &b, &c);
    x.insert_node(a, b, c);
  }
  int consultas, origem, destino;
  bool visited[vertices + 1][101];
  scanf("%d", &consultas);
  for (int i = 0; i < consultas; i++) {
    memset(visited, 0, sizeof(visited));
    scanf("%d %d", &origem, &destino);
    int cont = 0;
    for (int i = 0; i < x.array[origem].colors.size(); i++) {
      flag = false;
      DFS(origem, destino, visited, x, x.array[origem].colors[i].second, 0);
      if (flag) ++cont;
    }
    cout << cont << endl;
  }
}
void DFS(int start, int fim, bool visited[][101], grafo &x, int color,
         int cont) {
  visited[start][color] = true;
  if (start == fim) {
    flag = true;
    return;
  }
  for (int i = 0; i < x.array[start].colors.size(); i++) {
    if (flag) break;
    if (!visited[x.array[start].colors[i].first][color] &&
        x.array[start].colors[i].second == color) {
      DFS(x.array[start].colors[i].first, fim, visited, x, color, cont + 1);
    }
  }
}
