#include <bits/stdc++.h>
using namespace std;
int n, m, counter, dest;
vector<vector<pair<int, int>>> tree;
bool visited[100005] = {0};
bool colors[10000] = {0};
int dfs(int v, int color) {
  visited[v] = 1;
  if (v == dest) {
    if (!colors[color]) counter++;
    visited[v] = 0;
    colors[color] = 1;
    return 0;
  }
  for (int i = 0; i < tree[v].size(); i++) {
    if (!visited[tree[v][i].first] && tree[v][i].second == color) {
      dfs(tree[v][i].first, color);
    }
  }
  visited[v] = 0;
  return 0;
}
int dfs(int v) {
  visited[v] = 1;
  for (int i = 0; i < tree[v].size(); i++) {
    dfs(tree[v][i].first, tree[v][i].second);
  }
  for (int i = 0; i < tree[v].size(); i++) {
    colors[tree[v][i].second] = 0;
  }
  visited[v] = 0;
  return 0;
}
int main() {
  cin >> n >> m;
  tree.resize(n);
  int a, b, c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    tree[a - 1].push_back(pair<int, int>(b - 1, c));
    tree[b - 1].push_back(pair<int, int>(a - 1, c));
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    counter = 0;
    cin >> a >> dest;
    dest -= 1;
    dfs(a - 1);
    cout << counter << endl;
  }
  return 0;
}
